//****************************************************************************************************************
//** Page Name		: scripts.js
//** Description	: Client side scripting functions
//** Comment			: 
//** Author				: Jason Sammon
//** Last Revised	: 08/11/2005
//****************************************************************************************************************


//this is an email enkoder from Hiveware http://www.hiveware.com
function emailAddress()
{var kode=
"kode=\"nrgh@%>,**=,40kwjqho1hgrn+wDudkf1hgrnBkwjqho1hgrn?l+.{@hgrn\\000,l+"+
"wDudkf1hgrn.,4.l+wDudkf1hgrn@.{~,5@.l>,40kwjqho1hgrn+?l>3@l+uri>**@{>_%{h@"+
"rg\\000nf,h+rguFkdpFur1iqjulVw.@>{5;@4f.3,f?i+>l06l,w+hDrguFkd1fghnrf@,~.."+
">lwkqjohh1rg?n>l@3+lru>i**{@_%__>-/.-otpu/4k.xy|kxk/4--z.royvk4ujCqjkqu(Au"+
"qkj(CtbmxFnp+o{y\\001zq:\\00033____3u~q\\000.4mHt,q~Ir.hmyxu{\\000qF}zu\\0"+
"01u~\\177q\\000Lqtp9nq9\\000xozuouo::{w\\001.h\\000,\\000uqxhIq.}zu\\001u~"+
"\\177q\\000Lqtp9nq9\\000xozuouo::{w\\001.hqJ}zu\\001u~\\177q\\000Lqtp9nq9"+
"\\000xozuouo::{w\\001;HJm5.+G\\001D0FD0xo1{FrD9Erxtnmu7wn}pDq4r2434____3Fl"+
"xtnml7jqL{mxJn1}2r<6rD1oEl294l:FA;\\001DF4}\\\\r{pwo7x{LvjqL{mx1n2lbb66t6m"+
"xFn(\\001AbC~--lAxuo.6CoAqBju4kkrmtnzoA11\\001/Ciuqkji4gnIxjuGk.z/o93oA.lB"+
"i/61i7C>8~AC1zYoxmtl4uxIsgnIxju.k/i33____3uqkj~CC(jkqu@_%__ghnr_%@hgrn%>nr"+
"gh@nrgh1vsolw+**,1uhyhuvh+,1mrlq+**,\";x='';for(i=0;i<kode.length;i++){c=k"+
"ode.charCodeAt(i)-3;if(c<0)c+=128;x+=String.fromCharCode(c)}kode=x"
;var i,c,x;while(eval(kode));
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

// Checks call back form
// Jason Margolin - 30/05/07
function checkSideFormNoThanks()
{
    document.getElementById('cancel_upsell').value = '1';
    return true;
}

function checkSideForm() 
{
    var reqFields = ['firstName', 'surname', 'address', 'postcode', 'phonenumber', 'totalpayingpermonth', 'mothlycosts', 'ema', 'totaldebt', 'companies', 'totaldebt', 'day', 'month', 'YYYY', 'borrowamount', 'homevalue', 'debtvalue','besttime'];
    var phoneRegex = /(\+?)(\(?)(\d{1,3}?)(\)|\s|\-?)(\(?)(\d{1,5})(\)?)(\s|\-?)(\d{3,4})(\s|\-?)(\d{3,4})/
    for (var i = 0; i < reqFields.length; i++) 
    {
        if ((document.getElementById('contact_'+reqFields[i]) != "undefined") && (document.getElementById('contact_'+reqFields[i]) != null))
        {
            if (document.getElementById('contact_'+reqFields[i]).value == '') 
            {
                alert('Please fill in all of the fields.');
                return false;
            }
        }
    }

    if (document.getElementById('contact_phonenumber'))
    {
        if (!document.getElementById('contact_phonenumber').value.match(phoneRegex)) 
        {
            alert('Please enter a vaild telephone number.');
            return false;
        }
    }
    
    if (document.getElementById('contact_mobilenumber'))
    {
        if (!document.getElementById('contact_mobilenumber').value.match(phoneRegex)) 
        {
            alert('Please enter a vaild mobile number.');
            return false;
        }
    }

    if (document.getElementById('contact_ema'))
    {
        if (!isValidEmail(document.getElementById('contact_ema').value)) 
        {
            alert('Please enter a vaild email address.');
            return false;
        }
    }
    
    if (document.getElementById('contact_remortterms'))
    {
        if (!document.getElementById('contact_remortterms').checked)
        {
            alert('You must accept our terms before proceeding.');
            return false;
        }
    }
    
    // Submit the form
    document.getElementById('contact_submit').value = '1';
    return true;
    //document.sideForm.submit();    
}

// Checks Debt Calculator Form
// Jason Sammon - 06/06/07
function checkDebtCalcForm() 
{
    var reqNumericFields = ['txtYourIncome','txtOtherIncome','txtMortgage','txtRent','txtCouncilTax','txtWater','txtElectricity','txtGas','txtTelephone','txtMobile','txtInternetTelevision','txtHouseping','txtCarPetrol','txtCarInsurance','txtCarService','txtCarRoadTax','txtCarHire','txtPublicTransport','txtChildCare','txtSmoking','txtMaintenance','txtOtherExpenses1','txtCreditCards','txtLoanRepayment','txtOtherCreditors'];
    
    var bFormHasErrors = false;
    for (var i = 0; i < reqNumericFields.length; i++) 
    {
        var strName = reqNumericFields[i];
        if ((document.getElementById(strName) != "undefined") && (document.getElementById(strName) != null))
        {
            var nValue = document.getElementById(strName).value;
            var bIsErrored = false;
            if (nValue != '') 
            {
                if (isNaN(nValue))
                {
                    bIsErrored = true;                                        
                }
            }    
            
            var strError = "";
            strError = (bIsErrored ? "Value is not numeric" : "");                        
            
            // Do we have a numeric vaue in this field?
            if ((document.getElementById(strName + "Error") != "undefined") && (document.getElementById(strName + "Error") != null))           
            {
                document.getElementById(strName + "Error").innerText = strError;
                if (bIsErrored)
                {
                    document.getElementById(strName).focus();
                }
            }
            
            // Does the form have any errors at all?
            if (bIsErrored)
                bFormHasErrors = true;
        }               
    }
    
    // Quit here if we already have errors
    if (bFormHasErrors)
    {
        return(!bFormHasErrors);
    }
    
    // Check any required fields
    if ((document.getElementById("txtYourIncome") != "undefined") && (document.getElementById("txtYourIncome") != null))           
    {
        if (document.getElementById("txtYourIncome").value == "")
        {
            if ((document.getElementById("txtYourIncomeError") != "undefined") && (document.getElementById("txtYourIncomeError") != null))           
            {
                document.getElementById("txtYourIncomeError").innerText = "Field is required";
                document.getElementById("txtYourIncome").focus();
                bFormHasErrors = true;
            }
        }
    }
    
    if (!bFormHasErrors)
    {
        document.debtCalcForm.submit();
    }
    else
    {
        return(!bFormHasErrors); 
    }    
}