// JavaScript Document

/*
	Author: Mohammad Belal Hossain
	Date: 21-06-2009
	Task: expand or collase a particular section based on specified value
	parameters:
		theElementId = the element where user will select or write the value
		checkValue = the value that need to be matched
		extensionElementId = that will be hide or unhide
		displayValue = indicates what action have to take for checkValue
*/
function displayExtension(theElementId,checkValue,extensionElementId,displayValue){
	var elementVal = document.getElementById(theElementId).value;	
	if(displayValue == "block"){
		theDisplayValue = "block"; toggleValue = "none";
	}else {
		theDisplayValue = "none"; toggleValue = "block";
	}
	if(elementVal == checkValue){
		document.getElementById(extensionElementId).style.display = theDisplayValue;
	}else{
		document.getElementById(extensionElementId).style.display = toggleValue;
	}
}

/*
	Source: Internet
	Task: restrict a element to enter only certain values e.g. onKeyPress="return checkChar(this,'[0-9]',event);"
*/
function checkChar(oElement,filter,e){
	x=e;
	if( typeof( e.which ) == 'number' ) { e = e.which; } else if( typeof( e.keyCode ) == 'number'  ) { e = e.keyCode; } else if( typeof( e.charCode ) == 'number'  ) { e = e.charCode; }

	if (e!=8&&e!=0){
	e=x;
		if(oElement.onkeypress){
			if(document.all){
				var sKey=String.fromCharCode(event.keyCode);
				var re=new RegExp(filter);
					if(sKey!="\r" && !re.test(sKey))
						event.returnValue=false;
					event.keyCode=sKey.charCodeAt(0);
			}else{
				document.captureEvents(Event.ONKEYPRESS);
					var sKey=String.fromCharCode(e.which);
					var re=new RegExp(filter);
					if(sKey!="\r" &&  !re.test(sKey))return false;
			}
		}
	}
}		
function validateSectionOne(){
 // Section One (1)
	if(validateSelectBox('makeOfCarSelect', 0)){
	 if(validateSelectBox('modelOfCarSelect', 0)){
	  if(validateSelectBox('yearOfCarSelect', 0)){
	   if(validateSelectBox('transmissionTypeSelect', 0)){
	    if(validateSelectBox('fuelTypeSelect', 0)){
	     if(validateSelectBox('carSelect', 0)){
	
	      // Section One (2)
	      if(isNull('postCodeTxt','', 0)){
	       if(validateSelectBox('carParkedSelect', 0)){
	
	        // Section One (3)
	         if(validateSelectBox('carUsedSelect', 0)){
	          if(validateSelectBox('financeTypeSelect', 0)){
	            return true;
			  }
			 }
		   }
		  }
		 }
		}
	   }
	  }
	 }
	}
 	return false;
}

function validateSectionTwo(){
 // Section Two (1)
 if(validateSelectBox('coverTypeSelect', 1)){
  if(validateSelectBox('startDateSelect', 1)){

	// Section One (2)
	if(isNull('expectedDrivingKM','', 1)){
		if(checkRadioButton('noClaimBonusOpt',2,'noClaimBonusOptions', 1)){
			if(document.getElementById('noClaimBonusOpt1').checked == true){
				if(validateSelectBox('bonusEntitlementYearsSelect', 1)){
					if(document.getElementById('bonusEntitlementYearsSelect').options[document.getElementById('bonusEntitlementYearsSelect').selectedIndex].value == "5,1,60%"){
						if(validateSelectBox('ratingHeldYearsSelect', 1)){
						}else{return false;}
					}
				}else{return false;}
			}

			//Continuation
			if(validateSelectBox('coverOptionsSelect', 1)){
				return true;
			}
		}
	}
  }
 }
 return false;
}

