﻿/*
    Validation for Registration Form
    Created by: Brian 
    Created Date: 21/04/2009
*/
//Form validation
    function checkform ( form )
    {
    //Required Field Validation
    //First Name
    if (form.txtFirstName.value == "") {
    alert( "First name cannot be left blank." );
    form.txtFirstName.focus();
    return false ;
    }   
    //Last Name
    if (form.txtLastName.value == "") {
    alert( "Last name cannot be left blank." );
    form.txtLastName.focus();
    return false ;
    }
    //Date of Birth
    if (form.txtDOB.value == "") {
    alert( "Date of Birth cannot be left blank." );
    form.txtDOB.focus();
    return false ;
    }
    else{
    var Date = form.txtDOB.value;
        if (checkdate(Date)==false){
            alert( "Invalid Date Format." );
            form.txtDOB.value="";
            form.txtDOB.focus();
            return false ;
        }
    }
    //Gender
    if (form.ddlGender.selectedIndex == 0) {
    alert("Please select Gender");
    form.ddlGender.focus();
    return false;
    }
    //Race
    if (form.txtRace.value == "") {
    alert( "Race cannot be left blank." );
    form.txtRace.focus();
    return false ;
    }
    //Marital Status
    if (form.ddlStatus.selectedIndex == 0) {
    alert("Please Marital Status");
    form.ddlStatus.focus();
    return false;
    }
    //Nationality
    if (form.txtNationality.value == "") {
    alert( "Nationality cannot be left blank." );
    form.txtNationality.focus();
    return false ;
    }
    //Resident Country
    if (form.ddlCountry.selectedIndex == 0) {
    alert("Please select Resident Country");
    form.ddlCountry.focus();
    return false;
    }
    else{
        var ddlindex = form.ddlCountry.selectedIndex;
        var selvalue = form.ddlCountry.options[ddlindex].value;
        //if (form.ddlCountry.selectedIndex == 1){
        if (selvalue == "Singapore"){
        //Resident Type
            if (form.ddlResident.selectedIndex == 0) {
                alert("Please select Resident Type");
                form.ddlResident.focus();
                return false;
            }
            else {
                //Singapore Citizen
                if (form.ddlResident.selectedIndex == 1) {
                    //NRIC
                    if (form.txtNRIC.value == "") {
                    alert( "NRIC cannot be left blank." );
                    form.txtNRIC.focus();
                    return false ;
                    }
                }
                //SPR
                else if (form.ddlResident.selectedIndex == 2) {
                    //NRIC
                    if (form.txtPRNIRC.value == "") {
                    alert( "NRIC cannot be left blank." );
                    form.txtPRNIRC.focus();
                    return false ;
                    }
                    //Commencement Date
                    if (form.txtDatePR.value == "") {
                    alert( "Date cannot be left blank." );
                    form.txtDatePR.focus();
                    return false ;
                    }else{
                        var Date = form.txtDatePR.value;
                        if (checkdate(Date)==false){
                        alert( "Invalid Date Format." );
                        form.txtDatePR.value="";
                        form.txtDatePR.focus();
                        return false ;
                        }
                    }
                }
                //NonSPR
                else if (form.ddlResident.selectedIndex == 3) {
                    //Pass Type
                    if (form.ddlPassType.selectedIndex == 0) {
                    alert("Please select Pass Type");
                    form.ddlPassType.focus();
                    return false;
                    }
                    //Fin No
                    if (form.txtFin.value == "") {
                    alert( "FIN No cannot be left blank." );
                    form.txtFin.focus();
                    return false ;
                    }
                }
            }
        }
    }
    //Email
    if (form.txtEmail.value == "") {
    alert( "Email cannot be left blank." );
    form.txtEmail.focus();
    return false ;
    }
    else {
      var Email = form.txtEmail.value;
      if(checkemail(Email)== false) {
        alert( "Invalid Email Format." );
        form.txtEmail.value = "";
        form.txtEmail.focus();
        return false ;
      }  
    }
    //Mobile No
    if (form.txtHPhone.value == "") {
    alert( "Mobile no cannot be left blank." );
    form.txtHPhone.focus();
    return false ;
    }
    //Position Applied
    if (form.txtApplied.value == "") {
    alert( "Position applied for cannot be left blank." );
    form.txtApplied.focus();
    return false ;
    }
    //Current Position
    if (form.txtPosition.value == "") {
    alert( "Current position cannot be left blank." );
    form.txtPosition.focus();
    return false ;
    }
    //Current Employer
    if (form.txtEmployer.value == "") {
    alert( "Current employer cannot be left blank." );
    form.txtEmployer.focus();
    return false ;
    }
    //Notice Period
    if (form.txtNoticePeriod.value == "") {
    alert( "Notice period cannot be left blank." );
    form.txtNoticePeriod.focus();
    return false ;
    }
    else {
        if (chekcnumeric(form.txtNoticePeriod.value)== false){
                alert("Please enter numeric value for Notice Period.");
                form.txtNoticePeriod.value = "";
                form.txtNoticePeriod.focus();
                return false;
        }
    }
    //Last Drawn Salary
    if (form.txtLSalary.value != "") {
        //Is Numeric
        if (chekcnumeric(form.txtLSalary.value)== false){
            alert("Please enter numeric value for Last Drawn Salary.");
                form.txtLSalary.value = "";
                form.txtLSalary.focus();
                return false;
        }
//        else {
//            //Last Drawn Currency
//            if (form.ddlLCurrency.selectedIndex == 0) {
//                alert("Please select Currency for Last Drawn Salary");
//                form.ddlLCurrency.focus();
//                return false;
//            }
//        }
    }
    //Expected Salary
    if (form.txtESalary.value != "") {
        //Is Numeric
        if (chekcnumeric(form.txtESalary.value)== false){
            alert("Please enter numeric value for Expected Salary.");
                form.txtESalary.value = "";
                form.txtESalary.focus();
                return false;
        }
//        else {
//            //Expected Currency
//            if (form.ddlECurrency.selectedIndex == 0) {
//            alert("Please select Currency for Expected Salary");
//            form.ddlECurrency.focus();
//            return false;
//            }
//        }
    }
	else{
    	alert( "Expected salary cannot be left blank." );
    	form.txtESalary.focus();
    	return false ;
	}
    //File Upload
    if (form.fileUpload.value == "") {
    alert( "Please upload your resume in .doc, .docx or .pdf format." );
    form.fileUpload.focus();
    return false ;
    }
    return true ;
   }

