
var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) {
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}

ParseForNumber1(object)

if(keycode >= 48){
ValidatePhone(object)
}
}

function backspacerDOWN(object,e) {
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}

function GetCursorPosition(){

var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i=0; i<t1.length; i++)
{
if (t1.substring(i,1) != t2.substring(i,1)) {
if(!bool) {
cursorposition=i
bool=true
}
}
}
}

function ValidatePhone(object){

var p = phonevalue1

p = p.replace(/[^\d]*/gi,"")

if (p.length < 3) {
object.value=p
} else if(p.length==3){
pp=p;
d4=p.indexOf('(')
d5=p.indexOf(')')
if(d4==-1){
pp="("+pp;
}
if(d5==-1){
pp=pp+")";
}
object.value = pp;
} else if(p.length>3 && p.length < 7){
p ="(" + p;
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

object.value = pp;

} else if(p.length >= 7){
p ="(" + p;
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

l40 = pp.length;
p40 = pp.substring(0,8);
p40 = p40 + "-"

p41 = pp.substring(8,l40);
ppp = p40 + p41;

object.value = ppp.substring(0, maxphonelength);
}

GetCursorPosition()

if(cursorposition >= 0){
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1=object.value.indexOf(')')
e2=object.value.indexOf('-')
if (e1>-1 && e2>-1){
if (e2-e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}

var txtRange = object.createTextRange();
txtRange.moveStart( "character", cursorposition);
txtRange.moveEnd( "character", cursorposition - object.value.length);
txtRange.select();
}

}

function ParseChar(sStr, sChar)
{
if (sChar.length == null)
{
zChar = new Array(sChar);
}
else zChar = sChar;

for (i=0; i<zChar.length; i++)
{
sNewStr = "";

var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);

while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

sStr = sNewStr;
}

return sNewStr;
}



function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}


function valid(f) {
if (!/^\d*$/.test(f.value)) {
alert("Digits only!");
f.value = f.value.replace(/[^\d]/g,"");
}
} 

function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}

function Trim(stringToTrim) 
   {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
   }


function submit_form() {
 //document.getElementById("txtLastName").value=Trim(document.getElementById("txtLastName").value);
 //document.getElementById("FName").value=Trim(document.getElementById("FName").value);
 if (checkWholeForm(document.application))
	{
	    document.application.action = "submit_1.asp"
		document.application.submit();
	} 
	else {
    	return false; }
}