function validateSectionThree(){
  var maxValidYearValue = parseInt(document.getElementById('currentYearValue').value)-18;
  var minValidYearValue = maxValidYearValue - 78;
  // SECTION ONE (1)
   if(checkRadioButton('anyMotorInsuranceClaimOpt',3,'anyMotorInsuranceClaimOptions', 2)){
	if(validateSelectBox('ratingDiscountSelect', 2)){
	   // Check depending field(s)
	     if(document.getElementById('ratingDiscountSelect').options[document.getElementById('ratingDiscountSelect').selectedIndex].value == 5){
			 if(checkRadioButton('protectNCDOpt',2,'protectNCDOptions', 2)){}else{return false;}
		 }

		 //Continue 1
		 if(checkRadioButton('anyFinesImposedOpt',2,'anyFinesImposedOpt_hlm', 2)){
			 if(document.getElementById('anyFinesImposedOpt1').checked == true){
				if(isNull('imposedFinesDesc', 'anyFinesImposedOptDesc', 2)){}else{return false;}
			 }

             // Continue 2
             if(checkRadioButton('drivingLicenceCancelledOpt',2,'drivingLicenceCancelledOpt_hlm', 2)){
			  if(document.getElementById('drivingLicenceCancelledOpt1').checked == true){
				if(isNull('drivingLicenceCancelledOptDesc', 'drivingLicenceCancelledOptDescSection', 2)){}else{return false;}
			  }
			 }// End of Continue 2

             // Continue 3
             if(checkRadioButton('ownerThisCarOpt',2,'ownerThisCarOptions', 2)){
			   if(isNull('bearedPercentageTxt', 'bearedPercentageOption', 2) && isValueInvalid('bearedPercentageTxt',1, 100, 'bearedPercentageOption', 2)){
				 if(checkRadioButton('ownsAnotherCarOpt',2,'ownsAnotherCarOptions', 2)){

                    // SECTION TWO (2)
					// Continue 4
                    if(checkRadioButton('anyOtherDriverOpt',2,'anyOtherDriverOptions', 2)){
					  // Depending field(s)
					  if(document.getElementById('anyOtherDriverOpt1').checked == true){
						if(isNull('odBirthDayTxt', 'odDobOptions', 2) && isValueInvalid('odBirthDayTxt',1, 31, 'odDobOptions', 2)){
						  if(isNull('odBirthMonthTxt', 'odDobOptions', 2) && isValueInvalid('odBirthMonthTxt',1, 12, 'odDobOptions', 2)){
							if(isNull('odBirthYearTxt', 'odDobOptions', 2) && isValueInvalid('odBirthYearTxt',minValidYearValue, maxValidYearValue, 'odDobOptions', 2)){
							  if(checkRadioButton('odGenderOpt',2,'odGenderOptions', 2)){
								if(isNull('odName', 'odNameSection', 2)){
								  if(checkRadioButton('odAnyFinesImposedOpt',2,'odAnyFinesImposedOpt_hlm', 2)){
								    if(document.getElementById('odAnyFinesImposedOpt1').checked == true){
									  if(isNull('odImposedFinesDesc', 'odAnyFinesImposedOptDesc', 2)){}else {return false;}
									}

                                    // Continue 5
									if(checkRadioButton('odDrivingLicenceCancelledOpt',2,'odDrivingLicenceCancelledOpt_hlm', 2)){
									  if(document.getElementById('odDrivingLicenceCancelledOpt1').checked == true){
										if(isNull('odDrivingLicenceCancelledOptDesc', 'odDrivingLicenceCancelledOptDescSection', 2)){}else {return false;}
									  }

                                      // Continue 6
									  if(isNull('odBearedPercentageTxt', 'odBearedPercentageOption', 2) && isValueInvalid('odBearedPercentageTxt',1, 100, 'odBearedPercentageOption', 2)){	
									    if(checkRadioButton('odAnyOtherDriverOpt',2,'odAnyOtherDriverOptions', 2)){
										  if(document.getElementById('odAnyOtherDriverOpt1').checked == true){

                                             // Continue 7
											 if(isNull('odExtBirthDayTxt', 'odExtDobOptions', 2) && isValueInvalid('odExtBirthDayTxt',1, 31, 'odExtDobOptions', 2)){
											   if(isNull('odExtBirthMonthTxt', 'odExtDobOptions', 2) && isValueInvalid('odExtBirthMonthTxt',1, 12, 'odExtDobOptions', 2)){
												 if(isNull('odExtBirthYearTxt', 'odExtDobOptions', 2) && isValueInvalid('odExtBirthYearTxt',minValidYearValue, maxValidYearValue, 'odExtDobOptions', 2)){
												   if(checkRadioButton('odExtGenderOpt',2,'odExtGenderOptions', 2)){
													  if(isNull('odExtName', 'odExtNameSection', 2)){
														 if(checkRadioButton('odExtAnyFinesImposedOpt',2,'odExtAnyFinesImposedOpt_hlm', 2)){

                                                           // Continue 8
														   if(document.getElementById('odExtAnyFinesImposedOpt1').checked == true){
															 if(isNull('odExtImposedFinesDesc', 'odExtAnyFinesImposedOptDesc', 2)){}else {return false;}
														   }// End of Continue 8

                                                           // Continue 9
														   if(checkRadioButton('odExtDrivingLicenceCancelledOpt',2,'odExtDrivingLicenceCancelledOpt_hlm', 2)){
															 if(document.getElementById('odExtDrivingLicenceCancelledOpt1').checked == true){
																if(isNull('odExtDrivingLicenceCancelledOptDesc', 'odExtDrivingLicenceCancelledOptDescSection', 2)){}else {return false;}
															 }
															 
															 if(isNull('odExtBearedPercentageTxt', 'odExtBearedPercentageOption', 2) && isValueInvalid('odExtBearedPercentageTxt',1, 100, 'odExtBearedPercentageOption', 2)){}else {return false;}
														   }else {return false;} // End of Continue 9
														 }else {return false;}
													  }else {return false;}
												   }else {return false;}
												 }else {return false;}
											   }else {return false;}
											 }else {return false;} // End of Continue 7
										  }
									    }else {return false;}
									  }else {return false;}// End of Continue 6
                                    }else {return false;}// End of Continue 5
								  }else {return false;}
								}else {return false;}
							  }else {return false;}
							}else {return false;}
						  }else {return false;}
						}else {return false;}
					  }// End of Depending field(s)
					  
					  // SECTION THREE (3)
					   if(validateSelectBox('odAgeRestrictionSelect', 2)){
						 return true;
					   } // END OF SECTION THREE (3)
					}// End of Continue 4
				 }
			   }
			 }//  End of Continue 3
		 }// End of Continue 1
	}
   }
 return false;
}

