function popup( strURL, intW, intH, strName )

{var obwindow = window.open( strURL, strName, 'width='+intW+',height='+intH+',resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1' )}

function popup3( strURL, intW, intH, strName ) 
{return popup( strURL, intW, intH, strName );}

function popup2( strMsg, strURL, intW, intH, strName ) 
{if (confirm(strMsg)) {var obwindow = window.open( strURL, strName, 'width='+intW+',height='+intH+',resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,scrollbars=1' );obwindow.focus();obwindow.moveTo(50,50);}}

 function ValidateForm(TheForm){
	var msg="-----------------------------------------------\n     * Email address validation error\n-----------------------------------------------\n";
	var msg2="\nYou must include an accurate email address for a response.\n\nPlease verify that your email address is valid.";
	var msg3="\n\nEmail address should be in the following format: \nexample@example.com"

	if (TheForm.email) {
	    if (EmailCheck(trimString(TheForm.email.value))){
		    if ( TheForm.PostCode ) {if ( trimString(TheForm.PostCode.value).length > 0 ) {if ( validPostcode(TheForm.PostCode) ) {return true;} else {return false;}}} else {return true;}
		    }else{alert(msg + msg2 + msg3);TheForm.email.value="";TheForm.email.focus();return false;}
	}else{
	    if (TheForm.Email) {
	        if (EmailCheck(trimString(TheForm.Email.value))){
		        if ( TheForm.PostCode ) {if ( trimString(TheForm.PostCode.value).length > 0 ) {if ( validPostcode(TheForm.PostCode) ) {return true;} else {return false;}}} else {return true;}
		        }else{alert(msg + msg2 + msg3);TheForm.Email.value="";TheForm.Email.focus();return false;}
	    }
	}
 }

function EmailCheck(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }

function trimString(strString){
	//trim trailing spaces
	while (strString.charAt(strString.length-1)==' ')
	{strString=strString.substring(0,strString.length-1);}
	//trim leading spaces
	while (strString.charAt(0)==' ')
	{strString=strString.substring(1,strString.length);}
	return strString;
}

function borders ()
{//Temp Fix //Len is too long
//var d=document.all;var l=d.length;for ( var i=0; i < l ; i++)if(d[i].tagName=='DIV'){s=d[i].style;if(s.p){void(s.border=s.p);void(s.p=null)}else{void(s.border='0px');}}
}

function validateCheckCalc( frm ){
	if (!validPostcode(frm.postcode)) {return false;}else{
		//check for valid radio selection
		var go = false;
		if (frm.PTID) {	
			var i = 0;
			for (i = 0; i < frm.PTID.length; i++){if (frm.PTID[i].checked) {go = true;}}
		}else{go = true;}
		if (go) {frm.btn_submit.disabled=true; frm.btn_submit.value='wait...';return true;
		}else{alert('Please select an option');return false;}
	}
}

function popup4( strURL, intW, intH, strName )

{var obwindow = window.open( strURL, strName, 'width='+intW+',height='+intH+',resizable=yes,toolbar=no,location=yes,directories=no,status=no,menubar=yes,scrollbars=1' )}

function cOn ( strID ){
	document.getElementById(strID).style.backgroundColor='#f7f7f7';
}

function cOff ( strID ){
	document.getElementById(strID).style.backgroundColor='#fff';
}

