if (window.XMLHttpRequest){
    //mozilla/safari
	xmlHttpRequestObject=new XMLHttpRequest();
} else {
    //other less compliant browsers
	xmlHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
}

function switchPage(hide, show, pageid){
    hideHelpArea();
    setMessageArea('');
    setHelpMessage(pageid);
    toggle(hide,show);
}
function toggle(hide,show){
    hider=document.getElementById(hide);
    shower=document.getElementById(show);
    
    hider.style.display='none';
    
    if (show == 'cuLoginButton') {
		shower.style.display='inline';
    } else {
		shower.style.display='block';
    }
    
}
function setMessageArea(message){
	var x=document.getElementById("messagearea");
    // x.innerHTML='<br>' + message
    x.innerHTML=message
    if(message > ''){
        x.style.display="block";
    }else{
        x.style.display="none";
    };
}
function hideHelpArea(){
    x=document.getElementById('helparea');
    x.style.display='none';
}
function setHelpMessage(pageid){
    x=document.getElementById('helparea');
    y = '<hr size="1" class="login-hr" />'
    if(pageid==0){//Customer Login
        x.innerHTML=y+'Enter the Global Account Access User Name and Password for your account.';
    }else if(pageid==1){//Forgotten Password
        x.innerHTML=y+'Supply either the email address for the account, or the User Name and the system will reset the password and email you with the new login information.';
    }else{//Invoice Required
        x.innerHTML=y+'Supply an Invoice Number on the account for the specified email address so that the system will know which account requires the password reset.';
    }
}
function requestInvoice(email){
    x=document.getElementById('emailaddr');
    y=document.getElementById('multipleEmailAddress');
    x.value=email;
    y.innerHTML=email;
    switchPage('recover1','recover2', 2);
}
function sendLogin(){
    var allValid=1;
	//set user and pass variables to values in form
	user=document.loginform.name.value;
	pass=document.loginform.pass.value;

	if (isEmpty(user)){
		setMessageArea("You must supply a User Name in order to login.")
		allValid = 0;
	}

	if (allValid==1 && isEmpty(pass)){
		setMessageArea("You must supply a Password in order to login.")
		allValid = 0;
	}

	if(allValid==1){
    	//set up ajax request
    	xmlHttpRequestObject.open("POST", "find-user.php",true);
      	xmlHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

        //set POST variables
    	xmlHttpRequestObject.send("cCustUserName="+user+"&cCustPassword="+pass);

    	//when response is sent back, do this function
      	xmlHttpRequestObject.onreadystatechange=function(){

    		if (xmlHttpRequestObject.readyState==4 && xmlHttpRequestObject.status==200){

    		    //set debug text to page respnse
    			var responseHtml=xmlHttpRequestObject.responseText;
    			//alert(xmlHttpRequestObject.responseText);

    			if (responseHtml.substr(0,7)=='success'){
    			    //found user
    			    initOutro();
    			    toggle("cuLoginButton", "loggedInDivWrapper");
    			    y = responseHtml.trim();
    			    try{
    			        cCustNamex = document.getElementById("cCustNamex");
    			        cCustNamex.innerHTML = " " + y.substr(7);
    			    }catch(ex){
    			         document.all.cCustNamex.innerText = " " + y.substr(7);
    			    }
    			    setMessageArea('');
    			    //Entry Form scripting
    			    try{
    			        if(document.captureForm.iLoggedIn.value=="0"){
    			            document.captureForm.iAction.value = "0";
    			            document.captureForm.iLoggedIn.value = "1";
    			            document.captureForm.submit();
    			        }
    			    }catch(ex){
    			        //do nothing special, normal login
    			    }
    			}else if (responseHtml.substr(0,7)=='failure'){
    			    //no find user
    			    setMessageArea('The User Name and Password you provided are not valid.  Please check your entry and try again.');
    			    document.loginform.pass.value = '';
    			}
    		}
    	}
    }//end test if all valid.
}
function doLogout(){
    //set up ajax request and send
	xmlHttpRequestObject.open("POST", "log-out.php", true);
    xmlHttpRequestObject.send();

    //when response is sent back, do this function
  	xmlHttpRequestObject.onreadystatechange=function(){

		if (xmlHttpRequestObject.readyState==4 && xmlHttpRequestObject.status==200){
            //all good
			toggle("loggedInDivWrapper", "cuLoginButton");
			//get the host
			var host=(location.hostname);
			if (host =="192.168.0.10" || host =="168.158.199.69"){
			    host +="/responsivedata";
			} else {
			    //we are on prod
			    host = "www.responsivedata.com";
			}
			forceIframe(host);
		}
	}

}

function forceIframe(host){
    logoutdiv=document.getElementById('messagearea');
    var fwrite = '<iframe src="http://'+host+'/log-out.php" width="600" style="display:none"></iframe>';
    logoutdiv.innerHTML=fwrite;
}

function request1(){

 	email=document.request1.email.value;
	username=document.request1.username.value;
	try{
		showtemppass=document.request1.showtemppass.value;
	}catch(ex){
		var showtemppass = '';
	}

	if (!email && !username){
	    setMessageArea ("Please supply either an Email Address or your User Name.");
	} else {
    	//set up ajax request
    	xmlHttpRequestObject.open("POST", "recover-user.php",true);
      	xmlHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

        //set POST variables
    	xmlHttpRequestObject.send("email="+email+"&username="+username+"&showtemppass="+showtemppass);

    	//when response is sent back, do this function
      	xmlHttpRequestObject.onreadystatechange=function(){

    		if (xmlHttpRequestObject.readyState==4 && xmlHttpRequestObject.status==200){

    		    //set debug text to page respnse
    			var responseHtml=xmlHttpRequestObject.responseText;
    			setMessageArea(responseHtml);
    			if (responseHtml.substr(0,3)=='A p'){
    			     //divSlideFx.toggle();
                    switchPage('recover1','login', 0);
    			    setMessageArea(responseHtml);
    			}else if (responseHtml.substr(0,14)=='There are more'){
    			    requestInvoice(email);
    			}
    		}
    	}
	}
}

function request2(){

 	email=document.request2.emailaddr.value;
	iInvoice=document.request2.iInvoice.value;
	try{
		showtemppass=document.request2.showtemppass.value;
	}catch(ex){
		var showtemppass = '';
	}

	if (!iInvoice){
	    setMessageArea ("Please supply an Invoice Number from the account identified by: " + email);
	} else {
    	//set up ajax request
    	xmlHttpRequestObject.open("POST", "recover-user.php",true);
      	xmlHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

        //set POST variables
    	xmlHttpRequestObject.send("email="+email+"&iInvoice="+iInvoice+"&showtemppass="+showtemppass);

    	//when response is sent back, do this function
      	xmlHttpRequestObject.onreadystatechange=function(){

    		if (xmlHttpRequestObject.readyState==4 && xmlHttpRequestObject.status==200){

    		    //set debug text to page respnse
    			var responseHtml=xmlHttpRequestObject.responseText;
    			setMessageArea(responseHtml);
    			if (responseHtml.substr(0,3)=='A p'){
    			     //divSlideFx.toggle();
    			     switchPage('recover2','login', 0);
    			     setMessageArea(responseHtml);
    			}else{
    			    document.recover2.iInvoice.value = '';
    			}
    		}
    	}
	}
}