var isValid=true;
$(function() 
{
$("#result").hide();

	function validateMe(){
		
		
		policyValue = $("select#typeofcover").val();
		coverValue = $("input#cover").val();
		protectionValue = $("select#typeofprotection").val();
		excessperiodValue = $("select#excessperiod").val();
		dayValue = $("select#day").val();
		monthValue = $("select#month").val();
		yearValue = $("select#year").val();
		
		
		isValid=true;
		
		if(policyValue == "0")
		{
			alert("Please select Type of cover");
			isValid=false;
			return false;		
		}
		else if(protectionValue =="0")
		{
			alert("Please select the type of Protection");
			isValid=false;
			return false;
		}
		else if(excessperiodValue =="0")
		{
			alert("Please select the Excess period");
			isValid=false;
			return false;
		}
		else if(dayValue == "0" || monthValue == "0" || yearValue == "YYYY"){
			alert("Please enter your Date of Birth");
			isValid=false;
			return false;
		}
		else if(coverValue == String.fromCharCode(163))
		{
			
			alert("Please enter the your cover");
			isValid=false;
			return false;		
		} 
		else if(parseInt(coverValue) > 2000)
		{
			pound = String.fromCharCode(163);
			alert("The maximum cover allowed is "+ pound +"2,000");
			document.getElementById("cover").value = pound;
			isValid=false;
			return false;		
		}
		else if(parseInt(coverValue) < 100)
		{
			pound = String.fromCharCode(163);
			alert("The minimum benefit amount is "+ pound +"100");
			document.getElementById("cover").value = pound;
			isValid=false;
			return false;		
		}
		
		//script to calculate the age
		var today = new Date();
		var nowyear = today.getFullYear();
		var nowmonth = today.getMonth();
		var nowday = today.getDate();
		var ageValue = nowyear - yearValue;
		{
			if (monthValue > nowmonth) {ageValue = ageValue - 1}  // next birthday not yet reached
			else if (monthValue == nowmonth && nowday < dayValue) {ageValue = ageValue - 1}
			
		}
		
		{
		if(protectionValue == "izzncome_protection" || protectionValue == "payment_protection"){
			if(ageValue<18 || ageValue>64)
			{
				alert("Sorry you do not qualify for this product");
				isValid=false;
				return false;	
			}
		} else if (protectionValue == "payment_protection_plus"){
			if(ageValue<18 || ageValue>62)
			{
				alert("Sorry you do not qualify for this product");
				isValid=false;
				return false;	
			}
		} 
		}
		
		//calculating values for Income Protection Type
		if(protectionValue == "income_protection"){
			if( policyValue == "U"){
				if(excessperiodValue == "1"){
					value = "0.042900";	
				} else if(excessperiodValue == "2"){
					value = "0.036782";
				} else if(excessperiodValue == "3"){
					value = "0.035600";
				} else if(excessperiodValue == "4"){
					value = "0.032000";
				} else if(excessperiodValue == "5"){
					value = "0.029100";
				}
			} else if( policyValue == "AS"){
				if(excessperiodValue == "1"){
					value = "0.031300";
				} else if(excessperiodValue == "2"){
					value = "0.026606";
				} else if(excessperiodValue == "3"){
					value = "0.025200";
				} else if(excessperiodValue == "4"){
					value = "0.024000";
				} else if(excessperiodValue == "5"){
					value = "0.021800";
				}
			} else if( policyValue == "ASU"){
				if(excessperiodValue == "1"){
					value = "0.048654";
				} else if(excessperiodValue == "2"){
					value = "0.039114";
				} else if(excessperiodValue == "3"){
					value = "0.03763";
				} else if(excessperiodValue == "4"){
					value = "0.035616";
				} else if(excessperiodValue == "5"){
					value = "0.033496";
				}
			}
		//calculating values for Payment Protection Type	
		} else if(protectionValue == "payment_protection"){ 
			if( policyValue == "U"){
				if(excessperiodValue == "1"){
					value = "0.042600";
				} else if(excessperiodValue == "2"){
					value = "0.034200";
				} else if(excessperiodValue == "3"){
					value = "0.029800";
				} else if(excessperiodValue == "4"){
					value = "0.028400";
				} else if(excessperiodValue == "5"){
					value = "0.025500";
				}
			} else if( policyValue == "AS"){
				if(excessperiodValue == "1"){
					value = "0.031300";
				} else if(excessperiodValue == "2"){
					value = "0.026200";
				} else if(excessperiodValue == "3"){
					value = "0.025900";
				} else if(excessperiodValue == "4"){
					value = "0.023300";
				} else if(excessperiodValue == "5"){
					value = "0.017500";
				}
			} else if( policyValue == "ASU"){
				if(excessperiodValue == "1"){
					value = "0.048000";
				} else if(excessperiodValue == "2"){
					value = "0.042200";
				} else if(excessperiodValue == "3"){
					value = "0.037400";
				} else if(excessperiodValue == "4"){
					value = "0.031300";
				} else if(excessperiodValue == "5"){
					value = "0.028400";
				}
			}
			
		//calculating values for Payment Protection Type	 
		} else if(protectionValue == "payment_protection_plus"){ 
			if( policyValue == "U"){
				if(excessperiodValue == "1"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.033330";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.037370";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.048480";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.050500";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.052520";
					}
				} else if(excessperiodValue == "2"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.032100";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.035700";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.045500";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.047800";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.050700";
					}	
				} else if(excessperiodValue == "3"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.030800";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.034400";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.044000";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.044800";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.048400";
					}
				} 
			} else if( policyValue == "AS"){
				if(excessperiodValue == "1"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.025250";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.030300";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.032320";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.042420";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.048480";
					}
				} else if(excessperiodValue == "2"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.024500";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.029000";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.030400";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.040000";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.045400";
					}	
				} else if(excessperiodValue == "3"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.023800";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.027800";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.028800";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.038100";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.043200";
					}
				} 
			} else if( policyValue == "ASU"){
				if(excessperiodValue == "1"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.035350";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.039390";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.051510";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.054540";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.056560";
					}
				} else if(excessperiodValue == "2"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.033900";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.037800";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.047000";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.049200";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.051500";
					}	
				} else if(excessperiodValue == "3"){
					if(ageValue >= 18 && ageValue <= 30){
						value = "0.032700";
					} else if(ageValue >= 31 && ageValue <= 40){
						value = "0.036200";
					} else if(ageValue >= 41 && ageValue <= 50){
						value = "0.044700";
					} else if(ageValue >= 51 && ageValue <= 60){
						value = "0.045700";
					} else if(ageValue >= 61 && ageValue <= 62){
						value = "0.049200";
					}
				} 
			} 
		} 
		
		quoteValue = parseFloat(value)*parseInt(coverValue);
		
		$("#quote").hide();
		
		document.quotefrm.age.value = ageValue;
		document.quotefrm.monthlycost.value = Number(quoteValue).toFixed(2);
		document.quotefrm.monthlycover.value = coverValue;
		
		
		$('#monthlycosttxt').html("&pound;"+ Number(quoteValue).toFixed(2));
		$("#result").show();
		
		
		return false;
	}
	
	
	$("#getquote").click(validateMe);
	$("#quotefrm").submit(validateMe);
	
	
	$("#reqrequote").click(function(){
			$("#quote").show();   
			$("#result").hide(); 
	 });
	
	 $("#applyNow").click(function() { 

			if(isValid==true)document.quotefrm.submit();
			
	 });
	 