function validateEmailFriend(form) {
	var error = "";

			if (form.email1) {
				if (trimString(form.email1.value) == "") {error += "* Please enter an email address in E-mail One. \n";} else {
					if ( EmailCheck( trimString(form.email1.value) ) == false ) {error += "* Please enter a valid email address in E-mail One. \n";}
				}
			}

			if (form.email2) {
				if (trimString(form.email2.value) != "") {
					if ( EmailCheck( trimString(form.email2.value) ) == false ) {error += "* Please enter a valid email address in E-mail Two. \n";}
				}
			}
			
			if (form.email3) {
				if (trimString(form.email3.value) != "") {
					if ( EmailCheck( trimString(form.email3.value) ) == false ) {error += "* Please enter a valid email address in E-mail Three. \n";}
				}
			}

			if (form.yourname) {
				if ( trimString(form.yourname.value) == "" ) {error += "* Please enter your name. \n";} else {
					if ( trimString(form.yourname.value) < 2 ) {error += "* Your name must be at least 2 characters long. \n";} else {
					if ( trimString(form.yourname.value) > 50 ) {error += "* Your name must be no more than 50 characters long. \n";}
				}
				}
			}
			
			if (form.youremail) {
				if (trimString(form.youremail.value) == "") {error += "* Please enter your email address. \n";} else {
					if ( EmailCheck( trimString(form.youremail.value) ) == false ) {error += "* Please enter a valid email address in Your E-mail. \n";}
				}
			}
			
			if (form.yourmessage) {
				if ( trimString(form.yourmessage.value) != "" ) {
					if ( trimString(form.yourmessage.value).length < 10 ) {error += "* Your message must be at least 10 characters long. \n";} else {
					if ( trimString(form.yourmessage.value).length > 300 ) {error += "* Your message must be no more than 300 characters long. \n";}
				}
				}
			}
			
			if (error != "")
			{
				var msg="-------------------------------------------------------------------\n                      Form validation error\n-------------------------------------------------------------------\n";
				alert(msg + error);
				return (false);
			} else {
				return confirm('Are you sure?');
			}

}

function cc_submitlogin(frm)
{
  
  if (document.getElementById("username")) {
    var username = document.getElementById("username").value;
  }else{
    return false;
  }
    
  if (document.getElementById("password")) {
    var password = document.getElementById("password").value;
  }else{
    return false;
  }
  
  //var username = document.getElementById("username").value;
  //var password = document.getElementById("password").value;
  
  if(document.cc_login.site.value == '1')
  {
   document.cc_login.action ="http://forum.consumerchoices.co.uk/login.asp";
   document.cc_login.name.value = username;
  }
  else if(document.cc_login.site.value == '2')
  {
    document.cc_login.action ="http://blog.consumerchoices.co.uk/wp-login.php";
    document.cc_login.log.value = username;
    document.cc_login.pwd.value = password;
  }
  return document.cc_login.submit();
}

function validateCheckEnergyCalc( frm ){
	if (!validPostcode(frm.p)) 
	    {
	        return false;
	    }
	else
	{
		//check for valid radio selection
		var go = false;
		if (frm.PTID) 
		{	
			var i = 0;
			for (i = 0; i < frm.PTID.length; i++)
			{
			    if (frm.PTID[i].checked) 
			    {
			        go = true;
			    }
			}
		}
		else
		{
		    go = true;
		}
		if (go) 
		{
		    if (frm.quote.checked)
		    {
		        popup('popupform.asp?popup=1&pageid=makeitcheaper', 550, 800, 'info');
		        return false;
		    }
		    else
		    {
		        //frm.btn_submit.disabled=true; frm.btn_submit.value='wait...';return true;
		    }
		}
		else
		{
		    alert('Please select an option');return false;
		}
	}
}

function validateCheck(formval){
    var bErr = false;

    if (formval.telnum) {if ( trimString(formval.telnum.value).length != 0){if (!validPhoneNumber(formval.telnum)){bErr = true;}}}
    if (!validPostcode(formval.postcode)){bErr = true;}
	    if (bErr == false) {
		    //formval.btn_submit.disabled=true; 
		    if (formval.btn_submit.value.length > 6) {formval.btn_submit.value='SEARCHING...'}
			    return true;
	    }else {return false;}
}

