/*THE CONTENTS OF THIS FILE ARE COPYRIGHT CARALAN PRINTERS 2009
The author does not allow reproduction or use by any other party under any circumstances
*/

// GLOBAL VARIABLES //////////////////////////////////////////////////////////////////////////////////////////////////////////////

	// page_type and page_id are declared here but values are set on individual pages in editregion "head"
	var page_type = ""; // allows for different script behaviour on different groups of pages, eg script generated pages .cgi rather than static .htm
	var page_id = ""; // allows for differing script behaviour on different individual pages
	var current_page_url = "";


// BASKET HANDLER SCRIPT //////////////////////////////////////////////////////////////////////////////////////////////////////////////
	var basket_cookie =  get_basket_cookie();
	var basket_array = new Array();
	
  function add_to_basket(product_code){
		// call cgi script to update basket cookie wibble
		window.open('/cgi-bin/basket.cgi?context=add_to_basket&item=' + product_code, 'caralan_basket', 'WIDTH=500,HEIGHT=300,resizable,scrollbars');
	} // eof add_to_basket

	function get_basket_cookie(){ // Extracts basket cookie string from the document cookie
		my_basket_cookie = "";
		for( i in cookie_array) { // parse cookie array until basket cookie found
		  my_basket_cookie = cookie_array[i];
			if(my_basket_cookie.indexOf('caralan_basket') >= 0){ // this is the basket cookie  
				// clean up - remove invisibles
				my_basket_cookie.replace(/\r/g, "");
				my_basket_cookie.replace(/\n/g, "");
				my_basket_cookie.replace(/\t/g, "");
				my_basket_cookie.replace(/\f/g, "");
				
				return my_basket_cookie;
			}
    }
		return my_basket_cookie; // return empty string as default
	} // eof get_basket_cookie	


  function check_basket(){
		// reads basket cookie and sets browser objects to display summary
		basket_array = get_basket_array();
		document.getElementById("items_cost").innerHTML = "&pound;" + basket_array[1];
		document.getElementById("items_del").innerHTML = "&pound;" + basket_array[2];
		document.getElementById("vat").innerHTML = "&pound;" + basket_array[3];
		document.getElementById("total").innerHTML = "&pound;" + basket_array[4];
	} // eof check_basket



	function get_basket_array(){ // loads current username and password from cookie into global array
		basket_cookie =  get_basket_cookie();
		my_basket_string = basket_cookie;
		my_basket_array = new Array();
		my_basket_array[0] = "";
		my_basket_array[1] = "";
		my_basket_array[2] = "";
		my_basket_array[3] = "";
		my_basket_array[4] = "";
		//my_basket_array[5] = "";
		 
		//alert( "Whole Cookie : " + cookie_string + "\nBasket Cookie : " + basket_cookie); return;
				
		if(my_basket_string.indexOf("__") > 5){
			// my_basket_string = my_basket_string.substr(15); // chop off caralan_basket=       substr(16)   indexOf('=') - 1
			my_basket_string = my_basket_string.replace("caralan_basket=",""); //
			my_basket_string.replace(/=/, "" );    // chop off any stray "=" - re older firefox
			my_basket_array = my_basket_string.split("__");
			/*
			 alert("cookie : " + my_basket_string + "\num_items : " + my_basket_array[0] + "\nitems_cost : " + my_basket_array[1] + "\nitems_del : " + my_basket_array[2] + "\nitems_vat : " + my_basket_array[3]
			  + "\ntotal : " + my_basket_array[4]
				 + "\nitems_list : " + my_basket_array[5]);
			*/
		}
		return my_basket_array;
	} // eof get_basket_array		


// EOF BASKET HANDLER //////////////////////////////////////////////////////////////////////////////////////////////////////





