// JavaScript Document

function FormValidation(formName)
{
	if(formName=="prevIDForm")
	{
		if(document.prevIDForm.ID.value=="")
		{
			alert("Please enter your ID if you previously filled in the form");
			return false;
		}
		
		return true;
	}
	
	if(formName=="dirsecp1")
	{
		var form=document.dirsecp1;
		if(form.first_name.value =="")
		{
			alert("Please enter a value for the \"First Name\" field.")
			form.first_name.focus()
			return(false)	
		}
	
		if(form.last_name.value =="")
		{
			alert("Please enter a value for the \"Last Name\" field.")
			form.last_name.focus()
			return(false)	
		}
		
		if(form.email.value == "")
  		{
    		alert("Please enter a value for the \"Email\" field.");
    		form.email.focus();
    		return (false);
	
  		}

  		if(validate_email(form.email) == false) 
		{
			alert("Please enter a valid Email id.");
        	form.email.focus();
        	return (false);
  		}
		
		if(form.emailcheck.value == "true")
  		{
    		alert("Please enter valid email Id");
   			form.emailcheck.focus();
    		return (false);
  		}  
	
		if(form.home_street.value =="")
		{
			alert("Please enter a value for the \"Home Street\" field.")
			form.home_street.focus()
			return(false)	
		}
	
		if(form.home_city.value =="")
		{
			alert("Please enter a value for the \"Home City\" field.")
			form.home_city.focus()
			return(false)	
		}
		
		if(form.fmcountry.selectedIndex < 0)
  		{
    		alert("Please select one of the \"Country\" options.");
    		form.fmcountry.focus();
    		return (false);
  		}

  		if(form.fmcountry.selectedIndex == 0)
  		{
    		alert("The first \"Country\" option is not a valid selection.  Please choose one of the other options.");
    		form.fmcountry.focus();
    		return (false);
  		}

  		
		//alert(form.fmstate1.value);
		if(form.fmstate1.value =="" || form.fmstate1.value ==0 )
  		{
    		alert("Please enter a value or choose one of the given options for \"State\".");
    		form.fmstate1.focus();
   			return (false);
  		}	
			
		if(form.home_zip.value == "")
  		{
   			alert("Please enter a value for the \"Zip\" field.");
    		form.home_zip.focus();
    		return (false);
  		}
		
		/*if(form.home_zip.value != "" && !isInteger(form.home_zip.value))
  		{
   			alert("Please enter proper  \"Zip\" code");
			form.home_zip.value="";
			form.home_zip.focus();
    		return (false);
  		}

  		if(form.home_zip.value.length < 5)
  		{
    		
			alert("Please enter at least 5 digits in the \"Zip\" field. If your Zip code has less than 5 digits, add one or more zeros at the begining to complete five digits");
    		form.home_zip.focus();
    		return (false);
  		}
		
		if(form.home_zip.value.length > 10)
  		{
    		alert("Please enter at most 10 characters in the \"Zip\" field.");
   			form.home_zip.focus();
    		return (false);
  		} */
		
		/*if(form.home_US_phone.value == "") 
  		{
			alert("Please enter a phone number");
        	form.home_US_phone.focus();
        	return (false);
  		}
		
		
 		if(form.home_US_phone1.value!="")   			
   			form.home_US_phone.value=form.home_US_phone.value+"-"+form.home_US_phone1.value;
  		 else
	   		form.home_US_phone.value=form.home_US_phone.value;*/
		
		return true;
	}
	
//-------------------- Page 2----------------------------
	if(formName=="dirsecp2")
	{
		var form=document.dirsecp2;
		/*if(form.curpos.value =="")
		{
			alert("Please enter a value for the \"Current Position\" field.")
			form.curpos.focus()
			return(false)	
		}*/
	
		if(form.title.value == "")
		{
			alert("Please enter a value for the \"Title\" field.")
			form.title.focus()
			return(false)	
		}
		
		if(form.dstartdate.value == "")
  		{
    		alert("Please enter a value for the \"Date Started\" field.");
    		form.dstartdate.focus();
    		return (false);
	
  		}  		
	
		if(form.organaisation.value == "")
		{
			alert("Please enter a value for the \"School\\Organisation\\Practice\" field.")
			form.organaisation.focus()
			return(false)	
		}
	
		
		if(form.workaddress.value =="")
		{
			alert("Please enter a value for the \"Work Address\" field.")
			form.workaddress.focus()
			return(false)	
		}
		
		
		if(form.workcity.value =="")
		{
			alert("Please enter a value for the \"Work City\" field.")
			form.workcity.focus()
			return(false)	
		}
		
		if(form.workstate.value <= 0 || form.workstate.value=="")
  		{
    		alert("Please select one of the \"Work State\" options.");
    		form.workstate.focus();
    		return (false);
  		}

  		if(form.workcountry.value <= 0)
  		{
    		alert("Please select one of the \"Work Country\" options.");
    		form.workcountry.focus();
    		return (false);
  		}
  		
			
		if(form.workzip.value == "")
  		{
   			alert("Please enter a value for the \"Work Zip\" field. Enter five zeros if your address doesn't have Zip code");
    		form.workzip.focus();
    		return (false);
  		}
		
		/*if(form.workzip.value != "" && !isInteger(form.workzip.value))
  		{
   			alert("Please enter proper  \"Work Zip\" code");
			form.workzip.value="";
			form.workzip.focus();
    		return (false);
  		}

  		if(form.workzip.value.length < 5)
  		{
    		
			alert("Please enter at least 5 digits in the \"Work Zip\" field. If your Zip code has less than 5 digits, add one or more zeros at the begining to complete five digits");
    		form.workzip.focus();
    		return (false);
  		}
		
		if(form.workzip.value.length > 10)
  		{
    		alert("Please enter at most 10 characters in the \"Work Zip\" field.");
   			form.workzip.focus();
    		return (false);
  		} */
		
		if(form.work_phone.value == "") 
  		{
			alert("Please enter a valid phone number");
        	form.work_phone.focus();
        	return (false);
  		}
		
		/*if(validate_phonenum('dirsecp2',2) == false) 
  		{
			alert("Please enter a valid phone number");
        	form.work_phone2.focus();
        	return (false);
  		}
		
		
 		if(form.work_phone1.value!="")   			
   			form.work_phone.value=form.work_phone.value+"-"+form.work_phone1.value;
  		 else
	   		form.work_phone.value=form.work_phone.value;
		
		if(form.work_phone3.value!="")   			
   			form.work_phone2.value=form.work_phone2.value+"-"+form.work_phone3.value;
  		 else
	   		form.work_phone2.value=form.work_phone2.value;*/
		
		return true;
	}
	
	//------- PAGE 3 ----------------------------
	
	if(formName=="dirsecp3")
	{
		var form=document.dirsecp3;		
		return true;
	}
	
	//-------------- PAGE 4 -----------------------
	if(formName=="dirsecp4")
	{
		var form=document.dirsecp4;		
		return true;
	}
	//--------------- PAGE 5 ---------------------
	if(formName=="dirsecp5")
	{
		var form=document.dirsecp5;		
		return true;
	}
	//---------------- PAGE 6 -----------------------
	
	if(formName=="dirsecp6")
	{
		var form=document.dirsecp6;		
		
		if(form.resume.value =="")
		{
			alert("Please enter a value for the \"Resume\" field.")
			form.resume.focus()
			return(false)	
		}
	
		if(form.liccred.value == "")
		{
			alert("Please enter a value for the \"License/Credentials\" field.")
			form.liccred.focus()
			return(false)	
		}
		
		if(form.picture.value == "")
  		{
    		alert("Please enter a value for the \"Picture\" field.");
    		form.picture.focus();
    		return (false);
	
  		}  	
		
		if(form.vignette.value == "" && form.program5.value=="DIR C")
  		{
    		alert("Please enter a value for the \"Vignette\" field.");
    		form.vignette.focus();
    		return (false);
	
  		}
	
		return true;
	}
	
	//--------------- PAGE 7 ---------------------
	if(formName=="dirsecp7")
	{
		var form=document.dirsecp7;		
		return true;
	}
	
	//--------------- PAGE 8 ---------------------
	if(formName=="dirsecp8")
	{
		var form=document.dirsecp8;		
		return true;
	}
	
	//--------------- PAGE 9 ---------------------
	if(formName=="dirsecp9")
	{
		var form=document.dirsecp9;		
		return true;
	}
	
	//--------------- PAGE 10 ---------------------
	if(formName=="dirsecp10")
	{
		var form=document.dirsecp10;		
		return true;
	}
}


