// Copyright (c) 2008 Christopher Haynes

var pageUrl;
var pageHeading;
var srcElement;
var loadImageFlow = false;
var xmlHttp;
var flagCallPage = false;
var tabImages = new Array("images/menubtn.png","images/menubtnovr.png")

function getXmlHttp()
{
    try
    {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    //alert("hello");
    return xmlHttp;
}

function openPage()
{
    if(xmlHttp.readyState==4)
    {
		var div = document.getElementById("bodymiddle");  
		div.innerHTML = ""+xmlHttp.responseText+""; 
		
        if(loadImageFlow)
        {
          setTimeout('LoadImageFlow();', 5000);
        }
        //alert(srcElement.style.backgroundImage);
		
		// execute any scripts in the loaded page
				
		
		var x = div.getElementsByTagName("script");   
		for(var i=0;i<x.length;i++)  
		{  
			eval(x[i].text);  
		}  		 
	}
}

function LoadImageFlow()
{
  //domReady(function()
  //{

          alert('loadImageFlow');
    var instanceOne = new ImageFlow();
    instanceOne.init({ ImageFlowID:'myImageFlow'//*
,
                       reflectionGET:'&bgc=ff0000&height=10%',
                       reflectionP: 0.1,
                       reflectionPNG: true
//*/
                     });
                     
  //});
}

function callPage(url, pgheading, srcelmntid, doImageFlow)
{
	//alert ("test");	
    flagCallPage = true;
		//document.getElementById("linkpage").innerHTML = "<div style=\"text-align:center;\"><img src=\"images/ajax-loader.gif\"></div>";
		document.getElementById("bodymiddle").innerHTML = "<img src=\"images/ajax-loader.gif\">";
    pageUrl = url;
    pageHeading = document.getElementById("pageheading");
    var activetab = readCookie("active_tab")
    //reset current active tab
    if (document.getElementById(activetab)) {
		
      var oldTabElement = document.getElementById(activetab);
      oldTabElement.className = "menunormal";
      //alert("tabImages[0]:"+ tabImages[0]);
      //alert("oldTabElement.style.backgroundImage:"+oldTabElement.style.backgroundImage);
    }
    //set tab image
    //var activetab = readCookie("active_tab")
//*
    var srcElement = document.getElementById(srcelmntid);
    //alert("srcElement.className:"+srcElement.className);
    srcElement.className = "menucurrent";
//*/
/*
    alert(pgheading);
    if(pgheading != "null")
    {
      pageHeading.style.display = "";
      pageHeading.innerHTML = pgheading;      
    }
    else
    {
      pageHeading.style.display = "none";      
    }
*/
    var xmlHttp = getXmlHttp();
    xmlHttp.onreadystatechange = openPage;
    createCookie("active_url", url, 0);
    createCookie("active_title", pgheading, 0);
    createCookie("active_tab", srcelmntid, 0);
    //alert(Date());
    xmlHttp.open("GET", url+"&date="+Date(), true);
    xmlHttp.send(null);
	
	
}

function showCurrent()
{
		var currhref = window.location.href;
		if ((currhref.indexOf("index.php") == -1)
		    &&(currhref.substring(currhref.length-1, currhref.length)!="/"))
		  return;
		var activeurl = readCookie("active_url");
    var activetitle = readCookie("active_title");
    var activetab = readCookie("active_tab");

    if (activeurl!=null && activetitle!=null && activetab!=null)
        callPage(activeurl, activetitle, activetab);
    
/*
    else
        callPage('index.php', 'Home');
*/
}

function getData(dataType, dataId, displayArea, doneFunc){
    var xmlHttp = getXmlHttp();
    thisDisplayArea = displayArea;
    thisDoneFunction = doneFunc;
		document.getElementById(displayArea).innerHTML = "<img src=\"images/ajax-loader.gif\">";

    xmlHttp.onreadystatechange = showData;
    xmlHttp.open("GET", dataType+"/"+dataId+".htm&date="+Date(), true);
    xmlHttp.send(null);  
}

function showData(url, displaytarget)
{
    var  PostResultElmnt = document.getElementById(displaytarget);
		PostResultElmnt.innerHTML = "Submitting your information...";
    if(xmlHttp.readyState==4)
    {
        PostResultElmnt.innerHTML = ""+xmlHttp.responseText+"";
        //alert(srcElement.style.backgroundImage);
    }  
}

function getProduct(clickedItem){
  //alert("158."+window.location);
    var xmlHttp = getXmlHttp();
    thisDisplayArea = 'product_data';
		document.getElementById(thisDisplayArea).innerHTML = "<img src=\"images/ajax-loader.gif\">";

    xmlHttp.onreadystatechange = function() {showData(clickedItem._href+"?date="+Date(), 'product_data');};
    xmlHttp.open("GET", clickedItem._href+"?date="+Date(), true);
    xmlHttp.send(null);  
}

function getIngredient(selectedItem){
    var xmlHttp = getXmlHttp();
    url = 'data/ingredients/'+selectedItem+'.htm';
		document.getElementById('ing_info').innerHTML = "<img src=\"images/ajax-loader.gif\">";

    xmlHttp.onreadystatechange = function() {showData(url, 'ing_info');};
    xmlHttp.open("GET", url+"?date="+Date(), true);
    xmlHttp.send(null);  
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
/**
 * @author Chris
 * Copyright (c) 2008 Christopher Haynes
 */

//form fields array
var Flds = new Array();
 

/*
 IMPORTANT: YOU MUST SETUP ALL THE FIELDS THAT NEED TO BE SUBMITTED
*/
 
//array format = ("fieldname", "message if empty", "sample format", /^regular expression format$/)
Flds[0] = new Array("firstname", "Please enter your firstname.", "");
Flds[1] = new Array("lastname", "Please enter your lastname.", "");
Flds[2] = new Array("email", "Please enter your email address.", "yourname@yourdomain.com", /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/);
Flds[3] = new Array("telephone", "", "");
Flds[4] = new Array("enquiry", "Please enter your enquiry.", "");
 
function ProcessForm(formname)
{
 	var thisform = document.forms[formname];
	var postUrl = "mailform.php";
	var postParams = "";
	var element;

	//check input values
  //alert("length:"+Flds.length);
  for (var x=0; x<Flds.length; x++)
	{
	  //alert(x+":"+Flds[x][0]);
    element = thisform.elements[Flds[x][0]];
    
	  //check all required fields filled in
    if (Flds[x][1] != "")
    {
			if (element.value == "") 
			{
				alert(Flds[x][1]);
				document.getElementById(Flds[x][0]).focus();
				return false;
			}
      
    }
    
  	//check field format
    if (Flds[x][2] != "")
    {
			if (!Flds[x][3].test(element.value)) 
			{
				alert("Incorrect input format used in " + Flds[x][0] + ". Please use " + Flds[x][2] + " format.");
				document.getElementById(Flds[x][0]).focus();
				return false;
		  }
    }
    //inputs ok, build the post string
		postParams = postParams + element.name+"="+element.value+"&";
  }
  
	//everything's fine, hide the form and submit the info
	//thisform.style.visibility = "hidden";
	//trim & off end of postParams
	postParams = postParams.substr(0, postParams.length - 1);
	alert("postParams:"+postParams);
	var xmlHttp = getXmlHttp();
  xmlHttp.onreadystatechange = PostResult;
	xmlHttp.open("POST", postUrl, true);
  //Send the proper header information along with the request
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", postParams.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(postParams);
}

function PostResult()
{
    var PostResultElmnt = document.getElementById("bodymiddle");
		PostResultElmnt.innerHTML = "Submitting your information...";
    if(xmlHttp.readyState==4)
    {
        PostResultElmnt.innerHTML = xmlHttp.responseText;
    }
}

function order_form_submit()
{
	var postParams = "";
  
  //alert("90. order_form_submit");
  
  //inputs ok, build the post string
  postParams = postParams + 'order_name='+
     document.order_form.order_name.value+"&";
  			
  postParams = postParams + 'order_email='+
     document.order_form.order_email.value+"&";
  			
  postParams = postParams + 'order_contactH='+
     document.order_form.order_contactH.value+"&";
  			
  postParams = postParams + 'order_contactW='+
     document.order_form.order_contactW.value+"&";
  			
  postParams = postParams + 'order_contactC='+
     document.order_form.order_contactC.value+"&";
  			
  postParams = postParams + 'order_PhyAddy='+
     document.order_form.order_PhyAddy.value+"&";
  			
  postParams = postParams + 'order_PosAddy='+
     document.order_form.order_PosAddy.value+"&";
  			
  postParams = postParams + 'SEROD_no='+
     document.order_form.SEROD_no.value+"&";
  			
  postParams = postParams + 'MEROD_no='+
     document.order_form.MEROD_no.value+"&";
  			
  postParams = postParams + 'FEROD_no='+
     document.order_form.FEROD_no.value+"&";
  			
  postParams = postParams + 'FFEROD_no='+
     document.order_form.FFEROD_no.value+"&";
  			
  postParams = postParams + 'SHAM_no='+
     document.order_form.SHAM_no.value+"&";
  			
  postParams = postParams + 'COND_no='+
     document.order_form.COND_no.value+"&";
  			
  postParams = postParams + 'PCBW_no='+
     document.order_form.PCBW_no.value+"&";
  			
  postParams = postParams + 'BL_no='+
     document.order_form.BL_no.value+"&";
  			
  postParams = postParams + 'SC_no='+
     document.order_form.SC_no.value+"&";
  			
  postParams = postParams + 'PBCMT_no='+
     document.order_form.PBCMT_no.value+"&";
  			
  postParams = postParams + 'BSBW_no='+
     document.order_form.BSBW_no.value+"&";
  			
  postParams = postParams + 'BBL_no='+
     document.order_form.BBL_no.value+"&";
  			
  postParams = postParams + 'BBC_no='+
     document.order_form.BBC_no.value+"&";
  			
  postParams = postParams + 'BW_no='+
     document.order_form.BW_no.value+"&";
  			
  postParams = postParams + 'TBT_no='+
     document.order_form.TBT_no.value+"&";
  			
  postParams = postParams + 'TBB_no='+
     document.order_form.TBB_no.value+"&";
  			
  //if (document.order_form.chkbx.checked) {
	var deliveryList = document.getElementsByName("chkbx");
	for(var d = 0; d < deliveryList.length; d++){
		if(deliveryList[d].checked){
			  postParams = postParams + 'chkbx='+deliveryList[d].value+"&";
		}
	}
  //}
  postParams = postParams + 'Delv_no='+
     document.order_form.Delv_no.value+"&";
  			
  postParams = postParams + 'Total_no='+
     document.order_form.Total_no.value+"&";
  			
/*
  postParams = postParams + 'payment_method='+
     document.order_form.payment_method.value+"&";

*/  			
  
  postParams = postParams + 'order_ref='+
     document.order_form.order_ref.value+"&";
  		
  
  postParams = postParams + 'order_ref_desc='+
     document.order_form.order_ref_desc.value+"&";
  					
	//everything's fine, hide the form and submit the info
	//thisform.style.visibility = "hidden";
	//trim & off end of postParams
	postParams = postParams.substr(0, postParams.length - 1);
	//alert("postParams:"+postParams);
	var xmlHttp = getXmlHttp();
  xmlHttp.onreadystatechange = StoreResult;//function() {showData(url, 'mycustomscroll');};
	xmlHttp.open("POST", "process_order.php", true);
  //Send the proper header information along with the request
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", postParams.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(postParams);
}

function StoreResult()
{
    var PostResultElmnt = document.getElementById("mycustomscroll");
		PostResultElmnt.innerHTML = "Submitting your information...";
    //alert(xmlHttp.readyState);
    if(xmlHttp.readyState==4)
    {
        PostResultElmnt.innerHTML = xmlHttp.responseText;
    }
}