function validateSectionFour(){
 // Section Four (1)
 if(checkRadioButton('haveAnyInsuranceOpt',2,'haveAnyInsuranceOptions', 3)){
	  // Check depending field(s) [FOR YES]
	  if(document.getElementById('haveAnyInsuranceOpt1').checked == true){
	   if(validateSelectBox('insuraceCompanySelect', 3)){
		if(isNull('insuracePayPerYear', '', 3) && isValueInvalid('insuracePayPerYear',100, 20000, '', 3)){	 
		  if(checkRadioButton('isInsuranceDueOpt',2,'isInsuranceDueOpt_hlm', 3)){
			return true;
		  }
		}
	   }
	  }

	  // Check depending field(s) [FOR NO]
	  if(document.getElementById('haveAnyInsuranceOpt2').checked == true) {
		   if(checkRadioButton('hadAnyPreviousCarOpt',2,'hadAnyPreviousCarOptions', 3)){
			  if(document.getElementById('hadAnyPreviousCarOpt1').checked == true){
				 // Section A
				 if(validateSelectBox('pc_makeOfCarSelect', 3)){
				  if(validateSelectBox('pc_modelOfCarSelect', 3)){
				   if(validateSelectBox('pc_yearOfCarSelect', 3)){
				    if(validateSelectBox('pc_transmissionTypeSelect', 3)){
					 if(validateSelectBox('pc_fuelTypeSelect', 3)){
					  if(validateSelectBox('pc_carSelect', 3)){
				
					   // Section B
					   if(isNull('pc_postCodeTxt', '', 3)){
					    if(validateSelectBox('pc_carParkedSelect', 3)){
				
					     // Section C
					     if(validateSelectBox('pc_carUsedSelect', 3)){
						  if(validateSelectBox('pc_financeTypeSelect', 3)){
						   if(validateSelectBox('pc_insuraceCompanySelect', 3)){
						    if(isNull('pc_insuracePayPerYear', '', 3) && isValueInvalid('pc_insuracePayPerYear',500, 9999999, '', 3)){
								if(checkRadioButton('pc_isInsuranceDueOpt',2,'pc_isInsuranceDueOpt_hlm', 3)){
							 		return true;
								}
						    }
						   }
						  }
					     }
					    }
					   }
					  }
					 }
				    }
				   }
				  }
				 }
			  }else {return true;}
		   }
	  } 
  
 }
 return false;
}

        var url = 'http://www.zippy.com.au/utilities/leads/home/captcheck.php?code=';
        var captchaOK = 2;  // 2 - not yet checked, 1 - correct, 0 - failed
        
        function getHTTPObject()
        {
        try {
        req = new XMLHttpRequest();
          } catch (err1)
          {
          try {
          req = new ActiveXObject("Msxml12.XMLHTTP");
          } catch (err2)
          {
          try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3)
            {
	req = false;
            }
          }
	}
        return req;
	}
        
        var http = getHTTPObject(); // We create the HTTP Object        
        
        function handleHttpResponse() {
        if (http.readyState == 4) {
            captchaOK = http.responseText;
            if(captchaOK != 1) {
              alert('The entered code was not correct. Please try again');
              document.getElementById('txtCaptcha').value='';
              document.getElementById('txtCaptcha').focus();
              return false;
              }
			 
              document.getElementById('HomeQuoteForm').submit();
			
           }
        }

        function checkcode(thecode) {
			
        http.open("GET", url + escape(thecode), true);
        http.onreadystatechange = handleHttpResponse;
        http.send(null);
        }