function checkWholeForm(theForm) {
    var why = "";

    
    theForm.email.value=Trim(theForm.email.value);
    why += checkEmail(theForm.email.value, "Email");

    theForm.confirm_email.value = Trim(theForm.confirm_email.value);
    why += checkEmail(theForm.confirm_email.value, "Confirmed Email");

    theForm.password.value = Trim(theForm.password.value);
    why += checkPassword(theForm.password.value, "Password");

    theForm.password2.value = Trim(theForm.password2.value);
    if (!(theForm.password.value == theForm.password2.value))
        why += "Password and Confirmed password must be the same\n";

    why += checkDropdown(theForm.SecQuestion.selectedIndex, "Security Question");

    theForm.SecAnswer.value = Trim(theForm.SecAnswer.value);
    why += isEmptyDetails(theForm.SecAnswer.value, "Sequrity Answer");

    why += checkDropdown(theForm.title.selectedIndex, "Title");

    theForm.FName.value = Trim(theForm.FName.value);
    why += checkContactName(theForm.FName.value, "First Name");

    theForm.LName.value = Trim(theForm.LName.value);
    why += checkContactName(theForm.LName.value, "Last Name");

    if (isDate(theForm.DOfB.value) == false) {
        why += "Date of birth is wrong\n";
    }
    
    theForm.address.value = Trim(theForm.address.value);
    why += isEmptyDetails(theForm.address.value, "Address");

    theForm.PostalCode.value = Trim(theForm.PostalCode.value);
    why += isEmptyDetails(theForm.PostalCode.value, "PostalCode Code");

    theForm.City.value = Trim(theForm.City.value);
    why += isEmptyDetails(theForm.City.value, "City");

    why += checkDropdown(theForm.Country.selectedIndex, "Country");

    why += checkDropdown(theForm.Province.selectedIndex, "Province");

    why += checkPhone(theForm.Phone.value);

    for (i = 0, n = theForm.ValidDL.length; i < n; i++) {
        if (theForm.ValidDL[i].checked) {
            var checkvalue4 = theForm.ValidDL[i].value;
            break;
        }
    }
    var strValidDL = checkRadio(checkvalue4, "Valid Driver License");
    if (strValidDL == "") {
        if (theForm.ValidDL[0].checked) {
            why += checkDropdown(theForm.DLCountry.selectedIndex, "Driver License Country");
            why += checkDropdown(theForm.DrLicensePro.selectedIndex, "Driver License Province");
            why += checkDropdown(theForm.DrLicenseCl.selectedIndex, "Driver License Class");
        }
    }
    else
        why += strValidDL;




    why += checkDropdown(theForm.Education.selectedIndex, "Level of Education");



    //Work



    for (i = 0, n = theForm.ExpInOil.length; i < n; i++) {
        if (theForm.ExpInOil[i].checked) {
            var checkvalue2 = theForm.ExpInOil[i].value;
            break;
        }
    }
    
    
    why += checkRadio(checkvalue2, "Work experience in the Oil Industry");



    if (isDate(theForm.Date_available.value) == false) {
        why += "Available from Date is wrong\n";
    }

    //if (isDate(theForm.Date_available_to.value) == false) {
    //    why += "Available to Date is wrong\n";
    //}

    var date_from = new Date();
    //var date_to = new Date();


    //if (isDate(theForm.Date_available.value) || isDate(theForm.Date_available_to.value)) {
    //    date_from = theForm.Date_available.value

    //    date_to = theForm.Date_available_to.value

    //    if (date_to < date_from) {
    //        why += "Available from Date is later than Available to Date\n";
    //    }
    //}

    why += checkDropdown(theForm.reference.selectedIndex, "Reference");

    why += checkDropdown(theForm.Position1.selectedIndex, "Looking for Position 1");

    if (!document.getElementById("terms").checked) {
        why += "You have to accept terms of service";
    }
                  
//    theForm.FName.value=Trim(theForm.FName.value);
//    theForm.txtLastName.value = Trim(theForm.FName.value);

//    alert("");    
   // why += checkPhone(theForm.txtPhone.value);

//    why += checkContactName(theForm.FName.value,"First Name");
//    why += checkContactName(theForm.LName.value,"Last Name");    
//    why += isEmptyKey(theForm.txtKeywords.value);
//    why += isEmptyDetails(theForm.txtDetails.value);
//    why += isEmptyModel(theForm.txtModel.value);
	//why += isMileageNumeric();
//	why += isYearCorrect();
//	why += isPriceCorrect();
//    why += isDifferent(theForm.different.value);
 //   for (i=0, n=theForm.radios.length; i<n; i++) {
 //       if (theForm.radios[i].checked) {
 //           var checkvalue = theForm.radios[i].value;
 //           break;
 //       } 
 //   }
 //   why += checkRadio(checkvalue);
 //   why += checkDropdown(theForm.choose.selectedIndex);




    if (why != "") {
        why = "_____________________________\n" +
        "You failed to correctly fill in your:\n\n" +
        why + "\n_____________________________" +
        "\nPlease re-enter and submit again!";
        alert(why);
        return false;
    }
    else {
        //if (!checkfile())
        //    return false;
        //else
            return true;
    }
}


function show_workexperience(x) {
    //alert(application.workexperience.disabled);
    if (x == 1)
        document.getElementById("workexperience_oil").disabled = false;
    else
        document.getElementById("workexperience_oil").disabled = true;
}

function show_license(x) {
    //alert(application.workexperience.disabled);
    if (x == 1)
        document.getElementById("workexperience_oil").disabled = false;
    else
        document.getElementById("workexperience_oil").disabled = true;
}


