/*
    Author:  Lance Baker
    Date: 04/05/2008
    Purpose: Form Validation and Order Reviewing
*/
function GetXmlHttpObject(){
var xmlHttp = null;
try{
	xmlHttp=new XMLHttpRequest();
	} catch (e){
	try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function resetErrors(){
 // Changes the Error Display to None; which hides the corresponding span IDs
      document.getElementById("error_message").style.display = 'none';
}
function displayError(type) {
	var message = "";
	document.getElementById("error_message").style.display = '';
	switch (type) {
		case 1:
			message = "First Name Must Contain a Value.";
			break;
		case 2:
			message = "Last Name Must Contain a Value.";
			break;
		case 3:
			message = "Street Address Must Contain a Value.";
			break;
		case 4:
			message = "Suburb Must Contain a Value.";
			break;
		case 5:
			message = "Postcode Must Contain a Value.";
			break;
		case 6:
			message = "A Formatted Phone Number is Required. Example (00)0000-0000.";
			break;
		case 7:
			message = "Must Contain a Valid Email Address.";
			break;
		case 8:
			message = "'Year Completed' must contain a valid Year.";
			break;
		case 9:
			message = "Australian Citizen / Permanent Resident? Select either Yes or No.";
			break;
		case 10:
			message = "Do you have a prior qualification? Select either Yes or No.";
			break;
		case 11:
			message = "Do you have a drivers license? Select either Yes or No.";
			break;
		case 12:
			message = "Do you have your own transport? Select either Yes or No.";
			break;
		case 13:
			message = "Do you hold an OH&S induction card? Select either Yes or No.";
			break;
		case 14:
			message = "Please explain what prior qualification you hold, Must Contain a Value.";
			break;
		case 15:
			message = "Please ensure that you have uploaded your resume";
			break;
		case 16:
			message = "The file type for the resume is invalid; make sure it is in .doc format.";
			break;
		case 17:
			message = "Please ensure that you have read and agreed to our privacy policy.";
			break;
		case 18:
			message = "Please select your schooling level.";
			break;
		case 19:
			message = "Please select your license type.";
			break;
		case 20:
			message = "Email already exists, if you are already a member use the login area";
			break;
		case 21:
			message = "Email doesn't exist.";
			break;
		case 22:
			message = "Company Name must contain a value";
			break;
		case 23:
			message = "Contact Person must contain a value";
			break;
		case 24:
			message = "Please select a state";
			break;
		case 25:
			message = "Vacancy must contain a value";
			break;
		case 26:
			message = "No of Positions must contain a numeric value";
			break;
		case 27:
			message = "Location must contain a value";
			break;
		case 28:
			message = "Please select a region for the position";
			break;
		case 29:
			message = "Please select a category for the position";
			break;
		case 30:
			message = "Please select a education level";
			break;
		case 31:
			message = "Please select the position status";
			break;
		case 32:
			message = "Please select a position type";
			break;
		case 33:
			message = "Please select a transport method";
			break;
		case 34:
			message = "Enter a short description";
			break;
		case 35:
			message = "Enter the skills required";
			break;
		case 36:
			message = "Enter the position description";
			break;
		case 37:
			message = "Please select the age required";
			break;
		default:
			resetErrors();
	}
	document.getElementById("error_message").innerText = message;
	document.getElementById("error_message").textContent = message;
}
function validateInputText(inputvar, length, error){
 // The Inputvar is the variable of the form element.
 // The following just does a simple test to determine whether the value is empty.
   if ((inputvar.value == "") || (inputvar.value.length > length)){
       // Sets the display of the corresponding error message span to display; by removing the hidden setting.
	   displayError(error);
	   // Focuses in on the corresponding element.
	   inputvar.focus();
	   // Returns False, as the value is invalid.
	   return false;
   } else {
       // Rehides the Error span incase of it already being displayed.
	   displayError(0);
	   // Returns 1 as the statement is true. This will therefore be used to determine whether the test has been successful.
	   return true;
   }
}

function validateNumber(inputvar, length, error) {
	if ((inputvar.value == "") || (inputvar.value.length > length) || (!(IsNumeric(inputvar.value)))){
       // Sets the display of the corresponding error message span to display; by removing the hidden setting.
	   displayError(error);
	   // Focuses in on the corresponding element.
	   inputvar.focus();
	   // Returns False, as the value is invalid.
	   return false;
   } else {
       // Rehides the Error span incase of it already being displayed.
	   displayError(0);
	   // Returns 1 as the statement is true. This will therefore be used to determine whether the test has been successful.
	   return true;
   }
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
}

function validatePriorQual(priorQual, inputvar, error) {
	var myOption = -1;
	for (var i= priorQual.length-1; i > -1; i--) {
       if (priorQual[i].checked){
		   myOption = i; i = -1;
       }
	}
	if (myOption > -1) {
		if (priorQual[myOption].value == "1") {
			if (inputvar.value == "") {
				displayError(error);
				return false;
			} else {
				displayError(0);
				return true;
			}
		} else {
			
			return true;
		}
	} else {
		return false;
	}
}


function validateCode(inputvar, error) {
	if ((inputvar.value == "") || (inputvar.value.length != 4) || (!(IsNumeric(inputvar.value)))){
       // Sets the display of the corresponding error message span to display; by removing the hidden setting.
	   displayError(error);
	   // Focuses in on the corresponding element.
	   inputvar.focus();
	   // Returns False, as the value is invalid.
	   return false;
   } else {
       // Rehides the Error span incase of it already being displayed.
	   displayError(0);
	   // Returns 1 as the statement is true. This will therefore be used to determine whether the test has been successful.
	   return true;
   }
}

function setApproval(value, positionId) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	if ((IsNumeric(positionId)) && positionId > 0) {
		var url="ajax_execution.php";
		url=url+"?submit=2";
		url=url+"&posId="+positionId;
		url=url+"&value="+value;
		url=url+"&sid="+Math.random();
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
	}
	if (xmlHttp.responseText == 1) {
		window.location = "admincp.php?action=positionmanagement";
	}
}

function validateEmail(email, chkExists, edit) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	var emailPat=/^\w+(\.\w+){0,}@\w+(\.\w+){1,}(\.\w+){0,}$/;
	if (emailPat.test(email.value)){
		var url="ajax_execution.php";
		url=url+"?submit=1";
		url=url+"&email="+email.value;
		url=url+"&sid="+Math.random();
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		if ((!xmlHttp.responseText) && (chkExists) && (!edit)) {
					displayError(21);
					email.focus();
					return false;
		} else if ((xmlHttp.responseText) && (!chkExists) && (!edit)) {
					displayError(20);
					email.focus();
					return false;
		} else {
					displayError(0);
					return true;
		}
	} else {
       // If the statement is false; it reveals the error message span; by removing the hidden setting.
       displayError(7);
	   // Focuses in on the email address element.
	   email.focus();
	   email.select();
	   return false;
	}
}

