
var acrobatVersion = 0;
function getAcrobatVersion() {
	var agent = navigator.userAgent.toLowerCase(); 
	
	// NS3+, Opera3+, IE5+ Mac, Safari (support plugin array):  check for Acrobat plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
      for (i=0; i < navigator.plugins.length; i++ ) {
         var plugin = navigator.plugins[i];
         if (plugin.name.indexOf("Adobe Acrobat") > -1) {
            acrobatVersion = parseFloat(plugin.description.substring(30));
         }
      }
	}
   
	// IE4+ Win32:  attempt to create an ActiveX object using VBScript
	else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
	   document.write('<scr' + 'ipt language="VBScript"\> \n');
	   document.write('on error resume next \n');
		document.write('dim obAcrobat \n');
		document.write('set obAcrobat = CreateObject("PDF.PdfCtrl.5") \n');
		document.write('if IsObject(obAcrobat) then \n');
		document.write('acrobatVersion = 5 \n');
		document.write('else set obAcrobat = CreateObject("PDF.PdfCtrl.1") end if \n');
		document.write('if acrobatVersion < 5 and IsObject(obAcrobat) then \n');
		document.write('acrobatVersion = 4 \n');
		document.write('end if');
		document.write('</scr' + 'ipt\> \n');
  }

	// Can't detect in all other cases
	else {
		acrobatVersion = acrobatVersion_DONTKNOW;
	}

	return acrobatVersion;
}

acrobatVersion_DONTKNOW = -1;


function SetDirtyFlag(theControl){
	
	theControl.form.Certificate.disabled = true
	
}
function ValidateQuote(theForm){

	if (!theForm.txt_PackedProtected.checked){
		alert("Goods that are not fully packed and protected cannot be insured");
		theForm.txt_PackedProtected.focus();
		return false;
	}

	if (theForm.txt_ConveyanceSea.checked && !theForm.txt_EnclosedContainer.checked){
		alert("All goods shipped by sea must be in a fully enclosed ISO container");
		return false;
	}

	if (!theForm.txt_ConveyanceRoadRail.checked && !theForm.txt_ConveyanceAir.checked && !theForm.txt_ConveyanceSea.checked){
		alert("One or more conveyance method must be selected");
		return false;
	}	
	
	if (theForm.txt_CommodityCategoryID.value == 0){
		alert("A 'Commodity Category' must be selected");
		theForm.txt_CommodityCategoryID.focus();
		return false;
	}

	if (theForm.txt_ConditionID.value == 0){
		alert("A 'Goods Condition' must be selected");
		theForm.txt_ConditionID.focus();
		return false;
	}
	if (theForm.txt_FromLocation.value.length == 0){
		alert("A 'From Location' must be entered");
		theForm.txt_FromLocation.focus();
		return false;
	}

	if (theForm.txt_FromCountryID.value == 0){
		alert("A 'From Country' must be selected");
		theForm.txt_FromCountryID.focus();
		return false;
	}

	if (theForm.txt_ToLocation.value.length == 0){
		alert("A 'To Location' must be entered");
		theForm.txt_ToLocation.focus();
		return false;
	}
	
	if (theForm.txt_ToCountryID.value == 0){
		alert("A 'To Country' must be selected");
		theForm.txt_ToCountryID.focus();
		return false;
	}

	if ((theForm.txt_SumInsured.value - 0) == 0){
		alert("Sum Insured value must be more than zero"); 
		theForm.txt_SumInsured.focus();
		return false;
	}

	if (!isDate(theForm.txt_EstDepartureDate.value, "dd/MM/yyyy")){
		alert("Estimated departure date must be a valid date (dd/mm/yyyy)");
		theForm.txt_EstDepartureDate.focus();
		return false;
	}
	
	if (!isFutureDate(theForm.txt_EstDepartureDate.value, theForm.MaxFutureDepartureDate.value, true)){
		alert("Estimated departure date must be today or up to " + theForm.MaxFutureDepartureDate.value + " days in the future");
		theForm.txt_EstDepartureDate.focus();
		return false;
	}

	if (!theForm.chk_Confirm.checked){
		alert("You must read and confirm the declaration");
		return false;
	}
	
	var MaxSISelectedCurrency = ConvertToSelectedCurrency(theForm.MaximumSumInsured.value, theForm.txt_OriginalCurrencyID)
	var SelectedCurrencyCode = GetSelectedCurrencyCode(theForm.txt_OriginalCurrencyID)
	if ((theForm.txt_TotalSumInsured.value - 0) > MaxSISelectedCurrency){
		var strDestination = "default.asp?ID=7&QID=" + theForm.txt_QuoteID.value
 		if (SelectedCurrencyCode != 'GBP'){
			strDestination = strDestination + "&CMax=" + SelectedCurrencyCode + " " + MaxSISelectedCurrency
		}
		disableButtons(theForm);
		theForm.action = strDestination
		theForm.submit()
		return false;
	}
	return true
	//disableButtons(theForm);
}