// LOGIN HANDLER SCRIPT /////////////////////////////////////////////////////////////////////////////////////////////////////
	var reseller = false;
	var cookie_string = unescape(document.cookie); // whole document cookie string
	var cookie_array = new Array(); 
	cookie_array = cookie_string.split(";"); // cookie strings split into array
	var login_cookie = get_login_cookie(); // retrieve login cookie on document load
	var login_array = get_login_array();  // retrieve username and password as an array user is login_array[0] and password is login_array[1]
	var login_user = login_array[0];
	var login_password = login_array[1];	
	var login_status_changed = false; // indicates a page reload required by check_state() subroutine declared on htm pages.

	
	function get_login_array(){ // loads current username and password from cookie into global array
		my_login_string = login_cookie;
		my_login_array = new Array();
		my_login_array[0] = ""; // username
		my_login_array[1] = ""; // password
		my_login_array[2] = ""; // reseller status
		
		if(my_login_string.indexOf("_|_") > 5){
			// clean up string before split - IE and MOZ are different
			my_login_string = my_login_string.replace(/[ \t\f\r\n]/gi,""); //remove spaces and invisibles
			my_login_string = my_login_string.replace("caralan_login=user:",""); //
			my_login_array = my_login_string.split("_|_");
			
			if(my_login_array[1] != null){
				my_login_array[1] = my_login_array[1].substr(7);
			}
			// get reseller status
			if(my_login_array[2] != null){
				my_login_array[2] = my_login_array[2].substr(9);
				if( my_login_array[2] == 1){
					reseller = true;
				}
				else{
					reseller = false;
				}
			}			

			 //alert("cookie : " + my_login_string + "\nUser : " + my_login_array[0] + "  Passwd : " + my_login_array[1]);
		}
		my_login_array[0].replace(/ /g, ""); // username cannot contain spaces
		return my_login_array;
	} // eof get_login_password		
	

	
	function login(){ // posts username and password to server
	
		my_username = document.login_form.username.value;
		my_password = document.login_form.password.value;   
		// Check that username and password are OK first
		if( my_username.length < 2 || my_password.length < 2){
			alert("Please enter a valid username and password to login.\nUser : " + my_username + "  Passwd : " + my_password);
			return 0;
		}
		if( ! isEmail(my_username)){
			alert("Cannot Login. Your username must be your email address\n\n" + my_username + " is not valid");
			return 0;
		}
		if( ! JS_is_websafe_password(my_password)){
			return 0;
		}		
		// Check if already logged in
		if( login_user != null ){
			if( login_user.length > 4 ){
				if( ! confirm("Already logged in as " + login_user + ". Continue login?")){
					return 0;
				}
			}
		}
		
		// call cgi script to set login cookie
		window.open('http://www.caralan.com/cgi-bin/logon.cgi?login_request=login&username='  + my_username + '&password=' + my_password,'caralan_login', 'WIDTH=400,HEIGHT=300,resizable,scrollbars');
		login_status_changed = true; // reload page to reflect new login status	
	} // eof login
	

	function return_login(key_event){
		// captures enter and return key press chr(13) and calls login function 
		var charcode = key_event.keyCode;
		if(charcode == 13){
			login();
		}
	}


	

	function logout(){ // clears the login cookie and reloads the page
		if( login_user != "" ){ // only log out if logged in
			if(confirm("Log out " + login_user + "?")){
				window.open('/cgi-bin/logon.cgi?logout_request=logout&username='  + login_user + '&password=' + login_password,'Caralan_Logout', 'WIDTH=400,HEIGHT=300,resizable,scrollbars');
				//reset global variables
				cookie_string = ""; // whole document cookie string
				cookie_array[0] = ""; // cookie strings split into array
				cookie_array[1] = "";
				login_cookie = ""; // retrieve login cookie on document load
				login_array[0] = "";  // retrieve username and password as an array user is login_array[0] and password is login_array[1]
				login_array[1] = "";
				login_user = "";
				login_password = "";
			}
		}
		else{
			alert("You are already logged out");
		}
		login_status_changed = true; // reload page to reflect new login status
	} // eof logout
	
	
	function get_login_cookie(){ // Extracts login cookie string from the document cookie
		my_login_cookie = "";
		for( i in cookie_array) { // parse cookie array until login cookie found
		  my_login_cookie = cookie_array[i];
			if(my_login_cookie.indexOf('caralan_login') >= 0){ // this is the login cookie  
				// clean up - remove invisibles
				my_login_cookie.replace(/\r/g, "");
				my_login_cookie.replace(/\n/g, "");
				my_login_cookie.replace(/\t/g, "");
				my_login_cookie.replace(/\f/g, "");
				
				return my_login_cookie;
			}
    }
		return my_login_cookie; // return empty string as default
	} // eof get_login_cookie	
	
	function refresh_page(){
		if( current_page_url.length > 5){
			window.location.href = current_page_url;
		}
	}
	
	function check_login(){ // called on document focus. Updates browser page to display user login status
		if( login_status_changed && page_id == "basket_cgi" ){// login status has changed on basket summary page so recalculate basket
			login_status_changed = false; // reset latch - only reload once after a status change
			setTimeout("document.checkout_form.submit()", 3000); // resubmit checkout summary form  after 3 second delay
		}
		if( login_status_changed && page_type == "pricelist_cgi" ){// login status has changed on basket summary page so recalculate basket
			login_status_changed = false; // reset latch - only reload once after a status change
			setTimeout("refresh_page()", 3000); // reload the current url after 3 second delay 
		}

		//reset global variables
		cookie_string = unescape(document.cookie); // whole document cookie string
		cookie_array = cookie_string.split(";"); // cookie strings split into array
		login_cookie = get_login_cookie(); // retrieve login cookie on document load
		login_array = get_login_array();  // retrieve username and password as an array user is login_array[0] and password is login_array[1]
		login_user = login_array[0];
		login_user.replace(/ /g, ""); // username cannot contain spaces
		login_password = login_array[1];
	
	
		if( login_user.length > 4 ){	// user already logged in so change login options display
			// update document display
			document.getElementById("logout").style.visibility = "visible";
			document.login_form.username.value = login_user;
			document.login_form.username.disabled = true;
			document.login_form.password.disabled = true;
		}
		else{ // restore blank login stuff
			document.getElementById("logout").style.visibility = "hidden";
			document.login_form.username.disabled = false;
			document.login_form.password.disabled = false;
		}
		

	
	} // eof check_login	
	
// EOF LOGIN HANDLER SCRIPT /////////////////////////////////////////////////////////////////////////////////////////////////////	