$(".download_popup_link").click(function(){
	$("#emailiderr").html('');	
	$("#confirmmsg").html('');
	$("#tabdownloadpopup").show();
	$("#download_popup_txt").show();

});

$("#clicktodownload").click(function() {
    var name=document.downloadpopup.name.value;
	 var emailid=document.downloadpopup.emailid.value;
	//var name=$("#name").val();
	//alert(name);
	//var emailid=$("#emailid").val();
	$("#confirmmsg").html('');
	if (name==''){
	$("#emailiderr").html('Please enter name');	
	}
	else if (emailid==''){
		//$("#emailiderr").html('Please enter email id');	
		$("#emailiderr").html('Please enter Email');	
	}
	else if (!echeck(emailid)){
		//$("#emailiderr").html('Please enter email id');	
		$("#emailiderr").html('Invalid Email ID');	
	}
	
	/*if ( (name=='')||(emailid=='')){
		$("#emailiderr").html('Please enter name & email id');	
	}
	else if(!echeck(emailid)){
		$("#emailiderr").html('Invalid Email ID');	
	}*/
      else
	  { 
		$("#emailiderr").html('');
		$("#clicktodownload").attr("disabled","disabled");
		var formN = $('#downloadpopup');
		$.post(formN.attr('action'),formN.serialize(),function(data){
		//$.post("mail_download.php",{sid:Math.random(),name:''+$("#name").val(),emailid:''+$("#emailid").val(),pdf_id:''+$("#pdf_id").val()},function(data){
			$("#tabdownloadpopup").hide();
			$("#download_popup_txt").hide();
			$("#confirmmsg").html('<br><br>Thank you. The link to download this article has now been sent to your email. Please feel free to call us or chat with us if you have any questions.');	
		});
		$("#clicktodownload").removeAttr("disabled");
       }
});

