var strErrorCookie = 'MMError';

function fileUploadAjaxProblemWorkAround(obj)
{
document.forms[0].elements["txtFileuploadAjaxProblemWorkAround"].value = obj.value;
}

function disable(obj)
{
/*
if (obj.value = 'Sent for processing')
{
    alert('Sent for payment please be patient while this processes.');
}
obj.value = 'Sent for processing';
*/
}

function endRequest(sender, e){ 
err = e.get_error(); 
if (err){ 
if (e.get_error() != undefined){ 
e.set_errorHandled(true); 
} 
} 
}



function returnDone(intDisplay)
{
switch (intDisplay)
    {
    case 1:
    window.opener.document.getElementById('hrefSurveyUpload').innerText='Upload sent...';
    break
    case 2:
    window.opener.document.getElementById('hrefSurveyUpload').innerText='Upload successful!';  
    break
    case 3:
    window.opener.document.getElementById('hrefSurveyUpload').innerText='Upload failed!';  
    break

    }  
}


function DisplayUpload(obj,intDisplay)
{
switch (intDisplay)
    {
    case 1:
    var s = document.forms[0].elements["hdnHrefSurveyUpload"].value;
    myRef = window.open(s,'Upload','left=400,top=300,width=550,height=230,toolbar=0,resizable=1');
/*    obj.innerText='Done?';    */
    myRef.focus();
    break
    }
}

function cannotInsure1(obj)
{
    if (obj.value==5)
    {
    var s = document.forms[0].elements["ddlCategory"];
    var ddl1value = s.options[s.selectedIndex].value; 
    if ( s.value == 'speedboat' || s.value == 'rib' )    
        {
        alert('Apologies, unfortunately we cannot insure vessels above 230hp.');
        }
    }
}

function cannotInsure2(obj)
{
    if (obj.value=='Out')
    {
        alert('Apologies, unfortunately we cannot insure vessels from this registration category.');
    }
}

function cannotInsure2(obj)
{
    if (obj.value==5)
    {alert('Apologies, unfortunately we cannot insure vessels with power above 230hp.');};
}

function removeCommas(obj)
{
var myOldString = obj.value;
var myNewString = myOldString.replace(/\,/g,'');
}
function removeSpaces(obj)
{
var myOldString = obj.value;
var myNewString = myOldString.replace(/\ /g,'');
}

function checkMedRange()
{
        alert('NB: Please note Mediterranean not east of 35 degrees East excludes cover for North-African coast and the Bosporus and all war zones');
}


function DisableBackPage()
{
    //if(document.referrer != location.href)
    //{
    if (CheckError() == 1)
    {
        ClearError();
    }
    else
    {
        history.forward();
    }
    //}
}
function ClearError()
{
    var expirydate = new Date();
    expirydate.setTime ( expirydate.getTime() - 1 );
    document.cookie = strErrorCookie + '=-1;expires=' + expirydate.toGMTString();
}

function CheckError()
{
 if (document.cookie == '' || document.cookie.indexOf(strErrorCookie)==-1) 
 {
    return 0; 
 }
 else 
 {
    return 1; 
 }
}
function provideUponClaimWarning(obj)
{
if (obj.checked=true)
    {
    alert('Please be aware that failure to be able to provide this in the event of a claim will likely invalidate your insurance claim.');
    }
}

function HideUpload()
{
  window.close();
}