function validateCalcResults(form) {
	var error = "";
	if (form.emailcalcname) {
		if ( trimString(form.emailcalcname.value) == "" ) {error += "* Please enter your name. \n";} else {
			if ( trimString(form.emailcalcname.value) < 2 ) {error += "* Your name must be at least 2 characters long. \n";} else {
			if ( trimString(form.emailcalcname.value) > 50 ) {error += "* Your name must be no more than 50 characters long. \n";}
		}
		}
	}
	
	if (form.emailcalcaddress) {
		if (trimString(form.emailcalcaddress.value) == "") {error += "* Please enter your email address. \n";} else {
			if ( EmailCheck( trimString(form.emailcalcaddress.value) ) == false ) {error += "* Please enter a valid email address. \n";}
			
		}
	}
	
	if (form.emailcalcaddressconfirm) {
	    if (trimString(form.emailcalcaddress.value) != trimString(form.emailcalcaddressconfirm.value)) {error += "* Please ensure both email addresses match. \n";}
	}

	if (error != "")
	{
		var msg="-------------------------------------------------------------------\n                      Form validation error\n-------------------------------------------------------------------\n";
		alert(msg + error);
		return (false);
	} 
}

function validateRegStep1(form)
{
    var error = "";
    
    if (form.email) {
	var checkEM = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	if ( !form.email.value.match(checkEM) ) {error += "* Please enter a valid email address\n"}
	if (form.email.value != form.confirmEmail.value) {error += "* The email and confirm email fields don't match\n"}
    }
    
    if (form.password) {
	var checkEM = /^[a-zA-Z0-9]+$/;
	if ( !form.password.value.match(checkEM) ) {error += "* Please enter a valid password containing letters or numbers only.\n"}
	if (form.password.value != form.confirmPassword.value) {error += "* The password and confirm password fields don't match\n"}
    }
    
    var checkName = /^[a-zA-Z- ]+$/;
    
    if (form.firstName) {
    if (!form.firstName.value == ""){
    if ( !form.firstName.value.match(checkName) ) {error += "* Your FIRST NAME must not contain any characters other than a-z and hyphen.\n"}}
    else {error += "* Please enter a FIRST NAME.\n"}
    }

    if (error != "")
    {
	    var msg="-------------------------------------------------------------------\n                      Form validation error\n-------------------------------------------------------------------\n";
	    alert(msg + error);
	    return (false);
    } else {
	    return (true);
    }	
}

function validateRegStep2(form)
{
    var error = "";
    
    var checkName = /^[a-zA-Z- ]+$/;
    
    if (form.firstName) {
    if (!form.firstName.value == ""){
    if ( !form.firstName.value.match(checkName) ) {error += "* Your FIRST NAME must not contain any characters other than a-z and hyphen.\n"}}
    else {error += "* Please enter a FIRST NAME.\n"}
    }
    
    if (form.surName) {
    if (!form.surName.value == ""){
    if ( !form.surName.value.match(checkName) ) {error += "* Your SURNAME must not contain any characters other than a-z and hyphen.\n"}}
    else {error += "* Please enter a SURNAME.\n"}    
    }
    
    if (form.postcode) { if (validPostcodeReg(form.postcode).length != 0){error += "* " + validPostcodeReg(form.postcode) + "\n";}}
    
    if (error != "")
    {
	    var msg="-------------------------------------------------------------------\n                      Form validation error\n-------------------------------------------------------------------\n";
	    alert(msg + error);
	    return (false);
    } else {
	    return (true);
    }	
}

function validateRegStep3(form)
{
    var error = "";
    if (form.Income) 
    {

        if (!form.Income.value == "")
        {
            if (isNaN(form.Income.value)) {error += "* The income entered must be a valid number of pounds.\n";}
        }
    }  
    if (error != "")
    {
	    var msg="-------------------------------------------------------------------\n                      Form validation error\n-------------------------------------------------------------------\n";
	    alert(msg + error);
	    return (false);
    } else {
	    return (true);
    }	

}

