/*-------------------------------------------------------------------
general.js
For CentreMapsLive Ver. 2
General cross page scripts
Lovell Jonhs Ltd.
01/01/10
//------------------------------------------------------------------*/

//-------------------------------------------------------------------------GENERAL SCRIPTS

//************************Removes leading and trailing spaces from the passed string.
function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while ((ch == " ") || (ch.charCodeAt(0) == 9)) { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while ((ch == " ") || (ch.charCodeAt(0) == 9)) { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   //Remove return/new line characters from front of string
   var testCode = retValue.charCodeAt(0);
   var testCode2 = retValue.charCodeAt(1);
   if((testCode == "13") || (testCode2 == 10)){
   		retValue = retValue.substring(2, retValue.length);
   }
   return retValue;
} 
//****************Stop a form being submitted when enter is pressed
function checkEnter(e){
	evt = e || window.event;
	var characterCode = evt.keyCode? evt.keyCode : evt.charCode
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		return false; 
	}else{
		return true;
	}
}
//******************Stores if the shift key is pressed down - used in drawing circles and rectangle anno elements
function keyPressDown(e){
	evt = e || window.event;
	if(evt.shiftKey){
		shiftIsDown = true;		
		if(document.varform.selectedTool.value == "drawcircle"){
			drawAnnoCircleControl.activate();
			drawAnnoLabelControl.deactivate();
		}else if(document.varform.selectedTool.value == "drawrect"){
			drawAnnoRectControl.activate();
			drawAnnoLabelControl.deactivate();
		}
	}	
}
function keyReleased(e){
	shiftIsDown = false;
}
//******************Submits text box when return key pressed
function submitIt(theControl,e){		
	//Check if a return has been pressed to submit a textbox	
	evt = e || window.event;
	var unicode=evt.keyCode? evt.keyCode : evt.charCode
	if(unicode == 13){
		if(is_chrome){
			return false;
		}else{
			if(theControl == "login"){				//Run login
				runLogin('in');	
			}else if(theControl == "search"){			//Run the search
				showSearchOptions();
			}
			return true;
		}	
	}
}
//****************Goto user's home page
function goToHome(){
	document.varform.action = "account_home.php";
	document.varform.submit();
}
//****************Run Logout - called from all pages except the main map page
function runLogout(){	
	var qstring = "name=&pwd=&login=out&type=normal";		
	var logoutReq = new ajaxObject('runlogin.php', logoutResponse);
	logoutReq.update(qstring,'POST');	
}
//****************Response from running the logout
function logoutResponse(responseText, responseStatus){
	if(responseStatus == 200){	
		//Get the returned values
		if(responseText != ""){
			var tempArray = new Array();
			tempArray = responseText.split("|");	
			if(tempArray[0] == "ok"){
				if(tempArray[2] == "yes"){
       				window.location.href = "portalLogin.php";
				}else{											
					submittheform('ordergb');		//Go back to home page							
				}
			}
		}
	}
}
//*******************Opens a new window to show fixed content/help etc
function openNew(theSiteURL,theSite,theContent){
	var theURL = "";	
	if(theContent == "datapolicy"){ 								//Data licensing terms
		theURL = theSiteURL + "/info_files/" + theSite + "_data_licence.pdf";
	}else if(theContent == "format_info"){ 							//Info on formats					
		theURL = theSiteURL + "/info_files/format.htm";
	}else if(theContent == "overlay_info"){ 						//Info on data overlays
		theURL = theSiteURL + "/info_files/mapping_overlay_information_examples.htm";
	}else if(theContent == "supplierinfo"){ 						//Info on suppliers
		theURL = theSiteURL + "/info_files/supplier_information.htm";
	}else if(theContent == "paymentterms"){ 						//Payment terms
		theURL = theSiteURL + "/info_files/" + theSite + "_payment_terms.htm";
	}
	if(theURL != ""){
		var winName =  "newwindow";
		var features = "toolbar=no,scrollbars=yes,resizable=yes,width=600,height=600";
		window.open(theURL,winName,features);
	}
}
//*******************Replace any '&' in the query string
function escapeAmp(inputString){
	var outputString = inputString.replace(/&/g,"-amp-"); 
	return outputString;
}
//****************Run Cufon styling
function callCufon(){
	Cufon.replace("h1");
	Cufon.replace('h1', { fontWeight: 'bold', fontSize: '18px', color: '#24318D'}); 
	Cufon.replace("h2");
	Cufon.replace('h2', { fontWeight: 'bold', fontSize: '16px', color: '#24318D'}); 
	Cufon.replace("h3");
	Cufon.replace('h3', { fontWeight: 'bold', fontSize: '14px', color: '#24318D'});  
	Cufon.replace("h4");
	Cufon.replace('h4', { fontWeight: 'bold', fontSize: '11px', color: '#ED0489'});  //lineHeight: '30px'  pink '#ED0489'
	Cufon.replace("h5");
	Cufon.replace('h5', { fontWeight: 'bold', fontSize: '10px', color: '#24318D'});  	
	Cufon.now();
}

// make the specified div a windowed control in IE6
// this masks an iframe (which is a windowed control) onto the div,
// turning the div into a windowed control itself
function makeWindowed(p_div){
var is_ie6 =
   document.all && 
   (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1);
	if (is_ie6)
	{
	   var html =
		  "<iframe style=\"position: absolute; display: block; " +
		  "z-index: -1; width: 100%; height: 100%; top: 0; left: 0;" +
		  "filter: mask(); background-color: #ffffff; \"></iframe>";
	   if (p_div) p_div.innerHTML += html;
	   // force refresh of div
	   var olddisplay = p_div.style.display;
	   p_div.style.display = 'none';
	   p_div.style.display = olddisplay;
	};
}

//-------------------------------------------------End of file.