function checkIfOver70(obj)
{ //NB this function is also copied in the admin area

if(obj.value.length>0)
     {
        //calc age
        var myString = obj.value;
        myString.replace("-","/");
        myString.replace(".","/");
        
        var mySplitResult = myString.split("/");
            
        if (mySplitResult[2] != undefined)
        {
            var mySplitResultYear
            if(mySplitResult[2].length==2)
                {
                    mySplitResultYear = "19" + mySplitResult[2];
                }
            else
                {
                    mySplitResultYear =  mySplitResult[2];
                }

            var clientAge = displayage(mySplitResultYear, mySplitResult[1], mySplitResult[0], "years", 0, "rounddown");
            if (clientAge>70)
            {
            alert('Please be aware that unfortunately our policies do not include personal injury cover for clients over the age of 70.');
            }
         }
      }
}
function displayage(yr, mon, day, unit, decimal, round){ //NB this function is also copied in the admin area
    var one_day=1000*60*60*24
    var one_month=1000*60*60*24*30
    var one_year=1000*60*60*24*30*12
    
    today=new Date()
    var pastdate=new Date(yr, mon-1, day)

    var countunit=unit
    var decimals=decimal
    var rounding=round

    finalunit=(countunit=="days")? one_day : (countunit=="months")? one_month : one_year
    decimals=(decimals<=0)? 1 : decimals*10

    if (unit!="years"){
    if (rounding=="rounddown")
        document.write(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
    else
        document.write(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
    }
    else
    {
        yearspast=today.getFullYear()-yr-1
        tail=(today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0
        pastdate.setFullYear(today.getFullYear())
        pastdate2=new Date(today.getFullYear()-1, mon-1, day)
        tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals
        return (yearspast+tail);
    }
}


function tickCheck(obj) 
{
    if (obj.checked == false) {
        if (document.forms[0].elements["hdnTickCount_Current"].value > 0) {
            document.forms[0].elements["hdnTickCount_Current"].value = parseInt(document.forms[0].elements["hdnTickCount_Current"].value) - 1;
        }       
    }
    else {
        document.forms[0].elements["hdnTickCount_Current"].value = parseInt(document.forms[0].elements["hdnTickCount_Current"].value) + 1;
        if (parseInt(document.forms[0].elements["hdnTickCount_Current"].value) == parseInt(document.forms[0].elements["hdnTickCount_Total"].value)) {
            var tableName;
            for (var i = 0; i < parseInt(document.forms[0].elements["hdnTickTableCount_Total"].value); i++) {
                tableName = 'tblTickCheck' + (parseInt(i) + 1);
                document.getElementById(tableName).style.backgroundColor = '#FFFFFF';
            }
        }
    }
}


function tickCount(obj) 
{

    if (parseInt(document.forms[0].elements["hdnTickCount_Current"].value) < parseInt(document.forms[0].elements["hdnTickCount_Total"].value)) {
        var tableName;
        for (var i = 0; i < parseInt(document.forms[0].elements["hdnTickTableCount_Total"].value); i++) {
            tableName = 'tblTickCheck' + (parseInt(i) + 1);
            document.getElementById(tableName).style.backgroundColor = '#FF9999';
        }

        var currentTicks = document.forms[0].elements["hdnTickCount_Current"].value;
        if (currentTicks == 0) {
            alert("You have not ticked any of the 'I have read' statements highlighted in red, ALL must be ticked to proceed.");
        }
        else {
            alert("Only " + currentTicks + " of the " + document.forms[0].elements["hdnTickCount_Total"].value + " 'I have read' statements highlighted in red are ticked, ALL must be ticked to proceed.");

        }
        return false;
    }
    else {
        return true;
    }
}

function setHiddenClaimsValue(obj)
{
    var claims = 0;
    var rblClaims = document.forms[0].elements["rblClaims"];
    for (var i = 0; i < rblClaims.length; i++) 
   {
       if (rblClaims[i].checked) 
        {
            claims = rblClaims[i].value;
            break;
        }
    }
    document.forms[0].elements["hdnClaims"].value = claims;

}

/*
function zeroNcbIfClaims()
{
//find claims value
var claims = 0;
var rblClaims = document.forms[0].elements["rblClaims"];
    for (var i=0;i<rblClaims.length; i++)
    {
    if(rblClaims[i].checked)
        {
       claims = rblClaims[i].value;
        break;
        }
    }
    //if claims exist then zero NCB
    if (claims!=0) 
    {
        var YearsNoClaims = 0;
        var rblYearsNoClaims = document.forms[0].elements["rblYearsNoClaims"];
            if (rblYearsNoClaims!=undefined)
            {
            rblYearsNoClaims[0].checked = true; //ZERO NCB true
            for (var i=1;i<rblYearsNoClaims.length; i++)
            {
            rblYearsNoClaims[i].checked = false; //all else false
            }
            }
    }
    return claims
}

function zeroNcbIfClaimsYNC()
{
    //called from YearsNoClaims, if claims already set explain that NCB will be zero
    if (zeroNcbIfClaims()>0) 
    {
        alert('Apologies, claims more than 0 result in 0 no claims discount years.');
    }
    //also disable protected bonus if not 6 years NCB
    try
    {
        if (document.forms[0].elements["rblYearsNoClaims_6"].checked == true)
        {
        document.forms[0].elements["rblProtectedNoClaims"].disabled = 0;
                document.forms[0].elements["rblProtectedNoClaims_0"].disabled = 0;
            document.forms[0].elements["rblProtectedNoClaims_1"].disabled = 0;
        }
        else
        {
        
            document.forms[0].elements["rblProtectedNoClaims_0"].checked = 0;
            document.forms[0].elements["rblProtectedNoClaims_1"].checked = 0;
            document.forms[0].elements["rblProtectedNoClaims_0"].disabled = -1;
            document.forms[0].elements["rblProtectedNoClaims_1"].disabled = -1;
            document.forms[0].elements["rblProtectedNoClaims"].disabled = -1;
        }
    }
    catch(err){}
    
}
*/

function addbookmark()
{
bookmarkurl="http://www.MerciaMarine.co.uk"
bookmarktitle="Mercia Marine: Boat insurance specialists"
if (document.all)
window.external.AddFavorite(bookmarkurl,bookmarktitle)
}

function quickQuotes()
{
document.getElementById(qqBig).style.display = 'block';
document.getElementById(qqSmall).style.display = 'none';
}

function checkUnnamed(obj)
{
if(obj.checked==true)
    {
    document.forms[0].txtVesselName.value = 'Unnamed';
    }
}

function checkOtherHasValue()
{
    var OtherValue = document.forms[0].txtOtherValue.value - 0;
    if(parseInt(OtherValue)!=OtherValue-0)/*integer test*/
    {
        document.forms[0].txtOtherValue.value = 0;
    }
}
function checkPersonalEffectsHasValue()
{
    var PersonalEffectsValue = document.forms[0].txtPersonalEffectsValue.value - 0;
    if(parseInt(PersonalEffectsValue)!=PersonalEffectsValue-0) /*integer test*/
    {
        document.forms[0].txtPersonalEffectsValue.value = 0;
    }
}

function checkOtherAndPersonal()
{
    checkOtherHasValue();
    checkPersonalEffectsHasValue();
}

function categoryNotEnabledAtRenewal(obj)
{
alert('You cannot change vessel types at renewal. Instead you would need to raise a new quote/ contact Mercia Marine.');
}

/*function checkSupportOptions(obj)
{
    var ddl1value = obj.options[obj.selectedIndex].value; 
    switch (ddl1value) 
    {
    case '1'||'2':
        document.forms['form1'].elements('rblBreakdown_1').text = 'tttt';
        document.forms['form1'].elements('rblBreakdown_1').display = 'none';
        //document.forms['form1'].elements('lblCoverType').display = 'none';
        document.forms['form1'].elements('rblCoverType_1').display = 'none';
        document.forms['form1'].elements('txtVesselValue').display = 'none';
        document.forms['form1'].elements('txtVesselValue').enabled = false;
        document.forms['form1'].elements('txtVesselValue').display = false;
       
        
        //document.forms[0].getElementById('rblBreakdown_2').enabled = true;
	    break
    case '3'||'4'||'5'||'6'||'7'||'8'||'9':
        //document.forms[0].getElementById('rblBreakdown_1').enabled = true;
        document.forms['form1'].elements('rblBreakdown_2').display = false;
	    break
    default: 
	    break    
	}
}
*/

function max10percentPersonalEffects()
{
var vV; 
var cV;
vV = document.forms[0].txtVesselValue.value;
cV = document.forms[0].txtPersonalEffectsValue.value;
if (vV*0.1 < cV){
        document.forms[0].txtPersonalEffectsValue.value = vV * 0.1;
        alert('Your personal effects value has been reduced as 10% of vessel value is the maximum.');
    }
if (cV==""||cV==null){
    document.forms[0].txtPersonalEffectsValue.value = '0';
    }
}

function max10percentOtherValue()
{
var vV; 
var cV;
vV = document.forms[0].txtVesselValue.value;
cV = document.forms[0].txtOtherValue.value;
if (vV*0.1 < cV){
        document.forms[0].txtOtherValue.value = vV * 0.1;
        alert('Your other items value has been reduced as 10% of vessel value is the maximum.');
    }
if (cV==""||cV==null){
    document.forms[0].txtOtherValue.value = '0';
    }
}

function maxMSSRreplacement()
{
var hull; 
var mssr; 
hull = document.forms[0].txtVesselValue.value;
mssr = document.forms[0].txtRiggingSparsSailsValue.value;

if (hull*0.6 < mssr){
        document.forms[0].txtRiggingSparsSailsValue.value = hull * 0.6;
        alert('Your total mast, sails, spars and rigging value cannot be more than 60% of vessel value.');
    }
if (mssr==""||mssr==null){
    document.forms[0].txtRiggingSparsSailsValue.value = '0';
    }
}


function displayOtherValues()
{
//dumb used to make an ajax control text for additional value items appear/behave like a hyperlink but it works!
}

function max10percent()
{
/*
## To DO - need to test for nulls, to prevent javascript throwing a wobbly! 
    if (String(document.forms[0].txtPersonalEffectsValue.value).length > 0){
        max10percentPersonalEffects();
    }
    if (String(document.forms[0].txtOtherValue.value).length > 0){
        max10percentOtherValue();
    }
*/
}