// Validation methods and behaviour (c) VCard Pro webmasters
function ChkYear(aValue){
 // te buus veel arii gada perioda validaacija 1920...2001
 return ! isNaN(aValue);
}
//----------------------------------------------------------------------------

function ValidPersInfo() { //v2.0
  var msgTxt = "";
  var inpPointer;
//  var ind = 0;
//  var elem ;
  if(document.fmApply.p_firstname.value ==""){
    inpPointer = document.fmApply.p_firstname;
	msgTxt=  " - First name\n"; 
  }
  if(document.fmApply.p_surname.value ==""){
 	 if (! inpPointer ) inpPointer = document.fmApply.p_surname;			   
     msgTxt= msgTxt + " - Last name\n";
  }
  if(document.fmApply.p_byear.value ==""){
 	 if (! inpPointer ) inpPointer = document.fmApply.p_byear;
     msgTxt= msgTxt + " - Year of birth\n";
   } else  
      if ( !ChkYear(document.fmApply.p_byear.value) ){
   		   if (! inpPointer ) inpPointer = document.fmApply.p_byear;
	       msgTxt= msgTxt + " - Year of birth should contain numbers\n";
	  }

  if(document.fmApply.p_residence.value ==""){
 	 if (! inpPointer ) inpPointer = document.fmApply.p_residence;
     msgTxt= msgTxt + " - Address\n";
   }
  if(document.fmApply.p_city.value ==""){
 	 if (! inpPointer ) inpPointer = document.fmApply.p_city;
     msgTxt= msgTxt + " - City\n";
   }
//  if(document.fmApply.p_country.value ==""){
// 	 if (! inpPointer ) inpPointer = document.fmApply.p_country;
//     msgTxt= msgTxt + " - Country\n";
//   }

//  ind  = document.fmApply.p_country.selectedIndex;
//  elem = document.fmApply.p_country.item(ind,0);
//alert(document.fmApply.p_country.length);

//  elem = document.fmApply.p_country.item(0);
//alert(document.fmApply.p_country.selectedIndex);
  if(document.fmApply.p_country.options[document.fmApply.p_country.selectedIndex].value ==""){
//  if(elem.value ==""){
 	 if (! inpPointer ) inpPointer = document.fmApply.p_country;
     msgTxt= msgTxt + " - Country\n";
   }
  if(document.fmApply.p_postcode.value ==""){
 	 if (! inpPointer ) inpPointer = document.fmApply.p_postcode;
     msgTxt= msgTxt + " - Post code\n";
   }
  if(document.fmApply.p_idcode.value ==""){
 	 if (! inpPointer ) inpPointer = document.fmApply.p_idcode;
     msgTxt= msgTxt + " - Personal ID code\n";
   }
  if(inpPointer) {
    inpPointer.focus();
    alert("PERSONAL INFORMATION DETAILS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }
  return true; 
}//eof-ValidPersInfo()
//---------------------------------------------------

function CheckPhone(sender){
  var tel = sender.value;

if (tel != "") {
for (var i = 0; i < tel.length; i++) {
	var ch = tel.substring(i, i + 1)
if ((ch < "0" || "9" < ch) && ch != ' ' && ch != "+" && ch != ")" && ch != "(") {
	alert("\n Telephone or FAX nummber is incorrect.\n\nAllowed are only the following symbols: +( ) and space");
	sender.focus();
	return false;
}
 if (tel.length<7) {
    alert("Telephone or FAX nummber should contain at least 7 symbols!");
    sender.focus();
    return false;
		}
	}
 }
 return true;
}//eof-CheckPhone
//-------------------------------------------

function Check_E_mail(sender){
 var emailStr = sender.value;
if (emailStr != "") {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert("E-mail address is incorrect!\nMake sure you put the @ and . simbols.");
	sender.focus();
	return false;
}
	var user=matchArray[1]
	var domain=matchArray[2]

if (user.match(userPat)==null) {
    alert("The E-mail's user name is incorrect!")
	sender.focus();
    return false
}

	var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("The E-mail's IP address is incorrect!")
	        sender.focus();
    		return false
	    }
    }
  //  return true
}

	var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The E-mail's Domain name is incorrect!")
	sender.focus();
    return false
}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
	alert("The E-mail's First level domain name should end with 2 or 3 symbols.\n For example, *.uk or *.com")
	sender.focus();
   return false
}

if (len<2) {
	var errStr="The E-mail's Address doesn't contain the domain name!"
	alert(errStr)
	sender.focus();
	return false
	}
}
// e-mail end
// else
    return true;
}//eof-Check_E_mail
//----------------------------------------------------------------------------