/* Validate Email address */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}


function validate_email(eml){
	//var emailID=form.user_email;
	var emailID=eml;
	if ((emailID.value==null)||(emailID.value=="")){
		return false
	}
	if (echeck(emailID.value)==false){
		return false
	}
	return true
 }
 
 //====================================== end of email validate =================================
 
 
 
 //=============== phone validation=====================
 
 var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function formatPhone(num)
{ 
  var _return=false;
  /*
   * 7181238748 to 1(718)123-8748
   */ 

  if(num.length != 10)
  { 
    /* 
     * if user did not enter 10 digit phone number then simply print whatever user entered 
     */ 
	_return=_OUTPUT?num:false;
  } 
  else
  { 
    /* formating phone number here */ 
	_return="(";
	var ini = num.substring(0,3);
	_return+=ini+")";
	var st = num.substring(3,6);
	_return+=st+"-";
	var end = num.substring(6,10);
	_return+=end;
  }
  return _return; 
} 


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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone,formName,Nums)
{
s=stripCharsInBag(strPhone,validWorldPhoneChars);
ret = isInteger(s) && s.length >= minDigitsInIPhoneNumber;

if (ret == true) {
	if(formName=="dirsecp1")
		document.dirsecp1.home_US_phone.value = formatPhone(s);
	
	if(formName=="dirsecp2")
	{
		if(Nums==1)
			document.dirsecp2.work_phone.value=formatPhone(s);
		if(Nums==2)
			document.dirsecp2.work_phone2.value=formatPhone(s);
	}
} 
return (ret);
}

function validate_phonenum(formName,Nums){
	if(formName=="dirsecp1")
		var Phone=document.dirsecp1.home_US_phone;
	
	if(formName=="dirsecp2")
	{
		if(Nums==1)
			var Phone=document.dirsecp2.work_phone;
		if(Nums==2)
			var Phone=document.dirsecp2.work_phone2;
	}

	//alert(Phone.value)
	if ((Phone.value.length < 4) || (Phone.value.length > 14)){
		return false
	}

	if ((Phone.value==null)||(Phone.value=="")){
		return false
	}
	if (checkInternationalPhone(Phone.value,formName,Nums)==false){
		return false
	}
	return true
}


///================================= end of phone validation ==================================

//Inside Portal

function ChangeAmount(formName,objValue,objName)
{
	
	if(objName.checked)
	{
		formName.Total_Amount.value=Number(formName.Total_Amount.value)+Number(objValue);
	}
	else
	{
		formName.Total_Amount.value=Number(formName.Total_Amount.value)-Number(objValue);
	}
}

//Ends
