// following 7 lines are for redirecting http user to secured https
if (document.location.hostname == 'www.globalrescue.com') {
	if (document.location.protocol != "https:") {
		document.location.href = "https://www.globalrescue.com" + document.location.pathname;
	}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
// securing code ends

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openPictureWindow_Fever(imageType,imageName,imageWidth,imageHeight,alt,posLeft,posTop) {  // v4.01
	newWindow = window.open("","newWindow","width="+imageWidth+",height="+imageHeight+",scrollbars=no,left="+posLeft+",top="+posTop);
	newWindow.document.open();
	newWindow.document.write('<html><title>'+alt+'</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">'); 
	if (imageType == "swf"){
	newWindow.document.write('<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0\" width=\"'+imageWidth+'\" height=\"'+imageHeight+'\">');
	newWindow.document.write('<param name=movie value=\"'+imageName+'\"><param name=quality value=high>');
	newWindow.document.write('<embed src=\"'+imageName+'\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"'+imageWidth+'\" height=\"'+imageHeight+'\">');
	newWindow.document.write('</embed></object>');	}else{
	newWindow.document.write('<img src=\"'+imageName+'\" width='+imageWidth+' height='+imageHeight+' alt=\"'+alt+'\">'); 	}
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function days_between(date1, date2) { 

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = date1_ms - date2_ms;
 	//var difference_ms = Math.abs(date1_ms - date2_ms)
    //alert('difference_ms = '+difference_ms.toString());
    //alert(Math.round(difference_ms/ONE_DAY));
    // Convert to days and return
    return Math.round(difference_ms/ONE_DAY);

} 

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return (result3)
}

function getDefaultPagePrice(renewDate,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired) 
{	
	var x;
	daysLeft = currentExpirationDate - renewDate;
	date1 = new Date(currentExpirationDate);
	date2 = new Date(renewDate);
	cur_start = new Date(currentStartDate);
	var daysLeft = days_between(date1, date2);
	var daysBeforeStart = days_between(cur_start, date2);
		
	if( (isExpired==1) || (daysLeft<0) ){
		x = round_decimals(parseInt(costNewPlan),2);
	} else {
		if(daysBeforeStart > 0) {
			x = round_decimals(parseInt(costNewPlan) - parseInt(origPaid),2);
		} else {
			x = round_decimals(parseInt(costNewPlan) - ((parseInt(daysLeft)/parseInt(planLengthInDays))* parseInt(origPaid)),2);
		}
	}
	
	var newText;
	var b = document.forms[1].sub;
	var c = document.forms[1].cost;
	//alert(x);
	if( x > 0 ) {
		c.value = x;
		newText = 'Total: <strong>$' + x + '</strong>';
		document.getElementById("costdiv").innerHTML=newText;
     	b.disabled = false;
      	b.value = 'Add Security Service';
	} else {
		c.value = 0;
		newText = 'Renewal requires you to work with customer service.<br /><strong>Please contact Global Rescue customer support at <nobr>(800) 381-9754</nobr> to discuss your request.</strong>';
		document.getElementById("costdiv").innerHTML=newText;
		b.disabled = true;
      	b.value = 'Please call to add Security Service';
	}
}


function getSecurityAddPrice(renewDate,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired) 
{
	//alert('Entered getSecurityAddPrice(renewDate)');
	//alert('renewDate='+ renewDate);
	//alert('costNewPlan='+ costNewPlan.toString());
	//alert('planLengthInDays='+ planLengthInDays.toString());
	//alert('origPaid='+ origPaid.toString());
	//alert('isExpired='+ isExpired.toString());
	//alert('currentStartDate='+ isExpired.toString());
	//alert('currentExpirationDate='+ isExpired.toString());
	
	var x;
	daysLeft = currentExpirationDate - renewDate;
	date1 = new Date(currentExpirationDate);
	date2 = new Date(renewDate);
	cur_start = new Date(currentStartDate);
	var daysLeft = days_between(date1, date2);
	//alert(daysLeft.toString());
	var daysBeforeStart = days_between(cur_start, date2);
	//alert('daysBeforeStart='+daysBeforeStart.toString());
		
	if( (isExpired==1) || (daysLeft<0) ){
		x = round_decimals(parseInt(costNewPlan),2);
	} else {
		if(daysBeforeStart > 0) {
			x = round_decimals(parseInt(costNewPlan) - parseInt(origPaid),2);
		} else {
			x = round_decimals(parseInt(costNewPlan) - ((parseInt(daysLeft)/parseInt(planLengthInDays))* parseInt(origPaid)),2);
		}
	}
	
	var newText;
	var b = document.forms[0].sub;
	var c = document.forms[0].cost;
	//alert(x);
	if( x > 0 ) {
		c.value = x;
		newText = 'Total: <strong>$' + x + '</strong>';
		document.getElementById("costdiv").innerHTML=newText;
		
		EnableButton();
	} else {
		c.value = 0;
		newText = 'Renewal requires you to work with customer service.<br /><strong>Please contact Global Rescue customer support at <nobr>(800) 381-9754</nobr> to discuss your request.</strong>';
		document.getElementById("costdiv").innerHTML=newText;
		
		b.disabled = true;
      	b.value = 'Unable to renew online';
	}
}

function getRenewPrice(renewDate,withSecurity,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired) 
{
	//alert('Entered getSecurityAddPrice(renewDate)');
	//alert('renewDate='+ renewDate);
	//alert('withSecurity='+ withSecurity);
	//alert('costNewPlan='+ costNewPlan.toString());
	//alert('planLengthInDays='+ planLengthInDays.toString());
	//alert('origPaid='+ origPaid.toString());
	//alert('isExpired='+ isExpired.toString());
	
	var x;
	daysLeft = currentExpirationDate - renewDate;
	date1 = new Date(currentExpirationDate);
	date2 = new Date(renewDate);
	cur_start = new Date(currentStartDate);
	var daysLeft = days_between(date1, date2);
	//alert(daysLeft.toString());
	var daysBeforeStart = days_between(cur_start, date2);
	//alert('daysBeforeStart='+daysBeforeStart.toString());
		
	if(withSecurity==true) {
		if( (isExpired==1) || (daysLeft<0) ){
			x = round_decimals(parseInt(costNewPlan),2);
		} else {
			if(daysBeforeStart > 0) {
				x = round_decimals(parseInt(costNewPlan) - parseInt(origPaid),2);
			} else {
				x = round_decimals(parseInt(costNewPlan) - ((parseInt(daysLeft)/parseInt(planLengthInDays))* parseInt(origPaid)),2);
			}
		}
	
	} else {
		if( (isExpired==1) || (daysLeft<0) ){
			x = round_decimals(parseInt(origPaid),2);
		} else {
			if(daysBeforeStart > 0) {
				x = round_decimals(parseInt(origPaid),2);
			} else {
				x = round_decimals(parseInt(origPaid) - ((parseInt(daysLeft)/parseInt(planLengthInDays))* parseInt(origPaid)),2);
			}
		}
	}
	var newText;
	var b = document.forms[0].sub;
	//alert(x);
	if( x > 0 ) {
		newText = 'Total: <strong>	$' + x + '</strong>';
		document.getElementById("costdiv").innerHTML=newText;
		document.forms[0].cost.value = x;
		EnableButton();
	} else {
		newText = 'Renewal requires you to work with customer service.<br /><strong>Please contact Global Rescue customer support at <nobr>(800) 381-9754</nobr> to discuss your renewal request.</strong>';
		document.getElementById("costdiv").innerHTML=newText;
		document.forms[0].cost.value = 0;
		b.disabled = true;
      	b.value = 'Unable to renew online';
	}
	//return x;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function updateCCInfo()
{
	var choice;
	var newText;
	var x;
	var radioObj = document.forms[0].cardToUse;
	var radioLength = radioObj.length;
	/*for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			choice=radioObj[i].value;
			alert(choice);
		} 
	}*/
	//alert(cardToUse);
	//if( choice=="new" )
	if(radioObj[1].checked==true)
	{
		document.forms[0].first_name.value=document.forms[0].first_name2.value;
		//alert(document.forms[0].first_name.value);
		document.forms[0].last_name.value=document.forms[0].last_name2.value;
		document.forms[0].CardNumber.value=document.forms[0].CardNumber2.value;
		document.forms[0].Expiration.value=document.forms[0].Expiration2.value;
		document.forms[0].ccv.value=document.forms[0].ccv2.value;
		document.forms[0].new_card.value="new";
		document.forms[0].name.value=document.forms[0].first_name2.value + ' ' + document.forms[0].last_name2.value;
		
	} else {
	
		document.forms[0].first_name.value=document.forms[0].first_name1.value;
		document.forms[0].last_name.value=document.forms[0].last_name1.value;
		document.forms[0].CardNumber.value=document.forms[0].CardNumber1.value;
		document.forms[0].Expiration.value=document.forms[0].Expiration1.value;
		document.forms[0].ccv.value=document.forms[0].ccv1.value;
		document.forms[0].new_card.value="saved";
		document.forms[0].name.value=document.forms[0].first_name1.value + ' ' + document.forms[0].last_name1.value;
		
		}
	
	//var msg = ' first_name=' + document.forms[0].first_name.value + ' last_name=' + document.forms[0].last_name.value + ' card=' +  document.forms[0].CardNumber.value + ' exp=' + document.forms[0].Expiration.value + ' ccv=' +document.forms[0].ccv.value;
	//alert(msg);
	
	
}

function validate()
{
	var er = true;
	
	//start date is reqd
	if( document.forms[0].start_date.value.toString().length < 1 )
	{
		alert("Start date is required. Please enter your renewal start date.");
		document.forms[0].start_date.focus();
		er = false;
	} else {
	
		//cost must be greater than 0
		if( document.forms[0].cost.value <= 0 )
		{
			alert("This renewal or upgrade request requires the assistance of Global Rescue customer service.");
			er = false;
		} else {
			var choice;
			var radioObj = document.forms[0].cardToUse;
			var radioLength = radioObj.length;
			for(var i = 0; i < radioLength; i++) {
				if(radioObj[i].checked) {
					choice=radioObj[i].value;
				} 
			}
	
			//ccv is required
			if( choice=="new" )
			{
				if(document.forms[0].first_name2.value=="" || document.forms[0].first_name2.value==null )
				{
					//ccv reqd and ccv should match
					alert("Please enter the first name on your credit card.");
					document.forms[0].first_name2.focus();
					er = false;
				} else {
					if(document.forms[0].last_name2.value=="" || document.forms[0].last_name2.value==null )
					{
						//ccv reqd and ccv should match
						alert("Please enter the last name on your credit card.");
						document.forms[0].last_name2.focus();
						er = false;
					} else {
						if(document.forms[0].CardNumber2.value=="" || document.forms[0].CardNumber2.value==null )
						{
							//ccv reqd and ccv should match
							alert("Please enter your credit card number.");
							document.forms[0].CardNumber2.focus();
							er = false;
						} else {
							if(document.forms[0].Expiration2.value=="" || document.forms[0].Expiration2.value==null )
							{
								//ccv reqd and ccv should match
								alert("Please enter the expiration date on your credit card in the form MM/YYYY.");
								document.forms[0].Expiration2.focus();
								er = false;
							} else {
								if(document.forms[0].ccv2.value=="" || document.forms[0].ccv2.value==null )
								{
									//ccv reqd and ccv should match
									alert("Your credit card verification (CCV) number is required.  Please enter the three-character ccv number from the back of your credit card.");
									document.forms[0].ccv2.focus();
									er = false;
								}
							}
						}
					}
				}
			} else {
				if(document.forms[0].ccv1.value=="" || document.forms[0].ccv1.value==null )
				{
					//ccv reqd and ccv should match
					alert("Your credit card verification (CCV) number is required.  Please enter the three-character ccv number from the back of your credit card.");
					document.forms[0].ccv1.focus();
					er = false;
				} else {
					er = true;
				}
			}
		}
	}
	return er;
}

function formfinisher(renewDate,withSecurity,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired,functn)
{
	/*alert('Entered formfinisher');
	alert('renewDate='+ renewDate);
	alert('withSecurity='+ withSecurity);
	alert('costNewPlan='+ costNewPlan.toString());
	alert('currentStartDate='+currentStartDate.toString() );
	alert('currentExpirationDate='+currentExpirationDate.toString() );
	alert('planLengthInDays='+ planLengthInDays.toString());
	alert('origPaid='+ origPaid.toString());
	alert('isExpired='+ isExpired.toString());
	alert('functn='+ functn.toString());
	*/
	var valid = validate();
    //alert(valid);
    if( valid == true )
     {
     	
     	if( functn=="renew" ) {
     		//alert('functn equal to renew');
     		getRenewPrice(renewDate,withSecurity,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired);
     		//alert('After Get Renew Price');
     	} else {
     		//alert('functn not equal to renew');
     		getSecurityAddPrice(renewDate,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired);
     		//alert('After Get Sec Add Price');
     	}
     	//alert('prior to updateCCInfo');
     	updateCCInfo();
     	//alert('after updateCCInfo');
     	DisableButton(document.forms[0].cost.value);
     	
     }
}

function defaultForm(renewDate,withSecurity,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired,functn)
{
	var valid = validate();
     //alert(valid);
     if( valid == true )
     {
     	if( functn=="renew")
     		getRenewPrice(renewDate,withSecurity,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired);
     	else
     		getSecurityAddPrice(renewDate,costNewPlan,currentStartDate,currentExpirationDate,planLengthInDays,origPaid,isExpired);
     	updateCCInfo();
     	DisableButton(document.forms[0].cost.value);
     }
}

function DisableButton(cst)
  {
     var b = document.forms[0].sub;
     //updateCCInfo(ctu);
     
	     if( confirm("Your credit card will be charged $"+ cst.toString() + ".  Are you sure you want to proceed?") )
	     {
	     	b.disabled = true;
	      	b.value = 'Submitting';
	      	b.form.submit();
	      } 
   }
 
 function EnableButton()
   {
     	var b = document.forms[0].sub;
     	b.disabled = false;
      	b.value = 'Renew Account';
   }