function validateSectionFive(){

  var maxValidYearValue = parseInt(document.getElementById('currentYearValue').value)-18;
  var minValidYearValue = maxValidYearValue - 78;
 // Section One (1)
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if(document.getElementById('aboutYouTitle').value == '')
	{
		alert('Please select Your title.');
		document.getElementById('aboutYouTitle').style.border = '1px solid red';
		document.getElementById('aboutYouTitle').focus();
		return false;
	}
	
   if(document.getElementById('aboutYouFirstName').value == '')
	 {
	   alert('Please enter your first name.');
		document.getElementById('aboutYouFirstName').style.border= '1px solid red';
		document.getElementById('aboutYouFirstName').focus();
		return false;
	 }
    if(document.getElementById('aboutYouLastName').value == '')
	 {
	   alert('Please enter your last name.');
		document.getElementById('aboutYouLastName').style.border= '1px solid red';
		document.getElementById('aboutYouLastName').focus();
		return false;
	 }
    if(document.getElementById('birthDayTxt').value == '')
	 {
	   alert('Please enter your Birth date.');
		document.getElementById('birthDayTxt').style.border= '1px solid red';
		document.getElementById('birthDayTxt').focus();
		return false;
	 }
    if(document.getElementById('birthMonthTxt').value == '')
	 {
	   alert('Please enter your Birth Month.');
		document.getElementById('birthMonthTxt').style.border= '1px solid red';
		document.getElementById('birthMonthTxt').focus();
		return false;
	 }
	 if(document.getElementById('birthYearTxt').value == '')
		 {
		   alert('Please enter your Birth year.');
			document.getElementById('birthYearTxt').style.border= '1px solid red';
			document.getElementById('birthYearTxt').focus();
			return false;
		 }
	if(document.getElementById('genderOpt1').checked == false && document.getElementById('genderOpt1').checked == false)
		 {
		   alert('Please Select your gender.');
			document.getElementById('genderOpt1').style.border= '1px solid red';
			document.getElementById('genderOpt2').style.border= '1px solid red';
			document.getElementById('genderOpt1').focus();
			return false;
		 }
	if(document.getElementById('maritalStatusSelect').value == '')
		 {
		   alert('Please Select your Marital Status.');
			document.getElementById('maritalStatusSelect').style.border= '1px solid red';
			document.getElementById('maritalStatusSelect').focus();
			return false;
		 }
	if(document.getElementById('aboutYouCity').value == '')
		 {
		   alert('Please enter your City.');
			document.getElementById('aboutYouCity').style.border= '1px solid red';
			document.getElementById('aboutYouCity').focus();
			return false;
		 }
	if(document.getElementById('requesterEmailTxt').value == '')
		 {
		   alert('Please enter Your Email Address .');
			document.getElementById('requesterEmailTxt').style.border= '1px solid red';
			document.getElementById('requesterEmailTxt').focus();
			return false;
		 }
	if(!filter.test(document.getElementById('requesterEmailTxt').value))
	{
			 alert('Please enter Valid Email Address .');
			document.getElementById('requesterEmailTxt').style.border= '1px solid red';
			document.getElementById('requesterEmailTxt').focus();
			return false;
	}
	if(document.getElementById('aboutYouPhoneNumber').value == '')
	 {
	   alert('Please enter Your Phone Number.');
		document.getElementById('aboutYouPhoneNumber').style.border= '1px solid red';
		document.getElementById('aboutYouPhoneNumber').focus();
		return false;
	 }
	 if(document.getElementById('txtCaptcha').value == '')
	 {
	   alert('Please Verfy Code.');
		document.getElementById('txtCaptcha').style.border= '1px solid red';
		document.getElementById('txtCaptcha').focus();
		return false;
	 }
	 checkcode(document.getElementById('txtCaptcha').value);	
		return false; 
}