function validatePhone(inputvar){
   // The Following Phone Pattern will be used to check if the phone number is in the appropriate format.
   // For example a correctly formatted Phone Number would be; (00)00-000000.
   var phonePat=/^\(?\d{2}\)\d{4}([-\/\.])\d{4}$/;
   // The following checks if the statement is successful.
   if (phonePat.test(inputvar.value) && inputvar.value != '(00)0000-0000'){
       // If the statement is true; it sets the span error message to hide. 
	   displayError(0);
	   // Returns 1 as the statement is true. This will therefore be used to determine whether the test has been successful.
	   return true;
   } else {
       // If the statement is false; it reveals the error message span; by removing the hidden setting.
       displayError(6);
	   // Focuses in on the phone number element.
	   inputvar.focus();
	   // Selects the existing data within the Phone Number Field.
	   inputvar.select();
	   return false;
   }
}

function validateRadio(inputvar, error){
    // The following is a for loop; this determines the quantity of radio buttons with the ".length". And does a loop for that amount of times.
	var myOption = -1;
	for (var i= inputvar.length-1; i > -1; i--) {
		if (inputvar[i].checked) {
			myOption = i; i = -1;
		}
	}
	if (myOption == -1) {
		displayError(error);
		return false;
	} else {
		return true;
	}
}
function validateCheckBox(inputvalue, error){
   // The inputvalue variable is the checkbox from the form.
   // The following checks whether the variable is true.
   if (inputvalue == true){
       // If it is true; it hides the span error message, and returns 1.
       displayError(0);
	   return true;
   } else {
       // If it is false; it reveals the span error message, and returns false.
       displayError(error);
	   return false;
   }
}