function ValidContactInfo_Personal(){
  if (document.fmApply.c_info_type.checked) {
    SetTheSameContactInfo(document.fmApply.c_info_type);
  }
  return ValidContactInfo();
}//eof-ValidContactInfo_Personal
//----------------------------------------------------------------------------

function ValidContactInfo() { //v2.0
  var msgTxt = "";
  var inpPointer;

  if(document.fmApply.c_recptname.value ==""){
  	 inpPointer = document.fmApply.c_recptname;
     msgTxt = " - Recipient name\n";
  }
  if(document.fmApply.c_addr.value ==""){
  	 if (! inpPointer ) inpPointer = document.fmApply.c_addr;
     msgTxt= msgTxt + " - Address\n";
  }
  if(document.fmApply.c_city.value ==""){
  	 if (! inpPointer ) inpPointer = document.fmApply.c_city;
     msgTxt= msgTxt + " - City\n";
  }
  if(document.fmApply.c_country.options[document.fmApply.c_country.selectedIndex].value ==""){
  	 if (! inpPointer ) inpPointer = document.fmApply.c_country;
     msgTxt= msgTxt + " - Country\n";
  }
  if(document.fmApply.c_postcode.value ==""){
  	 if (! inpPointer ) inpPointer = document.fmApply.c_postcode;
     msgTxt= msgTxt + " - Postcode\n";
  }
  if(document.fmApply.c_fax.value !=""){
    if( ! CheckPhone(document.fmApply.c_fax) ) return false ;
  };

  if(document.fmApply.c_phone.value ==""){
  	 if (! inpPointer ) inpPointer = document.fmApply.c_phone;
     msgTxt= msgTxt + " - Phone number\n";
  }
  else 
    if( ! CheckPhone(document.fmApply.c_phone) ){
	   return false ;
   };

  if(document.fmApply.c_email.value ==""){
  	 if (! inpPointer ) inpPointer = document.fmApply.c_email;
     msgTxt= msgTxt + " - e-mail address\n";
  } else 
     if (! Check_E_mail(document.fmApply.c_email)) return false;

  if(inpPointer) {
    inpPointer.focus();
    alert("CONTACT INFORMATION DETAILS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }
  return true; 
}//eof-ValidContactInfo()
//---------------------------------------------------

function ValidPersIDInfo() { //v2.0
  var msgTxt = "";
  var inpPointer;
  if(document.fmApply.id_doc_no.value ==""){
  	 inpPointer = document.fmApply.id_doc_no;
     document.fmApply.id_doc_no.focus();
     msgTxt=  " - ID card or passport No\n";
  }
  if(document.fmApply.id_year_i.value ==""){
  	 if (! inpPointer )  inpPointer = document.fmApply.id_year_i;
     msgTxt= msgTxt + " - ID issue year\n";
  } else  
      if ( !ChkYear(document.fmApply.id_year_i.value)){
      	   if (! inpPointer )  inpPointer = document.fmApply.id_year_i;
	       msgTxt= msgTxt + " - ID issue year should contain nubers\n";
	  }

  if(document.fmApply.id_year_x.value ==""){
  	 if (! inpPointer )  inpPointer = document.fmApply.id_year_x;
     msgTxt= msgTxt + " - ID expire year\n";
  }else 
     if ( !ChkYear(document.fmApply.id_year_x.value)){
    	   if (! inpPointer )  inpPointer = document.fmApply.id_year_x;
	       msgTxt= msgTxt + " - ID expire year should contain numbers\n";
	  }

  if(document.fmApply.id_city.value ==""){
  	 if (! inpPointer )  inpPointer = document.fmApply.id_city;
     msgTxt= msgTxt + " - ID issue city\n";
  }
  if(document.fmApply.id_country.options[document.fmApply.id_country.selectedIndex].value ==""){
  	 if (! inpPointer )  inpPointer = document.fmApply.id_country;
     msgTxt= msgTxt + " - ID issue country\n";
  }
  if(inpPointer) {
    inpPointer.focus();
    alert("PERSONAL ID INFORMATION DETAILS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }
  return true; 
}//eof-ValidPersIDInfo()
//---------------------------------------------------


function ValidCorporateInfo() { //v2.0
  var msgTxt = "";
  var inpPointer;
  if(document.fmApply.m_companyname.value ==""){
  	 inpPointer = document.fmApply.m_companyname;
     document.fmApply.m_companyname.focus();
     msgTxt=  " - Company name\n";
  }
  if(document.fmApply.m_addr.value ==""){
  	 if (! inpPointer ) inpPointer =	document.fmApply.m_addr;
     msgTxt= msgTxt + " - Address\n";
  }
  if(document.fmApply.m_city.value ==""){
  	 if (! inpPointer ) inpPointer =	document.fmApply.m_city;
     msgTxt= msgTxt + " - City\n";
  }
  if(document.fmApply.m_country.options[document.fmApply.m_country.selectedIndex].value ==""){
     if (! inpPointer ) inpPointer =	document.fmApply.m_country;
     msgTxt= msgTxt + " - Country\n";
  }
  if(document.fmApply.m_postcode.value ==""){
     if (! inpPointer ) inpPointer =	document.fmApply.m_postcode;
     msgTxt= msgTxt + " - Post code\n";
  }
  if(document.fmApply.m_reg_no.value ==""){
     if (! inpPointer ) inpPointer =	document.fmApply.m_reg_no;
     msgTxt= msgTxt + " - Registration number\n";
   }
  if(document.fmApply.m_regyear.value ==""){
     if (! inpPointer ) inpPointer =	document.fmApply.m_regyear;
     msgTxt= msgTxt + " - Year of registration\n";
  }else  
      if ( !ChkYear(document.fmApply.m_regyear.value)){
			if (! inpPointer ) inpPointer =	document.fmApply.m_regyear;
	        msgTxt= msgTxt + " - Year of registration should contain numbers\n";
	  }
  if(inpPointer) {
    inpPointer.focus();
    alert("CORPORATE INFORMATION DETAILS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }
  return true; 
}
//----------------------------------------------------------------------------


function ValidReprPersInfo() { //v2.0
  // check for cardholder infotype
  var msgTxt = "";
  var inpPointer;
  
  inpPointer = document.fmApply.elements.item('u_info_type');
  if (inpPointer)
  {
	  if (inpPointer.checked) {
	    SetTheSameCardholderInfo(inpPointer);
	  };
	inpPointer = null;// clear functional value
  }
  
  if(document.fmApply.r_position.value ==""){
	 inpPointer = document.fmApply.r_position;
 	 msgTxt=  " - position name\n"; 
  }
  if(document.fmApply.r_firstname.value ==""){
	 if(! inpPointer) inpPointer = document.fmApply.r_firstname;
 	 msgTxt= msgTxt + " - First name\n"; 
  }
  if(document.fmApply.r_surname.value ==""){
	 if(! inpPointer) inpPointer = document.fmApply.r_surname;
     msgTxt= msgTxt + " - Last name\n";
  }
  if(document.fmApply.r_byear.value ==""){
	 if(! inpPointer) inpPointer = document.fmApply.r_byear;
     msgTxt= msgTxt + " - Year of birth\n";
  } else  
      if ( !ChkYear(document.fmApply.r_byear.value)){
   			if(! inpPointer) inpPointer = document.fmApply.r_byear;
	       msgTxt= msgTxt + " - Year of birth should contain numbers\n";
	  }
  if(document.fmApply.r_idcode.value ==""){
 	 if (! inpPointer ) inpPointer = document.fmApply.r_idcode;
     msgTxt= msgTxt + " - Personal ID code\n";
   }
  if(inpPointer) {
    inpPointer.focus();
    alert("CORPORATE REPRESENTATIVE INFORMATION DETAILS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }
  return true; 
}
//----------------------------------------------------------------------------

function ValidCA_Options(){
  var msgTxt = "";
  var inpPointer;

  if(document.fmApply.ac_card_count.value ==""){
  	 inpPointer = document.fmApply.ac_card_count;
     msgTxt= " - Number of cards to order\n";
  } else if ( isNaN(document.fmApply.ac_card_count.value)){
     	  inpPointer = document.fmApply.ac_card_count;
          msgTxt= " - Number of cards should contain numbers\n";
         }

  if (document.fmApply.ac_cname.checked){		 
   if(document.fmApply.ac_companyname.value ==""){
    	 if (! inpPointer )  inpPointer = document.fmApply.ac_companyname;
      msgTxt= msgTxt + " - Card embossement\n";
    }
  }else document.fmApply.ac_companyname.value ="";

  if(inpPointer) {
    inpPointer.focus();
    alert("CARD ACCOUNT OPTIONS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }
  return true; 
}//eof-ValidCA_Options()
//----------------------------------------------------------------------------

function ValidCA_Options_DC(){
  var msgTxt = "";
  var inpPointer;
//  alert(document.fmApply.product_id.options[document.fmApply.product_id.selectedIndex].value);
   if(document.fmApply.product_id.options[document.fmApply.product_id.selectedIndex].value ==""){
      inpPointer = document.fmApply.product_id;
      msgTxt= msgTxt + " - You have to choose some product\n";
    }

  if (document.fmApply.ac_cname.checked){		 
   if(document.fmApply.ac_companyname.value ==""){
    	 if (! inpPointer )  inpPointer = document.fmApply.ac_companyname;
      msgTxt= msgTxt + " - Company name embossement\n";
    }
  }else document.fmApply.ac_companyname.value ="";

   if(document.fmApply.ac_act_username.value ==""){
    	 if (! inpPointer )  inpPointer = document.fmApply.ac_act_username;
      msgTxt= msgTxt + " - Card user name embossement\n";
    }

  if(inpPointer) {
    inpPointer.focus();
    alert("CARD ACCOUNT OPTIONS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }
  return true; 
}//eof-ValidCA_Options_DC
//----------------------------------------------------------------------------
  

function SetTheSameContactInfo(aTheSame){
 if (aTheSame.checked){
  document.fmApply.c_title.selectedIndex = document.fmApply.p_title.selectedIndex;
  document.fmApply.c_recptname.value = document.fmApply.p_firstname.value+" "+document.fmApply.p_midname.value+" "+document.fmApply.p_surname.value;
  document.fmApply.c_addr.value = document.fmApply.p_residence.value;
  document.fmApply.c_city.value = document.fmApply.p_city.value;
  document.fmApply.c_state.value = document.fmApply.p_state.value;
  document.fmApply.c_country.selectedIndex = document.fmApply.p_country.selectedIndex;
  document.fmApply.c_postcode.value = document.fmApply.p_postcode.value;
 }
 else{
  document.fmApply.c_title.selectedIndex = 0;
  document.fmApply.c_recptname.value = "";
  document.fmApply.c_addr.value = "";
  document.fmApply.c_city.value = "";
  document.fmApply.c_state.value = "";
  document.fmApply.c_country.selectedIndex = 0;
  document.fmApply.c_postcode.value = "";
 }

  SetEnabledPersonalSameContact(aTheSame.checked);
}//eof-SetTheSameContactInfo
//------------------------------------------------------------------------------------------------

function SetEnabledPersonalSameContact(aEnabled){
	document.fmApply.c_title.disabled     = aEnabled;
	document.fmApply.c_addr.disabled      = aEnabled;
	document.fmApply.c_recptname.disabled = aEnabled;
	document.fmApply.c_city.disabled      = aEnabled;
	document.fmApply.c_state.disabled     = aEnabled;
	document.fmApply.c_country.disabled   = aEnabled;
	document.fmApply.c_postcode.disabled  = aEnabled;
}//eof-SetEnabledPersonalSameContact
//------------------------------------------------------------------------------------------------

function SetTheSameCardholderInfo(aTheSame){
 if (aTheSame.checked)
 {
  //CardHolder information
  document.fmApply.u_firstname.value	= document.fmApply.r_firstname.value;
  document.fmApply.u_midname.value		= document.fmApply.r_midname.value ;
  document.fmApply.u_surname.value		= document.fmApply.r_surname.value ;
  document.fmApply.u_bday.selectedIndex = document.fmApply.r_bdate.selectedIndex;
  document.fmApply.u_bmonth.selectedIndex = document.fmApply.r_bmonth.selectedIndex;
  document.fmApply.u_byear.value		= document.fmApply.r_byear.value; 
  document.fmApply.u_idcode.value		= document.fmApply.r_idcode.value; 
  //CardHolder ID Card information
	document.fmApply.uid_doc_no.value = document.fmApply.id_doc_no.value;
	document.fmApply.uid_date_i.selectedIndex = document.fmApply.id_date_i.selectedIndex;
	document.fmApply.uid_month_i.selectedIndex = document.fmApply.id_month_i.selectedIndex;
	document.fmApply.uid_year_i.value = document.fmApply.id_year_i.value;
	document.fmApply.uid_date_x.selectedIndex = document.fmApply.id_date_x.selectedIndex;
	document.fmApply.uid_month_x.selectedIndex = document.fmApply.id_month_x.selectedIndex;
	document.fmApply.uid_year_x.value = document.fmApply.id_year_x.value;
	document.fmApply.uid_city.value = document.fmApply.id_city.value;
	document.fmApply.uid_country.selectedIndex = document.fmApply.id_country.selectedIndex;
 } 
 else {
  //CardHolder information
  document.fmApply.u_firstname.value	= "";
  document.fmApply.u_midname.value		= "";
  document.fmApply.u_surname.value		= "";
  document.fmApply.u_bday.selectedIndex   = 0;
  document.fmApply.u_bmonth.selectedIndex = 0;
  document.fmApply.u_byear.value		= ""; 
  document.fmApply.u_idcode.value		= ""; 
  //CardHolder ID Card information
	document.fmApply.uid_doc_no.value = "";
	document.fmApply.uid_date_i.selectedIndex = 0;
	document.fmApply.uid_month_i.selectedIndex = 0;
	document.fmApply.uid_year_i.value = "";
	document.fmApply.uid_date_x.selectedIndex = 0;
	document.fmApply.uid_month_x.selectedIndex = 0;
	document.fmApply.uid_year_x.value = "";
	document.fmApply.uid_city.value = "";
	document.fmApply.uid_country.selectedIndex = 0;
 };// else

 SetEnabledCardholderInfo(aTheSame.checked);
}//eof-SetTheSameCardholderInfo
//------------------------------------------------------------------------------------------------

function SetEnabledCardholderInfo(aEnabled){
//CardHolder information
  document.fmApply.u_firstname.disabled = aEnabled;
  document.fmApply.u_midname.disabled   = aEnabled;
  document.fmApply.u_surname.disabled   = aEnabled;
  document.fmApply.u_bday.disabled      = aEnabled;
  document.fmApply.u_bmonth.disabled    = aEnabled;
  document.fmApply.u_byear.disabled     = aEnabled;
  document.fmApply.u_idcode.disabled    = aEnabled;
//CardHolder ID Card information
  document.fmApply.uid_doc_no.disabled	= aEnabled;
  document.fmApply.uid_date_i.disabled	= aEnabled;
  document.fmApply.uid_month_i.disabled	= aEnabled;
  document.fmApply.uid_year_i.disabled	 = aEnabled;
  document.fmApply.uid_date_x.disabled	= aEnabled;
  document.fmApply.uid_month_x.disabled	= aEnabled;
  document.fmApply.uid_year_x.disabled	= aEnabled;
  document.fmApply.uid_city.disabled	= aEnabled;
  document.fmApply.uid_country.disabled	= aEnabled;

}//eof-SetEnabledCardholderInfo
//------------------------------------------------------------------------------------------------

function SetBrowserDifferences_Personal(){
 if (!((navigator.appName=="Netscape")&& (parseInt(navigator.appVersion)==4))) {
   document.fmApply.p_firstname.size = 20;
   document.fmApply.c_recptname.size = 20;
   document.fmApply.comments.cols = 54;
 }
  SetEnabledPersonalSameContact(document.fmApply.c_info_type.checked);
}//eof-SetBrowserDifferences
//------------------------------------------------------------------------------------------------

var CONF_INCORPORATION_TYPE = 'ready';

function ValidateConfPackage(){
  var msgTxt = "";
  var inpPointer;

 if(CONF_INCORPORATION_TYPE == 'ready'){
 if(document.fmApply.ci_jurisdiction_ready.options[document.fmApply.ci_jurisdiction_ready.selectedIndex].value == ''){
		 inpPointer = document.fmApply.ci_jurisdiction_ready;
	 	 msgTxt=  " - Jurisdiction for ready made company\n"; 
		}
 }else {
	  if(document.fmApply.ci_jurisdiction_custom.options[document.fmApply.ci_jurisdiction_custom.selectedIndex].value == ''){
		 inpPointer = document.fmApply.ci_jurisdiction_custom;
	 	 msgTxt=  " - Jurisdiction for custom incorporation\n"; 
		}
	 }//else

  if(inpPointer) {
    inpPointer.focus();
    alert("COMPANY INCORPORATION DETAILS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }
/*
 if((document.fmApply.ac_type.value =="")||(document.fmApply.ac_type.value =="none" )){
	 if(! inpPointer) inpPointer = document.fmApply.ac_type;
 	 msgTxt= msgTxt + " - Bank account type\n"; 
  }

if(document.fmApply.crd_type.value ==""){
	 if(! inpPointer) inpPointer = document.fmApply.crd_type;
 	 msgTxt= msgTxt +  " - Account card type\n"; 
  }
*/

if(document.fmApply.r_firstname.value == ""){
	 if(! inpPointer) inpPointer = document.fmApply.r_firstname;
 	 msgTxt= msgTxt +  " - First name\n"; 
  }
  if(document.fmApply.r_surname.value ==""){
	 if(! inpPointer) inpPointer = document.fmApply.r_surname;
     msgTxt= msgTxt + " - Last name\n";
  }


  if(inpPointer) {
    inpPointer.focus();
    alert("PERSON INFORMATION DETAILS\n\nThe following information is missing or incorrect:\n\n"+msgTxt); 
    return false;
  }

 if (! ValidContactInfo()) return false;
}//eof-ValidateConfPackage
//------------------------------------------------------------------------------------------------

