//Handy little function to perform an Instr in Javascript, like in VB/ASP.
function inStr(start,string1,string2){
        if(start > 0){
        	string1 = Right(string1,string1.length - start)
        }
        var inStr = string1.indexOf(string2);
        return inStr + 1;
}

//////////////////////////////////////////////////////////////////////////////////
//Login script, which submits a client-side form located on the template page.
//Depending on what is passed to this function, the form may be submitted for
//the following actions:
// - Login
// - Logout
// - Forgot Password
// - Member details
//
//////////////////////////////////////////////////////////////////////////////////
function checkForm(action){
	//alert('Trying....');		
	
	
	if (action == 'login'){
		// Post form to external login page.
		var email = document.getElementById('txtEmail').value;
		var password = document.getElementById('txtPassword').value;
		var lblLoginError = document.getElementById('lblLoginError');
		var chkRemember = document.getElementById('chkRemember');
		//alert('Trying2....');
		if ((email.length > 0) && (email != '(EMAIL)')){
			if (password.length > 0) {
				//alert('Trying3....');
				lblLoginError.innerHTML = "";					
				var myloginform = document.getElementById('loginoutform'); 
				
				//At time of writing...
				//Firefox ver 1.0.4 can't find the form with the previous line.
				//So see if it failed, and try a straight reference instead. This works.					
				if(!myloginform){
					myloginform = document.loginoutform;
					//alert(myloginform);
				}
				myloginform.action = 'http://signon.farmonline.com.au/dosignin.asp';					
				myloginform.email.value = email;
				myloginform.password.value = password;					
				myloginform.returnurl.value = location.href;			
				
				
				if (chkRemember.checked == true){
					//alert('true');
					myloginform.rememberlogin.value = '1';
				}
				else {
					//alert('false');
					myloginform.rememberlogin.value = '0';
				}
				
				//alert(myloginform.email.value + ' | ' + myloginform.password.value);
				myloginform.submit();
			}
			else {
				lblLoginError.innerHTML = "Please enter your password.<br>";
			}
		}
		else {
			lblLoginError.innerHTML = "Please enter your email address.<br>";
		}
	}
	
	else if (action == 'showmemberdetails') {
		var myloginform = document.getElementById('loginoutform'); 
				
		//At time of writing...
		//Firefox ver 1.0.4 can't find the form with the previous line.
		//So see if it failed, and try a straight reference instead. This works.					
		if(!myloginform){
			myloginform = document.loginoutform;
			//alert(myloginform);
		}
		myloginform.action = 'http://signon.farmonline.com.au/memberprofile.asp';								
		myloginform.returnurl.value = location.href;	
		myloginform.submit();		
	}
	
	else if (action == 'forgottenpassword') {
		var myloginform = document.getElementById('loginoutform'); 
				
		//At time of writing...
		//Firefox ver 1.0.4 can't find the form with the previous line.
		//So see if it failed, and try a straight reference instead. This works.					
		if(!myloginform){
			myloginform = document.loginoutform;
			//alert(myloginform);
		}
		myloginform.action = 'http://signon.farmonline.com.au/passwordreminder.asp';								
		myloginform.returnurl.value = location.href;	
		myloginform.submit();		
	}
	
	else {
		// Post form to external logout page.
		var mylogoutform = document.getElementById('loginoutform');
		
		//At time of writing...
		//Firefox ver 1.0.4 can't find the form with the previous line.
		//So see if it failed, and try a straight reference instead. This works.					
		if(!mylogoutform){
			mylogoutform = document.loginoutform;
			//alert(myloginform);
		}
		
		mylogoutform.action = 'http://signon.farmonline.com.au/dosignout.asp';
		
		
		var myurl = location.href;	
		var pos;			
		
		// Check to see if current URL already contains the signout=true parameter, and avoiding adding it a second time. 
		// This can occur if the user logs in, logs out, then logs back in and out again immediately afterward.
		pos=inStr(0,myurl,'signout=true');
		if (pos > 0){
			// Current URL already contains signout=true. 
			mylogoutform.returnurl.value = location.href; //Do not add additional signout=true parameter.
		}
		else {
			// Current URL doesn't contain signout=true. 
			
			// Check to see if current URL already has a querystring parameter. 
			// If so, avoid appending a second question mark.
			pos=inStr(0,myurl,'?');
			if (pos > 0){							
				mylogoutform.returnurl.value = location.href + '&signout=true'; //append signout=true WITHOUT question mark					
			}
			else {
				mylogoutform.returnurl.value = location.href + '?signout=true'; //append signout=true WITH question mark
			}						
		}						
		
		mylogoutform.submit();
	}
}