function ValidateQuoteExtra(theForm){

	if (theForm.ClientID.value.length == 0){
		if (theForm.txt_FreightForwarder.length == 1 ) {
			alert("You have not selected any preferred Freight Forwarders. \n\n Click on 'Forwarders' then 'Add' before completing the quote");
			return false;
		} else {
			if (theForm.txt_FreightForwarder.options[theForm.txt_FreightForwarder.selectedIndex].text == ''){
				alert("A freight forwarder must be selected");
				theForm.txt_FreightForwarder.focus();
				return false;
			}
		}
	}
		
	if (theForm.txt_GoodsDescription.value.length == 0){
		alert("Description of goods and packing must be entered");
		theForm.txt_GoodsDescription.focus();
		return false;
	}

	var strMsg = "We must advise you that there are no refunds on this policy.\n\nAre you sure that you want to proceed?"
	if (confirm(strMsg) == false) { return false }

	return true
}

function ValidateQuoteDraft(theForm) {

	if (!isDate(theForm.txt_EstDepartureDate.value, "dd/MM/yyyy")){
		alert("Estimated departure date must be a valid date (dd/mm/yyyy)");
		theForm.txt_EstDepartureDate.focus();
		return false;
	}
	
	if (!isFutureDate(theForm.txt_EstDepartureDate.value, theForm.MaxFutureDepartureDate.value, true)){
		alert("Estimated departure date must be today or up to " + theForm.MaxFutureDepartureDate.value + " days in the future");
		theForm.txt_EstDepartureDate.focus();
		return false;
	}

	if (theForm.txt_GoodsDescription.value.length == 0){
		alert("Description of goods and packing must be entered");
		theForm.txt_GoodsDescription.focus();
		return false;
	}

	return true;
	
}

function InsuredChanged (theForm){
//	theForm.txt_SumInsured.value = formatCurrency(theForm.txt_SumInsured.value)
	if (!isCurrency(theForm.txt_SumInsured.value)){
		alert("Sum Insured value must be a currency value without commas e.g.1234.56"); 
		theForm.txt_SumInsured.value="0"
	}
	if (!isNumeric(theForm.txt_Uplift.value)){
		alert("Uplift Percentage value must be a number between 0 and 20 (inclusive)");
		theForm.txt_Uplift.value = "10"
	}
	if (!(eval(theForm.txt_Uplift.value) >= 0 && eval(theForm.txt_Uplift.value) <=20)) {
		alert("Uplift Percentage value must be between 0 and 20");
		theForm.txt_Uplift.value = "10"
	}
	theForm.txt_SumInsured.value = formatCurrency(theForm.txt_SumInsured.value)
	theForm.txt_TotalSumInsured.value = formatCurrency(theForm.txt_SumInsured.value*((100 + eval(theForm.txt_Uplift.value))/100));		  	
}

function formatCurrency(curr){
	var str = String(curr)
	var dp = str.indexOf(".")
	if (dp == -1){str += ".";dp = str.indexOf(".")}
	str += "00"
	return str.substr(0, dp + 3)
}