//Email Format Validation
function checkemail(strEmail){
    var str = strEmail;
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if (filter.test(str))
        return true;
    else
        return false;
}

//Date Format Validation
function checkdate(strDate){
    var str = strDate;
    var filter = /^\d{1,2}\/\d{1,2}\/\d{4}$/; 
    if (filter.test(str))
        return true;
    else
        return false;
}

//Numeric Validation
function chekcnumeric(strNo){
   var filter = "0123456789.-";
   var strChar;
   var blnResult = true;

   for (i = 0; i < strNo.length && blnResult == true; i++)
      {
      strChar = strNo.charAt(i);
      if (filter.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

//Limit Text Length
function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}

//validate upload file format
function validateFileExtension(fld) {
	if(!/(\.doc|\.docx|\.pdf)$/i.test(fld.value)) {
		alert("Please upload .doc, .docx or .pdf format file only.");
		fld.focus();
		return false;
	}
	return true;
}

//show/hide ddlResident
    function ChangeDropdowns(value){
    document.getElementById("SgCitizen").style.display = 'none';
    document.getElementById("SPR").style.display = 'none';
    document.getElementById("NonSPR").style.display = 'none';
    document.getElementById("ddlResident").selectedIndex = 0;
    if (value=="Singapore"){
    document.getElementById("ddlResident").style.display="block";
    }
    else{
    document.getElementById("ddlResident").style.display="none";
    }
    }
    //show/hide tbody
    function display(obj,id1,id2,id3) {
    txt = obj.options[obj.selectedIndex].value;
    document.getElementById(id1).style.display = 'none';
    document.getElementById(id2).style.display = 'none';
    document.getElementById(id3).style.display = 'none';
    if ( txt==id1 ) {
    document.getElementById(id1).style.display = 'block';
    }
    if ( txt==id2 ) {
    document.getElementById(id2).style.display = 'block';
    }
    if ( txt==id3 ) {
    document.getElementById(id3).style.display = 'block';
    }
    }    