function doLogin(URL, params)
{
    http_request = null;
    if(window.XMLHttpRequest) http_request = new XMLHttpRequest();
    else if(window.ActiveXObject) http_request = new ActiveXObject("Microsoft.XMLHTTP");
    if(http_request==null) return; // Failed to create the request

    http_request.onreadystatechange = function()
    {
        switch(http_request.readyState)
        {
        case 0:
            break;
        case 1:
            break;
        case 2: 
            break;
        case 3: 
            break;
        case 4:
        showResult(http_request.responseXML.getElementsByTagName('result')[0].firstChild.data);
        if(http_request.responseXML.getElementsByTagName('custid')){
        	setCustID(http_request.responseXML.getElementsByTagName('custid')[0].firstChild.data);
        }
            break;
        default:
            break;
        }
    }

    //xmlReq.open ('GET', URL, true);
    //xmlReq.send (null);
    
    http_request.open('POST', URL, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", params.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(params);
}

function setCustID(id){
	custid = id;
	c_id = id;
}

function strpos(haystack, needle, offset){ 
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

function substr(f_string, f_start, f_length){
 
    f_string += '';
 
    if (f_start < 0) {
        f_start += f_string.length;
    }
 
    if (f_length == undefined) {
        f_length = f_string.length;
    } else if (f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if (f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}

function showResult(result){
	
	if(strpos(result,"OK")!==false){
		//login successful
		var cust_name = substr(result,2);
		doWelcome(cust_name);
	}else {
	if(result=="pwdsent"){
		//forgotten pwd sent successfully
		doPwdSent();
	}else{
		//error
		document.getElementById('err').innerHTML = result; //use this for more precise error messages
		//document.getElementById('err').innerHTML = "please check your log in details and try again";
	}
}
}

function processLogin(){
	
	//get POST string from form
	var act = "action=process";
	var email_address = "email_address=" + escape(encodeURI(document.getElementById("aj_email_ad").value));
	var pwd = "pwd=" + escape(encodeURI(document.getElementById("aj_pwd").value));

//alert("inside aj_contact_us.js");

	var params_str = act + "&" + email_address + "&" + pwd;
//alert(params_str);	
	doLogin('aj_login/aj_login.php', params_str);
}

function processForgotPwd(){
	
	var email_address = "email_address=" + escape(encodeURI(document.getElementById("forgot_email_ad").value));
	var act = "action=forgot_email";
	
	var params_str = act + "&" + email_address;
//alert(params_str);	
	doLogin('aj_login/aj_login.php', params_str);
}

function doForgottenPwd(){
	
	var htmlForgottenPwd = "<div style='padding-top:65px;' id='forgot_id' name='forgot_id'><div>email address</div><div style='position:relative;left:-2px;'><input type='text' name='forgot_email_ad' id='forgot_email_ad'  size='35' /></div><div style='font-size:0.8em;padding-top:5px;'>Please enter your email address and click 'submit >'.<br/>You will then be sent a password reminder email.</div><div class='forward' style='padding-top:5px;'><img onClick='processForgotPwd();' style='cursor: pointer; cursor: hand;' src='images/alwaysriding/new_buttons/submit.gif' alt='Submit' title='Submit' /></div><br class='clearBoth' /><div id='err' style='padding-top:5px;width:100%;color:#CA0000;font-size:0.8em;text-align:right;'></div></div>";
	
	document.getElementById('login_content').innerHTML = htmlForgottenPwd;
	
}

function doPwdSent(){
	var htmlPwdSent = "<div style='padding-top:65px;' id='sent_pwd' name='sent_pwd'><div style='color:#0c87d8;font-weight:bold;'>password reminder sent</div><div style='padding-top:5px;'>This should arrive in your inbox shortly</div><div style='padding-top:25px;'><img onClick='showLoginPage();' style='cursor: pointer; cursor: hand;' src='images/alwaysriding/new_buttons/btn_on_grey_back_to_log-in.gif' alt='Back to Login' title='Back to Login' /></div></div>";
	
	document.getElementById('login_content').innerHTML = htmlPwdSent;
}

function showLoginPage(){	
	document.getElementById('login_content').innerHTML=aj_login_page;
}

function doWelcome(cust_name){
	var htmlWelcome = "<div style='padding-top:130px;color:#0c87d8;font-weight:bold;text-align:center;' id='welcome' name='welcome'>Hey&nbsp;" + cust_name + ", how's it going?<br/>Welcome back.</div><div class='forward' style='padding-top:15px;'><img onClick='doShopLoggedIn();' style='cursor: pointer; cursor: hand;' src='images/alwaysriding/new_buttons/btn_on_grey_continue.gif' alt='Back to Shop' title='Back to Shop' /></div>";
	
	document.getElementById('login_content').innerHTML = htmlWelcome;
}

function doShopLoggedIn(){
	//alert("back to shop now, but logged in");
	if(current_page=='shopping_cart'){
		document.getElementById('light').style.display='none';
		document.getElementById('fade').style.display='none';
		window.location.reload();
	}else{
	
		if((current_page=='product_info')&&(typeof gotoTags!="undefined")&&(gotoTags==1)){
			var page_load = "http://www.alwaysriding.co.uk/index.php?main_page=product_info&products_id=" + gotoPid + "#tag_section";
			window.location.href = page_load;
			return false;
		}
		
		if(typeof loadData == 'function') { 
			loadData('http://www.alwaysriding.co.uk/aj_update_cart.php');//update dynabasket
			document.getElementById('login_li').innerHTML='<span style="position:relative;top:0px;"><a style="font-size:12px;color:#4c4c55;" href="https://www.alwaysriding.co.uk/index.php?main_page=logoff">log out</a>&nbsp;|&nbsp;<a style="font-size:12px;color:#4c4c55;" href="https://www.alwaysriding.co.uk/index.php?main_page=my_account">my account</a></span>';
		}else{
			document.getElementById('login_li').innerHTML='';
		}
		
		if(writeReviewLogin==1){
			if(document.getElementById('write_review')){
				document.getElementById('write_review').style.display='block';
			}
			if(document.getElementById('read_reviews')){
				document.getElementById('read_reviews').style.display='none';
			}
			reviewsLoClicked();
			window.location.hash = "#BPRWrapper";
			writeReviewLogin=0;
		}
		
		if(current_page=='product_info'){
			if(document.getElementById("add_tag_link")){
				//document.getElementById("add_tag_link").setAttribute("onclick","tag_form.submit();return false;");
				document.getElementById("add_tag_link").setAttribute("onclick","doSubmit();return false;");
			}
			
			if(document.getElementById("check0")){
				document.getElementById("check0").setAttribute("onclick","doCheckboxSubmit(this);");
			}
			if(document.getElementById("check1")){
				document.getElementById("check1").setAttribute("onclick","doCheckboxSubmit(this);");
			}
			if(document.getElementById("check2")){
				document.getElementById("check2").setAttribute("onclick","doCheckboxSubmit(this);");
			}
			if(document.getElementById("check3")){
				document.getElementById("check3").setAttribute("onclick","doCheckboxSubmit(this);");
			}
		}
		
		document.getElementById('light').style.display='none';
		document.getElementById('fade').style.display='none';
	}
}