function numchar(str, x) {
    var count;
    count = 0;
    for (i = 0; i <= (str.length - 1); i++) {
        if (str.charAt(i) == x) {
            count++;
        }
    }
    return count;
}

// email

function checkEmail (strng, name) {
var error="";
if (strng == "") {
   error = "Put your email.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = name + " contains errors.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "email contains errors.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng, name) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 10)) {
       error = "The " + name + " is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The " + name + " contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The " + name + " must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       

// username - 4-10 chars, uc, lc, and underscore only.

function checkContactName (strng, name) {
var error = "";
if (strng == "") {
   error = "write " + name + ".\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 1) || (strng.length > 20)) {
       error = name + " wrong length.\n";
    }
//    else if (illegalChars.test(strng)) {
//    error = name + " contains errors.\n";
//    } 
return error;
}       

// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.\n"
  }
return error;	  
}// Model textbox

function isEmptyModel(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The Model has not been selected.\n"
  }
return error;	  
}
// Key Info textbox

function isEmptyKey(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The Key Info area has not been filled in.\n"
  }
return error;	  
}
// Details Info textbox

function isEmptyDetails(strng, input_name) {
var error = "";
  if (strng.length == 0) {
     error = "The " + input_name + " has not been filled in.\n"
  }
return error;	  
}

// Mileage textbox
function isMileageNumeric(){var error = "";
if(!isNumeric(document.application.txtMileage.value) && document.application.txtMileage.value != ""){
	error = "Mileage value should be numeric.\n";
	return error;
  }
return error;
}
// Year textbox
function isYearCorrect(){var error = "";
var d1 = new Date();
var y1 = d1.getFullYear()+2;
if(!isNumeric(document.application.txtYear.value))
	{error = "Year value should be numeric.\n"
	;return error;}

if(!(document.application.txtYear.value >= 1901 && document.application.txtYear.value <= y1))
	{error = "Year value should be between 1901 and " + y1 + "\n";
	return error;}
return error;
}

// Year textbox
function isPriceCorrect(){var error = "";
if(!isNumeric(document.application.txtPrice.value))
	{error = "Price value should be numeric.\n";
	return error;}
return error;
}function isNumeric(str) {
	var isValid = true;
	if(str.search(/^[0-9]+$/) == -1) {
	isValid = false;
	}
	return isValid;
}

function checkMileage(obj)
{
	if(!isNumeric(obj.value) && obj.value != "")
		{alert("Please indicate mileage as a numeric value. To proceed, remove all commas, decimals, text or other non-numeric characters.");
		obj.focus();
		obj.select();
		return false;}
}
function checkPrice(obj)
{
	if(!isNumeric(obj.value) && obj.value != "")
		{alert("Please indicate price as a numeric value. To proceed, remove all commas, decimals, text or other non-numeric characters.");
		obj.focus();
		obj.select();
		return false;}
}// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue, name) {
var error = "";
   if (!(checkvalue)) {
       error = "Please answer: " + name + ".\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice, name) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the " + name + " list.\n";
    }    
return error;
}    

// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var error=""
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strYear = dtStr.substring(0, pos1)
	var strMonth = dtStr.substring(pos1 + 1, pos2)
	var strDay = dtStr.substring(pos2 + 1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
	    error="The date format should be : yyyy-mm-dd"
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		error="Please enter a valid month"
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		error="Please enter a valid day"
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		error="Please enter a valid 4 digit year between "+minYear+" and "+maxYear
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		error="Please enter a valid date"
		return false
	}
return true
}


function checkfile()
{
 var badNameJPG=""; 
 var allBlank=true;
 var rxJPG=new RegExp("[^\.]\.jpg\s*$", "i");

 var obj;

 obj = document.application.ImageUp.value.replace(/^\s+/, '').replace(/\s+$/, ''); 	  
if( obj.length )
   { 
    allBlank=false;
    if( !rxJPG.test(obj) )
	     badNameJPG+='\n\n' + obj;
   }  

 if( !allBlank )
	if ( badNameJPG ){
		   alert( 'A required .jpg extension was not found in:' + badNameJPG);
			return false;	
			}
return true;
}