function validateInsuranceQuoteForm(){
	if(isNull('txtCaptcha', '', 4)){
		if(validateSectionOne()){
			if(validateSectionTwo()){
				if(validateSectionThree()){
					if(validateSectionFour()){
					  if(validateSectionFive()){
						 if(isNull('txtCaptcha', '', 4)){
						  return true;
						 }
					  }
					}
				}
			}
		}
	}
	return false;
}

function validatePasswordVerityForm(){
 if(isNull('toiqotePassword', '', -1)){
	return true;
 }
 return false;
}

function validateStoreUserEmailForm(){
 if(isNull('toiqoteEmail', '', -1)){
   if(verifyEmail('toiqoteEmail', -1)){
	return true;
   }
 }
 return false;
}

/*
	Function for checking null value
	by Mohammad Belal Hossain
	on February 2009
	parameter: eid = element id
	return: if value is null it returns false
*/
function isNull(eid, markedEid, sectionId){
	var val = document.getElementById(eid).value;
	if(markedEid == ''){ markedEid = eid + "_hlm"; } 

	if(val == ""){
		alert("You can not leave this field blank");
		if(sectionId != -1){ddaccordion.expandone('submenuheader',sectionId);}
		document.getElementById(markedEid).style.border = '1px solid #FF3300';
		document.getElementById(eid).focus();
		return false;
	}
	document.getElementById(markedEid).style.border = 'none';
	return true;
}

/*
	Function for email validations
	by Mohammad Belal Hossain [edited code from net code]
	on February 2009
	parameter: eid = element id
	return: if value is in email format it returns true, else return false
*/
function verifyEmail(eid, sectionId){
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var hlmEid = eid + "_hlm";

	 if (document.getElementById(eid).value.search(emailRegEx) == -1) {
		alert("Please enter a valid email address.");
		if(sectionId != -1){ddaccordion.expandone('submenuheader',sectionId);}
		document.getElementById(hlmEid).style.border = '1px solid #FF3300';
		document.getElementById(eid).focus();
		return false;
     }
	document.getElementById(hlmEid).style.border = 'none';
     return true;
}

/*
	Function for select box or combo box validation
	by Mohammad Belal Hossain
	on February 2009
	parameter: eid = element id
	return: if first option is selected it will return false, for any other value will return true
*/
function validateSelectBox(eid, sectionId){
	var hlmEid = eid + "_hlm";
	if(document.getElementById(eid).selectedIndex == 0){
		alert("You have not selected any option.");
		if(sectionId != -1){ddaccordion.expandone('submenuheader',sectionId);}
		document.getElementById(hlmEid).style.border = "1px solid #FF3300";
		return false;
	}
	document.getElementById(hlmEid).style.border = "none";
	return true;	
}

/*
	Function for radio component validation
	by Mohammad Belal Hossain
	on February 2009
	parameter: 
		eid = element id
		eidNumber = number of radio box
		markedEid = if no radio option is selected then which element to highlight
	return: if any of the radio element is selected will return true, else false
*/
function checkRadioButton(eid,eidNumber,markedEid,sectionId){
	var val = false;
	for(var i=1;i<=eidNumber;i++){
		val = document.getElementById(eid+i).checked;
		//alert("eid["+eid+i+" val["+val);
		if(val == true){
		    document.getElementById(markedEid).style.border = "none";
			return true;
		}
	}
	alert("You must select an option");
	if(sectionId != -1){ddaccordion.expandone('submenuheader',sectionId);}
	document.getElementById(markedEid).style.border = "1px solid #FF3300";
	return val;
}

/*
	Function for check invalid range of value
	by Mohammad Belal Hossain
	on February 2009
	parameter: 
		eid = element id
		minRangeVal = min value range including this value
		maxRangeVal = maximum value range including this value
	return: if the element value is less or greater than the defiened range, it will say invalid value
*/
function isValueInvalid(eid, minRangeVal, maxRangeVal, markedEid, sectionId){
	if(markedEid == ''){ markedEid = eid + "_hlm"; } 
	var value = parseInt(document.getElementById(eid).value);
	if((value<minRangeVal) || (value>maxRangeVal)){
		alert("Sorry, you have provided invalid value. Valid value range is ["+minRangeVal+"-"+maxRangeVal+"]");
		if(sectionId != -1){ddaccordion.expandone('submenuheader',sectionId);}
		document.getElementById(markedEid).style.border = "1px solid #FF3300";
		document.getElementById(eid).focus();
		return false;
	}
	document.getElementById(markedEid).style.border = "none";
	return true;	
}

function validateHomeQuoteEmailPage()  {
	
  return validateSectionFive();
}
