﻿function PasswordSubmit(){
    updatehelp("Please wait ... ");
    var isValid = false;
    var pw1 = document.getElementById("pw");
    var pw2 = document.getElementById("pw2");
    var hint = document.getElementById("hint");
    
    if (isValidPassword(pw1.value) == true){ 
		isValid = true;
    }
    else {
		isValid = false;
		updatehelp('Enter a valid password (space and quote characters are not allowed.)')
		pw1.focus()
		pw1.style.borderColor = "#a60b03"
		return;
    }
  
    if	(pw1.value.length <= 15) {
		 isValid = true;
    }
    else {
		updatehelp('Your password cannot be more than 15 characters long.')
		pw1.focus()
		pw1.style.borderColor = "#a60b03"
		return;
	}
	if (pw1.value.length >= 4) {
		isValid = true;
	}
	else{
		updatehelp('Your password must be at least 4 characters long.')
		pw1.focus()
		pw1.style.borderColor = "#a60b03"
		return;
	}
	if (pw2.value == pw1.value) {
		isValid = true;
    }
    else {
		updatehelp('Your passwords do not match.')
		pw2.focus()
		pw2.style.borderColor = "#a60b03"
		return;
    }
    
    if(hint.value=='')
        updatehelp("You must type a hint in.");
    else if(pw1.value!=pw2.value)
        updatehelp("Your must type your password in twice and they must match.");
    else{
        function failhint(obj) {updatehelp("error setting hint")};
        function sucesshint(obj) {login('/userarea/carquotes.aspx')};
        YAHOO.util.Connect.asyncRequest("POST", "/loginAjaxRequester.aspx", { success: sucesshint, failure: failhint }, "function=sethint&email=" + document.getElementById("email").value + "&pw=" + pw1.value + "&pw2=" + pw2.value + "&hint=" + document.getElementById("hint").value);
    }
}

function updatehelp(t) {
    document.getElementById("helptext").innerHTML = t;
    document.getElementById("helptext").className = "helptext";
}
function showhint() {
	updatehelp('Searching...')
    if(checkEmail2(document.getElementById("email").value) == true){
        makerequest("/loginAjaxRequester.aspx", "function=gethint&email=" + document.getElementById("email").value, "helptext");
    }else{
        updatehelp("You must enter a valid email address to see your hint.");
    }
}

function ChangeLoginText(){
//    var b = document.getElementById("loginbutton").value;
//    document.getElementById("helptext").className = '';
//    if(b.indexOf("PLEASE"))
//    {
//        document.getElementById("loginbutton").value="PLEASE WAIT";
//        document.getElementById("loginbutton").disabled=true;
//    }else{
//        document.getElementById("loginbutton").value="-- Log in ---";
//        document.getElementById("loginbutton").disabled=false;
//    }
}
function login(url) {
	updatehelp("Please wait ... ")
    var isValid = false;
    var email = document.getElementById("email");
	if (email && checkEmail2(email.value)) {
        isValid = true;
    }
    else {
        isValid = false;
        updatehelp('Please enter a valid email address.')
        email.focus()
        email.style.borderColor = "#a60b03"
        return
    }
   var password = document.getElementById("pw");
   if (password && isValidPassword(password.value) == true){ 
		isValid = true;
    }
    else {
		isValid = false;
		updatehelp('Enter a valid password (space and quote characters are not allowed.)')
		document.getElementById('pw').focus()
		document.getElementById('pw').style.borderColor = "#a60b03"
		return;
    }
    if	(password.value.length <= 15) {
		 isValid = true;
    }
    else {
		updatehelp('Your password cannot be more than 15 characters long.')
		document.getElementById('pw').focus()
		document.getElementById('pw').style.borderColor = "#a60b03"
		return;
	}
	if (password.value.length >= 4) {
		isValid = true;
	}
	else{
		updatehelp('Your password must be at least 4 characters long.')
		document.getElementById('pw').focus()
		document.getElementById('pw').style.borderColor = "#a60b03"
		return;
	}
    if(checkEmail2(document.getElementById("email").value) && document.getElementById("pw").value!='') {
        function successHandler2(obj) {
            if(obj.responseText.toString()=="")
            {
                updatehelp("Sorry your details are not valid.");
                ChangeLoginText();
            }else{ 
                if (document.getElementById("checker") && document.getElementById("checker").checked){
                    toMem(document.getElementById("checker"));
                }
                if(document.getElementById("new"))
                {
                    setopacity(document.getElementById("new"),3);
                    document.getElementById("new").style.MozOpacity=0.40;
                }
                if(obj.responseText=='true'){
                    SetCustomer('LoggedInVisitor', url);
                }else if(obj.responseText=='false'){
                    ChangeLoginText();
                    updatehelp("Sorry your login details are not correct.")
                } else if (obj.responseText.indexOf('Hint') > -1) {
                    ChangeLoginText();
                    updatehelp(obj.responseText.toString())
                }
                else if(obj.responseText=='validuser'){
                    ChangeLoginText();
                    //updatehelp("Sorry your login details are not correct but we recognise that you have used the site before so we have emailed you with a link to setup your password.")
                    updatehelp("Sorry your login failed. You are on our database and we have emailed you with a link to setup your password.")
                }
            }
        }
        function failureHandler2(obj) {location.href = "?section=";}
        YAHOO.util.Connect.asyncRequest("POST", "/loginAjaxRequester.aspx", { success: successHandler2, failure: failureHandler2 }, "function=login&email=" + document.getElementById("email").value + "&pw=" + document.getElementById("pw").value);
    }else{
        updatehelp("Please enter your email and password.");
        ChangeLoginText();
    }
}