function getExt(filename) {
   var dot_pos = filename.lastIndexOf(".");
   if(dot_pos == -1)
      return "";
   return filename.substr(dot_pos+1).toLowerCase();
}

function validateUploadFile(inputvar, edit) {
	if (!edit) {
		if (inputvar.value != "") {
			var ext = getExt(inputvar.value);
			if (ext == "doc") {
				displayError(0);
				return true;
			} else {
				displayError(16);
				return false;
			}
		} else {
			displayError(15);
			return false;
		}
	} else {
		displayError(0);
		return true;
	}
}

function validateCheckBox(inputvalue, error){
   // The inputvalue variable is the checkbox from the form.
   // The following checks whether the variable is true.
   if (inputvalue == true){
       // If it is true; it hides the span error message, and returns 1.
       displayError(0);
	   return true;
   } else {
       // If it is false; it reveals the span error message, and returns false.
       displayError(error);
	   return false;
   }
}

function validateSelect(inputvar, error){
 // Fetches the inputname and input variable data.
 // The Inputname is just a text variable containing the name of the element. This will be used for the displaying of the corresponding error message.
 // The Inputvar is the variable of the form element.
 // The following just does a simple test to determine whether the value is empty.
   if (inputvar.value == "" || inputvar.value == "0"){
	   displayError(error);
	   inputvar.focus();
	   return false;
   } else {
	   displayError(0);
	   return true;
   }
}

