// JavaScript
//requires the HTTP.js script for HTTP(AJAX) requests

var $ = function(id){
    return document.getElementById(id);
}
var $CE = function(name){
    return document.createElement(name);
}

//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];
}

function contactUs(product){
    
    document.title = getTitle("Contact Us");

	startLoader();
	// Callback function for request to handle
	function callback(response){		                
		var ajaxDisplay = document.getElementById('mainContent');
                //document.getElementById("loading").style.display = "none";//hide loading
                ajaxDisplay.innerHTML = response;
                $("mainContent").style.display = "block";	
                $("txtsubject").value = "Additional Documentation Request";
                
                switch(product){
                    case "sl":
                        $("chkSL").checked = true;
                        break;
                        
                    case "fq":
                        $("chkFQ").checked = true;
                        break;                                                
                        
                    case "lng":
                        $("chkLNG").checked = true;
                        break;
                        
                    case "dig":
                        $("chkDIG").checked = true;
                        break;
                }
	}
	HTTP.getText('PHP/data.php?data=Contact', callback);    
}

function onLoad(){
    menuCtrl.init();
    getData('PHP/data.php', "Home");
}
//loader display functions
function startLoader(){
		//show loading
	$("mainContent").style.display = "none";
	//$("loading").style.display = "block";
}
function stopLoader(response){
	//populate new data and then hide loading screen
	var ajaxDisplay = $('mainContent');
	//document.getElementById("loading").style.display = "none";//hide loading
	ajaxDisplay.innerHTML = response;
	$("mainContent").style.display = "block";
}

//AJAX Function
var DATA_URL ="PHP/data.php";
function getData(strData)
{
	document.title = getTitle(strData);
	startLoader();
	// Callback function for request to handle
	function callback(response){					
		stopLoader(response);		
	}
	HTTP.getText(DATA_URL+"?data="+strData , callback);
};

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

function getTitle(strData){
	switch(strData){
		case "StatLinkOverview":			
			return "StatLink Overview";
			break;
					
		case "StatLinkCS":
			
			return "StatLink Capture Station";													  
			break;
			
		case "StatLinkDash":			
			return "StatLink Dashboard";			
			break;
			
		case "TranscoInvoiceClient":			
			return "Transco Invoice Client";
			break;
			
		case "Extensions":			
			return "StatLink Extensions";
			break;
		
		case "FirstQuoteOverview":			
			return "FirstQuote Overview";
			break;
		
		case "FirstQuoteServer":			
			return "FirstQuote Server";
			break;
			
		case "FirstQuoteServer2":			
			return "FirstQuote Server";
			break;
			
		case "FirstQuoteLite":			
			return "FirstQuote Lite";
			break;
			
		case "FirstQuoteLite2":			
			return "FirstQuote Lite";
			break;
		
		case "FirstQuoteProfessional":				
			return "FirstQuote Professional";
			break;
		
		case "FirstQuotePro2":			
			return "FirstQuote Professional";
			break;
		
		case "FirstQuoteGraph":			
			return "FirstQuote Graph";
			break;
		
		case "FQExtensions":			
			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 & 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 surname field has content
	if (theForm.txtname.value == "")
	{
		alert("Please enter your name");
		theForm.txtname.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.txtname = theForm.txtname.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;
	postVars.docFQ = $("chkFQ").checked;
        postVars.docSL = $("chkSL").checked;
        postVars.docLNG = $("chkLNG").checked;
        postVars.docDIG = $("chkDIG").checked;               
        
	function responseHandler(response){
                theForm.reset();
		$("mailResponse").innerHTML = response;
	}
	
	HTTP.post("PHP/mail.php", postVars, responseHandler)
	//startLoader();
}
function loadData(){		
    return;
	var target = $_GET("target");	
	getData(target);
}