function sendreminder(){
    if(checkEmail2(document.getElementById("email").value))
    {
        updatehelp("Please wait...");
        ChangeLoginText();
        makerequest("/loginAjaxRequester.aspx", "function=sendreminder&email=" + document.getElementById("email").value, "helptext");       
        ChangeLoginText();
    }else{
        updatehelp("To receive your password you must enter a valid email address.");
        
    }
}

function makerequest(url,q,el) {
    function successHandler(obj) {YAHOO.util.Dom.get(el).innerHTML = obj.responseText;}
    function failureHandler(obj) {location.href = "?section=";}
    YAHOO.util.Connect.asyncRequest("POST", url,{success:successHandler,failure:failureHandler},q);
}

function SetCustomer(customertype, url) {
	if (!url) url = "/generic/userarea/index.aspx";
    function successHandler2(obj) {document.location.href = url; }
    function failureHandler2(obj) {location.href = "?section=";}
    YAHOO.util.Connect.asyncRequest("POST", "/loginAjaxRequester.aspx", { success: successHandler2, failure: failureHandler2 }, "function=" + customertype);
}



function SetCustomerType(url, customertype) {
    function successHandler2(obj) { document.location.href = url; }
    function failureHandler2(obj) { location.href = "?section="; }
    YAHOO.util.Connect.asyncRequest("POST", "/loginAjaxRequester.aspx", { success: successHandler2, failure: failureHandler2 }, "function=" + customertype);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//<!-- Cookie script - Scott Andrew -->
//<!-- Popup script, Copyright 2005, Sandeep Gangadharan --> 
//<!-- For more free scripts go to http://www.sivamdesign.com/scripts/ -->

function newCookie(name,value,days) {
 var days = 1;   // the number at the left reflects the number of days for the cookie to last
                 // modify it according to your needs
 if (days) {
   var date = new Date();
   date.setTime(date.getTime()+(days*24*60*60*1000));
   var expires = "; expires="+date.toGMTString(); }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/"; }

function readCookie(name) {
   var nameSG = name + "=";
   var nuller = '';
  if (document.cookie.indexOf(nameSG) == -1)
    return nuller;

   var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length); }
    return null; }

function eraseCookie(name) {newCookie(name,"",1); }

function toMem(a) {
    newCookie('email', document.getElementById("email").value);     // add a new cookie as shown at left for every
    newCookie('pw', document.getElementById("pw").value);   // field you wish to have the script remember
    newCookie('checker', document.getElementById("checker").value);   // field you wish to have the script remember
}

function delMem(a) {
	eraseCookie('theName');   // make sure to add the eraseCookie function for every field
	eraseCookie('theEmail');
	eraseCookie('checker');
	document.form.name.value = '';   // add a line for every field
	document.form.email.value = ''; 
	document.form.checker.value = ''; 
}