function validateLicenseType(appDriverLicense, appDriverLicenseType, error) {
	var myOption = -1;
	for (var i= appDriverLicense.length-1; i > -1; i--) {
       if (appDriverLicense[i].checked){
		   myOption = i; i = -1;
       }
	}
	if (myOption > -1) {
		if (appDriverLicense[myOption].value == "1") {
			if (validateSelect(appDriverLicenseType, error)) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function validateForm(theForm, edit){
   // This is the main validation function.
   // First of all it resets any errors already being displayed.
   resetErrors();
   // Now it declares the variables with the associated form elements.
   // It doesn't though set the variable with the absolute value of the form element but rather to the form element itself.   
   if (validateInputText(theForm.appFirstName, 30, 1) &&
	   validateInputText(theForm.appLastName, 30, 2) &&
       validateInputText(theForm.appStreet, 30, 3) &&
	   validateInputText(theForm.appSuburb, 30, 4) &&
	   validateCode(theForm.appPostCode, 5) &&
	   validatePhone(theForm.appPhone) &&
	   validateEmail(theForm.appEmail, false, edit) &&
	   validateSelect(theForm.appSchoolLevel.options[theForm.appSchoolLevel.options.selectedIndex], 18) &&
	   validateCode(theForm.appYearSchoolCompleted, 8) &&
	   validateRadio(theForm.appResidency, 9) &&
	   validateRadio(theForm.appPriorQual, 10) &&
	   validatePriorQual(theForm.appPriorQual, theForm.appPriorQualDesc, 14) &&
	   validateRadio(theForm.appDriverLicense, 11) &&
	   validateLicenseType(theForm.appDriverLicense, theForm.appDriverLicenseType.options[theForm.appDriverLicenseType.options.selectedIndex] , 19) &&
	   validateRadio(theForm.appHasTransport, 12) &&
	   validateRadio(theForm.appOHSInductionCard, 13) &&
	   validateCheckBox(theForm.appPrivacy.checked, 17) &&
	   validateUploadFile(theForm.appResumeFile, edit)) {
			theForm.submit();
   }
}

function validateEmpForm(theForm, edit){
   // This is the main validation function.
   // First of all it resets any errors already being displayed.
   resetErrors();
   // Now it declares the variables with the associated form elements.
   // It doesn't though set the variable with the absolute value of the form element but rather to the form element itself.   
   if (validateInputText(theForm.empCompanyName, 30, 22) &&
	   validateInputText(theForm.empContactPerson, 30, 23) &&
       validateInputText(theForm.empAddress, 30, 3) &&
	   validateInputText(theForm.empSuburb, 30, 4) &&
	   validateCode(theForm.empPostCode, 5) &&
	   validateSelect(theForm.empState.options[theForm.empState.options.selectedIndex], 24) &&
	   validatePhone(theForm.empPhone) &&
	   validateEmail(theForm.empEmail, false, edit)) {
			theForm.submit();
   }
}

function validatePositionForm(theForm) {
	resetErrors();
	if (validateInputText(theForm.posName, 60, 25) &&
		validateNumber(theForm.posVacancies, 2, 26) &&
		validateInputText(theForm.posLocation, 50, 27) &&
		validateSelect(theForm.regionId.options[theForm.regionId.options.selectedIndex], 28) &&
		validateSelect(theForm.categoryId.options[theForm.categoryId.options.selectedIndex], 29) &&
		validateSelect(theForm.posEducationLevel.options[theForm.posEducationLevel.options.selectedIndex], 30) &&
		validateSelect(theForm.posStatus.options[theForm.posStatus.options.selectedIndex], 31) &&
		validateRadio(theForm.posType, 32) &&
		validateRadio(theForm.posTransport, 33) &&
		validateRadio(theForm.posAge, 37) &&
		validateInputText(theForm.posShortDesc, 300, 34) &&
		validateInputText(theForm.posSkills, 300, 35)) {
			theForm.submit();
		}
}

function forgotPassword(theForm) {
   // This is the main validation function.
   // First of all it resets any errors already being displayed.
   resetErrors();
   // Now it declares the variables with the associated form elements.
   // It doesn't though set the variable with the absolute value of the form element but rather to the form element itself.   
   if (validateInputText(theForm.appFirstName, 30, 1) &&
	   validateInputText(theForm.appLastName, 30, 2) &&
	   validateCode(theForm.appPostCode, 5) &&
	   validateEmail(theForm.appEmail, true, false)) {
			theForm.submit();
   }
}

function forgotEmpPassword(theForm) {
   // This is the main validation function.
   // First of all it resets any errors already being displayed.
   resetErrors();
   // Now it declares the variables with the associated form elements.
   // It doesn't though set the variable with the absolute value of the form element but rather to the form element itself.   
   if (validateInputText(theForm.empCompanyName, 30, 22) &&
	   validateInputText(theForm.empContactPerson, 30, 23) &&
	   validateCode(theForm.empPostCode, 5) &&
	   validateEmail(theForm.empEmail, true, false)) {
			theForm.submit();
   }
}

window.addEvent('domready', function(){
			$$('input.DatePicker').each( function(el){
			new DatePicker(el);
			});
});
function getState(stateValue, stateId) {
			window.location = "positions.php?" + stateId + "=" + stateValue; 
}
function getRegion(regionValue, regionId, stateValue, stateId) {
			window.location = "positions.php?" + stateId + "=" + stateValue + "&" + regionId + "=" + regionValue;
}
function scbg(objRef, state){
			objRef.style.backgroundColor = (1 == state) ? '#ffffc9' : '#ffffff';
			return;
}
tinyMCE.init({
		   theme : "advanced",
		   mode: "exact",
		   elements : "box1,box2",
		   plugins : "style",
		   font_size_classes : "fontSize1,fontSize2,fontSize3,fontSize4,fontSize5,fontSize6,fontSize7",
		   font_size_style_values : "xx-small,x-small,small,medium,large,x-large,xx-large",
		   theme_advanced_toolbar_location : "top",
		   theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,fontsizeselect,fontselect",
		   theme_advanced_buttons2 : "justifyleft,justifycenter,justifyright,justifyfull,formatselect,bullist,numlist,outdent,indent",
		   theme_advanced_buttons3 : "forecolor,link,unlink,anchor,image,separator,styleprops,"
		   +"undo,redo,cleanup,code,separator,sub,sup,charmap",
		   width : "485px"
});
function showHide(id){ 
	el = document.getElementById(id); 
	el.style.display = (el.style.display != 'block')? 'block' : 'none';
}
function showElement(id){
	el = document.getElementById(id);
	el.style.display = '';
}
function hideElement(id){
	el = document.getElementById(id); 
	el.style.display = 'none';
}
function confirmDelete() {
var ok=confirm("Are you sure you want to delete this record?");
if (ok)
	return true ;
else
	return false ;
}
function valiForm(obj){
   for (i=0;i<obj.length;i++){
      if (obj[i].name.indexOf('_')==0){
         if (obj[i].value.length==0) {
            alert('You have left out some required information.\nNote that all fields marked with a * are required.');
            obj[i].focus();
            return false;
         }
      }
   }
   return true;
}