function validateRegStep4(form)
{
    var error = "";
    if (!form.broadbandISPMonth.value == "" || !form.broadbandISPYear.value == "" ) 
    {
        if (form.broadbandISPMonth.value == "" || form.broadbandISPYear.value == "")
        {
            error +='You must enter both the month and year for BROADBAND ISP\n';
        } 
    }

    if (!form.balanceTransferMonth.value == "" || !form.balanceTransferYear.value == "" ) 
    {
        if (form.balanceTransferMonth.value == "" || form.balanceTransferYear.value == "")
        {
            error +='You must enter both the month and year for CREDIT CARD BALANCE TRANSFER\n';
        } 
    }

    if (!form.mortgageDealMonth.value == "" || !form.mortgageDealYear.value == "" ) 
    {
        if (form.mortgageDealMonth.value == "" || form.mortgageDealYear.value == "")
        {
            error +='You must enter both the month and year for MORTGAGE INTRODUCTORY DEAL\n';
        } 
    }

    if (!form.energyDealMonth.value == "" || !form.energyDealYear.value == "" ) 
    {
        if (form.energyDealMonth.value == "" || form.energyDealYear.value == "")
        {
            error +='You must enter both the month and year for ENERGY FIXED RATE DEAL\n';
        } 
    }

    if (!form.homephoneContractMonth.value == "" || !form.homephoneContractYear.value == "" ) 
    {
        if (form.homephoneContractMonth.value == "" || form.homephoneContractYear.value == "")
        {
            error +='You must enter both the month and year for HOME PHONE CONTRACT\n';
        } 
    }

    if (error != "")
    {
	    var msg="-------------------------------------------------------------------\n                      Form validation error\n-------------------------------------------------------------------\n";
	    alert(msg + error);
	    return (false);
    } else {
	    return (true);
    }	
}

function validateLogin (form)
{
    var error = "";
    
    if (form.username) {
	var checkEM = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	if ( !form.username.value.match(checkEM) ) {error += "* Please enter a valid email address\n"}
    }
    
    if (form.password) {
    if (form.password.value == "") {error += "* Please enter your password\n"}
    }
    
    if (error != "")
    {
	    var msg="-------------------------------------------------------------------\n                      Form validation error\n-------------------------------------------------------------------\n";
	    alert(msg + error);
	    return (false);
    } else {
	    return (true);
    }	
}

function validPostcodeReg(frmElement){
/*
There are 6 possible valid UK postcodes: where L = letter and N = Number
LN NLL LLN NLL LNN NLL LNL NLL LLNN NLL LLNL NLL
*/
var pcode = frmElement.value;
var pcType1 = /^[A-Z]{1}\d{1}\s{1}\d{1}[A-Z]{2}/;
var pcType2 = /^[A-Z]{2}\d{1}\s{1}\d{1}[A-Z]{2}/;
var pcType3 = /^[A-Z]{1}\d{2}\s{1}\d{1}[A-Z]{2}/;
var pcType4 = /^[A-Z]{2}\d{2}\s{1}\d{1}[A-Z]{2}/;
var pcType5 = /^[A-Z]{2}\d{1}[A-Z]{1}\s{1}\d{1}[A-Z]{2}/;
var pcType6 = /^[A-Z]{1}\d{1}[A-Z]{1}\s{1}\d{1}[A-Z]{2}/;
var sAlertEmpty = "Please enter a valid UK post code.";
var sAlertLength = "Sorry, but the postcode you entered was too long or too short. " + sAlertEmpty;
var sAlertInvalid = "Sorry, the postcode [" + frmElement.value + "] is not valid. " + sAlertEmpty;
	if (pcode==null||pcode==""){return sAlertEmpty;}
	pcode = formatPostcode(pcode);
	if (pcode.length<6||pcode.length>8){return sAlertLength;}	
	if (pcode.match(pcType1)||pcode.match(pcType2)||pcode.match(pcType3)||pcode.match(pcType4)||pcode.match(pcType5)||pcode.match(pcType6)){
		frmElement.value = pcode; return '';
	}
	else {return sAlertInvalid;}
}