function TotalInsuredChanged (theForm){
	if (!isCurrency(theForm.txt_TotalSumInsured.value)){
		alert("Total Sum Insured value must be a currency value without commas e.g.1234.56"); 
		theForm.txt_TotalSumInsured.value="0"
	}
	theForm.txt_TotalSumInsured.value = formatCurrency(theForm.txt_TotalSumInsured.value)
	theForm.txt_SumInsured.value = formatCurrency((theForm.txt_TotalSumInsured.value/((100 + eval(theForm.txt_Uplift.value))))*100);		  	
}

function ConveyanceSea(theForm) {
	
	theForm.txt_EnclosedContainer.disabled = (theForm.txt_ConveyanceSea.checked==false);
	
	return true
}

function NavigateToLogin(theForm, loginURL) {
	window.location= loginURL + '?QID=' + theForm.txt_QuoteID.value
}

function NavigateToSaved(theForm) {
	NavigateToForm(theForm, 6)
}

function NavigateToForm(theForm, ID){
	disableButtons(theForm);
	theForm.action = 'default.asp?ID=' + ID;
	theForm.submit()
}

function NavigateToRecall(theForm, ID){
	disableButtons(theForm);
	theForm.action = 'recall.asp?ID=' + ID;
	theForm.submit()
}

function NavigateToPayment(theForm,ID){
	var destination = '/public/payment.asp'
	destination+=ID?'?ID='+ID:''
	disableButtons(theForm)
	theForm.action = destination
	theForm.submit()
}

function NavigateToBrokerAccount(theForm){
	disableButtons(theForm)
	theForm.action = '/broker/account.asp'
	theForm.submit()
} 

function NavigateSelectStatus(theForm) {
	window.location='recall.asp?Status=' + theForm.txt_Status.options[theForm.txt_Status.selectedIndex].text
}

function NavigateToCertificate(quote){
//navigator.userAgent.toLowerCase()
	if(!document.all) { 
		//not IE - check version of adobe
		if (getAcrobatVersion() < 7 ){ 
			alert("The version of Adobe Acrobat required to view certificates is 7.0.5 or higher");
			return
		}
	}	
	var strEncrypted = jsEncryptString(quote)
	window.open("certificatedisplay.asp?KEY=" + strEncrypted ,"Certificate",'toolbar=no,status=no,address=no,scrollbars=yes,menubar=no,resizable=yes,directories=no,fullscreen=no')
}

function QuotePerformAction(theControl) {
	var action = theControl.innerHTML
	//DO NOT USE CHILDNODES AS MOZILLA SEES WHITESPACE IN XML AS EXTRA TEXT NODES. USE getElementsByTagName instead.
	var quote = theControl.parentNode.parentNode.getElementsByTagName('input')[0].value 
	switch (action.toLowerCase()){
		case 'buy':
			//window.location = 'default.asp?ID=1&QID=' + quote
			document.forms[0].action = 'default.asp?ID=1&QID=' + quote
			document.forms[0].submit()
			break
		case 'view':
			window.location = 'default.asp?ID=1&QID=' + quote
			break
		case 'delete':
			if(confirm("Click OK to delete the selected quote")){
				window.location = 'recall.asp?ID=1&Delete=' + quote
			}
			break
		case 'copy':
			window.location = 'default.asp?ID=1&Copy=' + quote
			break
		case 'certificate':
			NavigateToCertificate(quote)
		default:
			//do nothing
	}
}
function EnableControls(theForm){
	theForm.txt_EnclosedContainer.disabled = !theForm.txt_ConveyanceSea.checked
	if (theForm.txt_ClientID.value !=''){ theForm.txt_AgentID.value = theForm.txt_ClientID.value}
	return false;
}

function ConvertToSelectedCurrency(theValue, theCurrencyControl){
	var ExchRateFactor = theCurrencyControl.options[theCurrencyControl.selectedIndex].attributes.getNamedItem("Extra").value
	return theValue * ExchRateFactor
}

function GetSelectedCurrencyCode(theCurrencyControl){
	var SelectedCurrencyCode = theCurrencyControl.options[theCurrencyControl.selectedIndex].attributes.getNamedItem("Extra1").value
	return SelectedCurrencyCode
}

