﻿  
function doc(id)
{
	var ie	= (document.all);
	var ns4	= document.layers ? true : false;
	var dom	= document.getElementById && !document.all ? true : false;

	if (dom)
		return document.getElementById(id);
	else if (ie)
		return document.all[id];
	else if(ns4)
		return document.layers[id];
}


function jointForm(thisForm, elementId)
{
    if (doc(elementId).checked) 
    {
	    for (var i=0; i<doc(thisForm).elements.length; i++) {
		    if (doc(thisForm).elements[i].disabled ) 
		    {
	            doc(thisForm).elements[i].disabled = false;
	        }
	    }
	}
 }
 
 function singleForm(thisForm)
 {
    doc("part_name").disabled = true;
    doc("part_email").disabled = true;
    doc("part_gender_m").disabled = true;
    doc("part_gender_f").disabled = true;
    doc("part_dob").disabled = true;
 }
 
function onlyNumbers(evt) 
{
    // keyCode      Key
    // --------------------------------
    // 48/58        0/9
    // 8            Backspace
    var e = evt;
    var charCode = e.which || e.keyCode;
    var valid = false;
    
    if (charCode >= 48 && charCode <= 58 || charCode==8) valid = true;
    return valid;
}
 
function validate_form() {
    
    var errString = "";
    var PartnerError = "";
    var valid = true;
    
    var txtContribution = (doc("mortgage_amount2")!=null) ? doc("mortgage_amount2").value : "NULL";
    var chkSingleJoint = (doc("single_joint_j")!=null) ? doc("single_joint_j").checked : "NULL";
    var dob = (doc("dob_mortgage_amount2")!=null) ? doc("dob_mortgage_amount2").value : "NULL";
    var postCode = (doc("Post_Code")!=null) ? doc("Post_Code").value : "NULL";


    if (txtContribution!="NULL") { if (txtContribution == "") errString += "\n Size of contribution"; }
    if (doc("name").value == "") errString += "\n Name";
    if (doc("email").value == "") errString += "\n Email";
    if (doc("email").value != "" && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(doc("email").value))) errString += "\n Email address: incorrect format";
    if (dob!="NULL") { if (dob == "") errString += "\n Date of Birth";}
    if (postCode!="NULL") { if (postCode == "") errString += "\n Post Code";}

    
    
    if (chkSingleJoint!="NULL") {
        if (chkSingleJoint) {
            if (doc("part_name").value == "") PartnerError += "\n Name";
           // if (document.getElementById("part_email").value == "") PartnerError += "\n email";
           // if (doc("part_email").value != "" && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(doc("part_email").value))) PartnerError += "\n Email address: incorrect format";
            if (doc("part_dob").value == "") PartnerError += "\n Date of Birth";
       
        }
    }
    
    if (errString!="" || PartnerError!="") {
        valid = false;
        var OutputMessage = null;
        
        OutputMessage = "Please complete the following fields:" + errString; 
        if (PartnerError!="") OutputMessage += "\n\nPlease complete partner details" + PartnerError;
        
        alert(OutputMessage);
    }
    
    return valid;
}

function radioToggle(rId, rIdValue, rCount, rCount2Tier) {
    var answerIds = rId.replace("_no","")
    answerIds = answerIds.replace("_yes","") 
    if(rIdValue == "Yes") {
        for(var i=1;i<=rCount;i++) {
            if(doc(answerIds+'_yes_'+i)) {
                doc(answerIds+'_yes_'+i).disabled = false;  
            }
        }
        if(rCount2Tier > 0) {
            for(i=1;i<=rCount2Tier;i++) {
                if(doc(answerIds+'_yes2_'+i)) {
                    doc(answerIds+'_yes2_'+i).disabled = false;  
                }
            }
        }
    }
    else {
        for(var j=1;j<=6;j++) {
            if(doc(answerIds+'_yes_'+j)) {
                doc(answerIds+'_yes_'+j).disabled = true;  
            }
        }
        if(rCount2Tier > 0) {
            for(i=1;i<=rCount2Tier;i++) {
                if(doc(answerIds+'_yes2_'+i)) {
                    doc(answerIds+'_yes2_'+i).disabled = true;  
                }
            }
        }
   }
}

function validateASPXform(mode,step) {
    switch(step) {
        case "ss1":
            //just check email is present
            if (doc("email_pc1").value == "" || !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(doc("email_pc1").value))) {
                alert("Please ensure you provide a valid email address.")
                doc("email_pc1").focus();
                return false;
            }
    }
    //return false; //leave on to test without submission
}

function singleJointHandler(pc, frm) {
    var boolState = true; //assume single, disabled to be true
    if(pc=="pc2") { //joint
        boolState = false;
    }
    try { //frm is either PLA or EI at the mo, certain ones dont exist on PLA, so useing frm==EI on some only
        doc("titlepc2_mr").disabled = boolState;    
        doc("titlepc2_mrs").disabled = boolState;    
        doc("titlepc2_miss").disabled = boolState;    
        doc("titlepc2_ms").disabled = boolState;    
        doc("titlepc2_other").disabled = boolState;
        doc("title_other_pc2").disabled = boolState;
        doc("genderpc2_male").disabled = boolState;
        doc("genderpc2_female").disabled = boolState;
        doc("surname_pc2").disabled = boolState;
        doc("forenames_pc2").disabled = boolState;
        doc("dob_dd_pc2").disabled = boolState;
        doc("dob_mm_pc2").disabled = boolState;
        doc("dob_yyyy_pc2").disabled = boolState;
        //doc("ni_pc2").disabled = boolState;
        if(frm=="PLA"){doc("smokerstatuspc2_yes").disabled = boolState;}
        if(frm=="PLA"){doc("smokerstatuspc2_no").disabled = boolState;}
        if(frm=="PLA"){doc("smokerstatuspc2_never").disabled = boolState;}
        if(frm=="EI"){doc("nationality_pc2").disabled = boolState;}
        doc("maritalstatuspc2_single").disabled = boolState;
        doc("maritalstatuspc2_marriedcivilpartnership").disabled = boolState;
        doc("maritalstatuspc2_separated").disabled = boolState;
        doc("maritalstatuspc2_divorced").disabled = boolState;
        doc("maritalstatuspc2_widowed").disabled = boolState;
        if(frm=="EI"){doc("dependant_relationship").disabled = boolState;}
        if(frm=="EI"){doc("occupation_pc2").disabled = boolState;}
        if(frm=="EI"){doc("occupationtypepc2_full-time").disabled = boolState;}
        if(frm=="EI"){doc("occupationtypepc2_part-time").disabled = boolState;}
        if(frm=="EI"){doc("prev_occupation_pc2").disabled = boolState;}
        if(frm=="EI"){doc("podc_dd_pc2").disabled = boolState;}
        if(frm=="EI"){doc("podc_mm_pc2").disabled = boolState;}
        if(frm=="EI"){doc("podc_yyyy_pc2").disabled = boolState;}
        //if(frm=="EI"){doc("homeaddress_line1_pc2").disabled = boolState;}
        //if(frm=="EI"){doc("homeaddress_line2_pc2").disabled = boolState;}
        //if(frm=="EI"){doc("homeaddress_line3_pc2").disabled = boolState;}
        doc("postcode_pc2").disabled = boolState;
        if(frm=="EI"){doc("daytime_telephone_pc2").disabled = boolState;}
        if(frm=="EI"){doc("evening_telephone_pc2").disabled = boolState;}
        doc("email_pc2").disabled = boolState;
    }
    catch(err) {
    }
}