// JavaScript
//requires the HTTP.js script for HTTP(AJAX) requests
var alreadyRun = false; //Menu Setting



//check for target GET var
function $_GET(name){ 
  //checks for a "GET" variable and returns the value if any.
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "noValue";
  else
    return results[1];
}



//hides or shows the menus
function HideOrShowMenu(div)
{	
	if(alreadyRun == false){alreadyRun = true}//menu has been opened manually 
	var div = document.getElementById(div);	
	if(div.style.display == "block"){
		div.style.display = "none";
	}
	else{		
		div.style.display = "block";	
	}
}


//image swapping for expand/collapse 
function swapImage(val) 
{	
	var image = document.getElementById(val);
	var index = image.src.indexOf("images/");
	var src =  image.src.substring(index);

	if(src == "images/add.png"){
		image.src = "images/collapse.png";
	}else{		
		image.src = "images/add.png";
	}
}
//loader display functions
function startLoader(){
		//show loading
	document.getElementById("mainContent").style.display = "none";
	document.getElementById("loading").style.display = "block";
}
function stopLoader(response){
	//populate new data and then hide loading screen
	var ajaxDisplay = document.getElementById('mainContent');
	document.getElementById("loading").style.display = "none";//hide loading
	ajaxDisplay.innerHTML = response;
	document.getElementById("mainContent").style.display = "block";
}

//AJAX Function
function ajaxFunction(strTarget, strData)
{
	document.title = getTitle(strData);

	startLoader();
	// Callback function for request to handle
	function callback(response){					
		stopLoader(response);		
	}
	HTTP.getText(strTarget+"?data="+strData , callback);
};

//returns a title string for defined targets of the ajax data query

function getTitle(strData){
	switch(strData){
		case "StatLinkOverview":
			if (alreadyRun == false){
				HideOrShowMenu('StatLinkMenu'); 
				swapImage('SLAdd_image');
				alreadyRun = true;
			}
			return "StatLink Overview";
			break;
					
		case "StatLinkCS":
			if (alreadyRun == false){
				HideOrShowMenu('StatLinkMenu'); 
				swapImage('SLAdd_image');
				alreadyRun = true;
			}
			return "StatLink Capture Station";													  
			break;
			
		case "StatLinkDash":
			if (alreadyRun == false){
				HideOrShowMenu('StatLinkMenu'); 
				swapImage('SLAdd_image');
				alreadyRun = true;
			}
			return "StatLink Dashboard";			
			break;
			
		case "TranscoInvoiceClient":
			if (alreadyRun == false){
				HideOrShowMenu('StatLinkMenu'); 
				swapImage('SLAdd_image');
				alreadyRun = true;
			}
			return "Transco Invoice Client";
			break;
			
		case "Extensions":
			if (alreadyRun == false){
				HideOrShowMenu('StatLinkMenu'); 
				swapImage('SLAdd_image');
				alreadyRun = true;
			}
			return "StatLink Extensions";
			break;
		
		case "FirstQuoteOverview":
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Overview";
			break;
		
		case "FirstQuoteServer":
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Server";
			break;
			
		case "FirstQuoteServer2":
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Server";
			break;
			
		case "FirstQuoteLite":
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Lite";
			break;
			
		case "FirstQuoteLite2":
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Lite";
			break;
		
		case "FirstQuoteProfessional":	
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Professional";
			break;
		
		case "FirstQuotePro2":
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Professional";
			break;
		
		case "FirstQuoteGraph":
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Graph";
			break;
		
		case "FQExtensions":
			if (alreadyRun == false){
				HideOrShowMenu('FirstQuoteMenu'); swapImage('FQAdd_image');
				alreadyRun = true;
			}
			return "FirstQuote Extensions";
			break;
			
		case "Digital":
			return "Digital Trading System";
			break;
			
		case "EasyCVA":
			return "EasyCVA";
			break;
		
		case "LNGLive":
			return "LNGLive (Previously Corsair)";
			break;
			
		case "About":
			return "About Innovez";
			break;
		
		case "Clients":
			return "Our Clients";
			break;
		
		case "Contact":
			return "Contact Us";
			break;
			
		default:
			return "Innovez - Turnkey and Bespoke Energy Trading IT Solutions";
			break;
	}
}

//validate form inputs
function Form_Validator(theForm)
{

	// check to see if the firstname box has content
	if (theForm.txtfirstname.value == "")
	{
		alert("Please enter a first name");
		theForm.txtfirstname.focus();
		return (false);
	}

	 // check to see if the surname field has content
	if (theForm.txtsurname.value == "")
	{
		alert("Please enter a surname");
		theForm.txtsurname.focus();
		return (false);
	}
	
	
		 // check to see if the company field has content
	if (theForm.txtcompany.value == "")
	{
		alert("Please enter a company name");
		theForm.txtcompany.focus();
		return (false);
	}
	
		 // check to see if the surname field has content
	if (theForm.txtemail.value == "")
	{
		alert("Please enter an email address so that we can contact you back");
		theForm.txtemail.focus();
		return (false);
	}
	else
	{
		 validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		strEmail = theForm.txtemail.value;
	
	   // search email text for regular exp matches
		if (strEmail.search(validRegExp) == -1) 
		{
		  alert('A valid e-mail address is required.\nPlease amend and retry');
		  return false;
		} 
	}
	
	
	// check to see if the subject field has content
	if (theForm.txtsubject.value == "")
	{
		alert("Please enter a subject");
		theForm.txtsubject.focus();
		return (false);
	}
	
	// check to see if the message field has content
	if (theForm.txtmessage.value == "")
	{
		alert("Please enter a message");
		theForm.txtmessage.focus();
		return (false);
	}
		// check to see if the message field is not too long
	if (theForm.txtmessage.value.length > 254)
	{
		alert("Please keep your message under 255 characters long");
		theForm.txtmessage.focus();
		return (false);
	}
	return true;	
}

function sendMail(){
	var theForm = document.forms["contactForm"];
	var postVars = {};
	
	postVars.txtfirstname = theForm.txtfirstname.value;
	postVars.txtsurname = theForm.txtsurname.value;
	postVars.txtemail = theForm.txtemail.value;
	postVars.txtcontactnumber = theForm.txtcontactnumber.value;
	postVars.txtcompany = theForm.txtcompany.value;
	postVars.txtmessage = theForm.txtmessage.value;
	postVars.txtsubject = theForm.txtsubject.value;
	
	function responseHandler(response){
		stopLoader(response);
	}
	
	HTTP.post("PHP/mail.php", postVars, responseHandler)
	startLoader();
}
function loadData(){		
	var target = $_GET("target");	
	ajaxFunction('PHP/data.php', target);
}