﻿  
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;
}