$("#callmeback").click(function() {

var phone=$("#phone").val();
var title=$("#title").val();
var fname=$("#fname").val();
var sname=$("#sname").val();
var calltime=$("#calltime").val();

$("#callbackfrmerr").html('');

if(phone==''){
	$("#callbackfrmerr").html('Please enter phone no');	
}
else if(fname==''){
	$("#callbackfrmerr").html('Please enter first name');	
}
else{
	
$("#callbackfrmerr").html('');

		
$.post("call_back.php",{sid:Math.random(),phone:''+phone,title:''+title,fname:''+fname,sname:''+sname,calltime:''+calltime},function(data){

$("#main_table_callback").hide();
$("#callbackconfirmmsg").show();
$("#callbackconfirmmsg").html('<br><br><p class="text-05 orange">Thank you for your request. One of our trained advisors will call you back at the time  requested by you</p>');	

});


}

});








});




$(document).ready(function() {
						   
$(".biclick").click(function() { 
 $.getScript("http://www.bestinsurance.co.uk/bi_click_track.php?op=click&gt_ref="+escape(document.referrer)+"&gt_pg="+escape(window.location.pathname)+"&gt_lk="+$(this).attr("rel"));
});
				   
						   
//Code goes here

//When you click on a link with class of poplight and the href starts with a # 
$('a.poplight[href^=#]').click(function() {
	
	   	$("#quote1").show();
		$("#result1").hide();
	    ageValue = $("select#age1").val('');
		policyValue = $("select#typeofcover1").val('');
		coverValue = $("input#cover1").val('');
		name = $("input#name1").val('');
		telephone = $("input#telephone1").val('');
		email = $("input#email1").val('');
		postcode = $("input#postcode1").val('');
		
    var popID = $(this).attr('rel'); //Get Popup Name
    var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/closeBtn.png" class="btn_close" title="Close Window" alt="Close" border="0" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 90) / 2;
    var popMargLeft = ($('#' + popID).width() + 65) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
});

//Close Popups and Fade Layer
$('a.close,#fade').live('click', function() { //When clicking on the close or fade layer...
    $('#fade ,.popup_block').fadeOut(function() {
        $('#fade,a.close').remove();  //fade them both out
    });
    return false;
});
});




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 validatenewsletter(){
var firstname=document.frmnewsletter.firstname.value;
var lastname=document.frmnewsletter.lastname.value;
var email=document.frmnewsletter.email.value;
var telephone=document.frmnewsletter.telephone.value;
var enquiry=document.frmnewsletter.enquiry.value;
var name=firstname+"&nbsp;"+lastname;

	if(firstname=='' || firstname=='First Name'){
		$("#subscriptionfrmerr").html('Please Enter First Name');	
	}
	else if(lastname=='' || lastname=='Last Name'){
		$("#subscriptionfrmerr").html('Please Enter Last Name');	
	}
	else if(email =='' || email=='Email'){
		$("#subscriptionfrmerr").html('Please Enter Email');	
	}
	else if(!echeck(email)){
		$("#subscriptionfrmerr").html('Invalid Email ID');	
	}
	else if(telephone =='' || telephone=='Telephone'){
		$("#subscriptionfrmerr").html('Please Enter Telephone Number');	
	}
	else if (checkInternationalPhone(telephone)==false){
          $("#subscriptionfrmerr").html('Please Enter <br/> Valid TelePhone Number');
	}
	else if(enquiry =='' || enquiry=='Enquiry'){
		$("#subscriptionfrmerr").html('Please Enter Enquiry');	
	}
	else{
	$.post("subscribe.php",{sid:Math.random(),name:''+name,email:''+email,telephone:''+telephone,enquiry:''+enquiry},function(data){
		$("#frmnewsletter").hide();
		$("#subscriptionsucess").show();
		$("#subscriptionsucess").html('<br/>Thank you for your enquiry, <br/>one of our advisors will contact you shortly');
		$("#subscriptionfrmerr").hide();
		});
		
	}

	
}


/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
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 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 trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
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){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