function validateMyAccount(form)
{
    var error = "";
    
    if (form.email) {
	var checkEM = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	if ( !form.email.value.match(checkEM) ) {error += "* Please enter a valid email address\n"}
	if (form.email.value != form.confirmEmail.value) {error += "* The email and confirm email fields don't match\n"}
    }
    
    if (form.password) {
	var checkPS = /^[a-zA-Z0-9]+$/;
	if ( !form.password.value.match(checkPS) ) {error += "* Please enter a valid password containing letters or numbers only.\n"}
	if (form.password.value != form.confirmPassword.value) {error += "* The password and confirm password fields don't match\n"}
    }
    
    var checkName = /^[a-zA-Z- ]+$/;
    
    if (form.firstName) {
    if (!form.firstName.value == ""){
    if ( !form.firstName.value.match(checkName) ) {error += "* Your FIRST NAME must not contain any characters other than a-z and hyphen.\n"}}
    else {error += "* Please enter a FIRST NAME.\n"}
    }
    
    if (form.surName) {
    if (!form.surName.value == ""){
    if ( !form.surName.value.match(checkName) ) {error += "* Your SURNAME must not contain any characters other than a-z and hyphen.\n"}}
    else {error += "* Please enter a SURNAME.\n"}    
    } 
    
    if (form.postcode) { if (validPostcodeReg(form.postcode).length != 0){error += "* " + validPostcodeReg(form.postcode) + "\n";}}
  
    if (form.Income) 
    {

        if (!form.Income.value == "")
        {
            if (isNaN(form.Income.value)) {error += "* The income entered must be a valid number of pounds.\n";}
        }
    }
    
  
    if (!form.broadbandISPMonth.value == "" || !form.broadbandISPYear.value == "" ) 
    {
        if (form.broadbandISPMonth.value == "" || form.broadbandISPYear.value == "")
        {
            error +='You must enter both the month and year for BROADBAND ISP\n';
        } 
    }

    if (!form.balanceTransferMonth.value == "" || !form.balanceTransferYear.value == "" ) 
    {
        if (form.balanceTransferMonth.value == "" || form.balanceTransferYear.value == "")
        {
            error +='You must enter both the month and year for CREDIT CARD BALANCE TRANSFER\n';
        } 
    }

    if (!form.mortgageDealMonth.value == "" || !form.mortgageDealYear.value == "" ) 
    {
        if (form.mortgageDealMonth.value == "" || form.mortgageDealYear.value == "")
        {
            error +='You must enter both the month and year for MORTGAGE INTRODUCTORY DEAL\n';
        } 
    }

    if (!form.energyDealMonth.value == "" || !form.energyDealYear.value == "" ) 
    {
        if (form.energyDealMonth.value == "" || form.energyDealYear.value == "")
        {
            error +='You must enter both the month and year for ENERGY FIXED RATE DEAL\n';
        } 
    }

    if (!form.homephoneContractMonth.value == "" || !form.homephoneContractYear.value == "" ) 
    {
        if (form.homephoneContractMonth.value == "" || form.homephoneContractYear.value == "")
        {
            error +='You must enter both the month and year for HOME PHONE CONTRACT\n';
        } 
    }

    if (error != "")
    {
	    var msg="-------------------------------------------------------------------\n                      Form validation error\n-------------------------------------------------------------------\n";
	    alert(msg + error);
	    return (false);
    } else {
	    return (true);
    }	
}

function switchTab (el) {

    var clickedtab =  el.parentNode.parentNode;
    // set all tabs to default style using jquery
    $(".tab-selected").attr('class', 'tab-default');
    // select clicked tab regardless of what class it is
    clickedtab.className = "tab-selected"
    
    //show hide content
    var tabText = clickedtab.id.replace("tb-","")
    // first hide all panels
    $(".showTab").removeClass('showTab').addClass('hideTab');
    // now show corresponding clicked tab
    $("#dv-" + tabText).removeClass('hideTab').addClass('showTab');
    
    
}