// JavaScript Document
// global variables for timeout and for current menu
var timerset=false,current;
var inputcount=0, IsDirty=false;
var backup = false;
function SetDirty(obj)
{
	var msg = document.getElementById("SaveMessage");
	msg.innerHTML = '<span class='+'"'+'errortext'+'"'+'><b>This data has not been saved</b></span>';
	IsDirty = true;

    var objid = obj.id;
    var rec = objid.indexOf(" ");
    if (rec != -1)
    {
        var recno = objid.slice(rec+1);
        if (document.getElementById("changed"+recno))
        {
            document.getElementById("changed"+recno).value = 1;
        }
    }
}

function PrintIt(){
	if (window.print) {
	    window.print() ;
	} else {
	    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
	}
}

function NoPrint()
{
    history.back(1);
}

function CheckIsDirty(e)
{
      if (IsDirty)
      {
            IsDirty = false;
            alert('Save your work?');


            var frm = window.event? window.event.srcElement: e ? e.target: null;
            if (!frm) return;
            
            frm.submitAllowed = false;
            if (e && e.stopPropagation && e.preventDefault)
            {
                  e.stopPropagation();
                  e.preventDefault();
            }
            if (window.event)
            {
                  window.event.cancelBubble = true;
                  window.event.returnValue = false;
                  return false;
            }
      }
      IsDirty = false;

}

function RemoveDirty()
{
      IsDirty = false;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function hilite(obj) {
	obj.style.border = '1px solid #CC0000';
}

function delite(obj) {
	obj.style.border = '1px solid #00B0D7';		
}

sfHover = function() {
    if (!document.getElementById("navBar")) return;
    var sfEls = document.getElementById("navBar").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onmouseover=function() {
            this.className+=" sfhover";
        }
        sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
}
function fieldvalidate(field,msg)
{
	with (field)
	{
		if (value==null || value=="")
        {
			alert(msg);
			return false;
        }
        else
            return true;
	}
}

function rvalidate(thisform)
{
	with (thisform)
	{
		if (fieldvalidate(establishmentname,"Business name cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (fieldvalidate(baddress1,"Business address 1 cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (fieldvalidate(bpostcode,"Business post code cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (fieldvalidate(btelephone,"Business telephone cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (fieldvalidate(surname,"Contact surname cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (fieldvalidate(email,"Contact email cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (email.value != confirmemail.value)
		{
			alert("Contact email is not the same as confirm email");
			return false;
		}
		if (fieldvalidate(password,"Contact password cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (password.value != confirmpassword.value)
		{
			alert("Contact password is not the same as confirm password");
			return;
		}
		if (fieldvalidate(caddress1,"Contact address 1 cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (fieldvalidate(cpostcode,"Contact post code cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
		if (fieldvalidate(ctelephone,"Contact telephone cannot be blank") == false)
		{
			establishmentname.focus();
			return false;
		}
	}
	return true;
}

function ajaxRequest()
{
      var activexmodes = ["MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];

      if (window.XMLHttpRequest) // if Mozilla, Safari etc
            return new XMLHttpRequest();

      if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
            for (var i=0; i<activexmodes.length; i++)
            {
                  try
                  {
                        return new ActiveXObject(activexmodes[i]);
                  }
                  catch(e)
                  {
                  //suppress error
                  }
            }
      }
      else
            return false;
}

function preload(item)
{
      var mygetrequest=new ajaxRequest();

      if (mygetrequest === false)
      {
            alert('Failed to create ajaxRequest');
            exit;
      }
      mygetrequest.onreadystatechange=function()
	  {
	      if (mygetrequest.readyState==4)
	      {
	            if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1)
	            {
					// the first part of the response is the operator info
					var t = mygetrequest.responseText;

					if (t == '0') return;

					var i = t.indexOf(":");
					var j = t.indexOf(";");
					var t2 = t.slice(0,j); // this is the operator info

					while (j > 0)
					{
						var id = t2.slice(0,i);
						var val = t2.slice(i+1,j);

						document.getElementById(id).value = val;
                        t = t.slice(j+1);
                        i = t.indexOf(":");
	                    j = t.indexOf(";");
	                    t2 = t.slice(0,j);
					}
					document.getElementById("preloaded").value=1;
	            }
	            else
	            {
	                  alert("An error has occured making the request");
	            }
	      }
      }


      var name=encodeURIComponent(document.getElementById("establishmentname").value);
      var type=encodeURIComponent(document.getElementById("establishmenttype").value)
      mygetrequest.open("GET", "preload.php?name="+name+"&type="+type, true);
      mygetrequest.send(null);
}

function loadlead(item)
{
	if (document.getElementById("doadd").value != 1) return false;

	var mygetrequest=new ajaxRequest();

	if (mygetrequest === false)
	{
	    alert('Failed to create ajaxRequest');
	    exit;
	}
	mygetrequest.onreadystatechange=function()
	{
		if (mygetrequest.readyState==4)
		{
		    if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1)
		    {
				// the first part of the response is the operator info
				var t = mygetrequest.responseText;

				if (t == '0') return;

				var i = t.indexOf(":");
				var j = t.indexOf(";");
				var t2 = t.slice(0,j); // this is the operator info

				while (j > 0)
				{
					var id = t2.slice(0,i);
					var val = t2.slice(i+1,j);

					document.getElementById(id).value = val;
		            t = t.slice(j+1);
		            i = t.indexOf(":");
		            j = t.indexOf(";");
		            t2 = t.slice(0,j);
				}
				document.getElementById("preloaded").value=1;
		    }
		    else
		    {
		          alert("An error has occured making the request");
		    }
		}
	}


	var name=encodeURIComponent(document.getElementById("BusinessName").value);
	var type=encodeURIComponent(document.getElementById("ETypeId").value)
	mygetrequest.open("GET", "preload.php?loadname="+name+"&type="+type, true);
	mygetrequest.send(null);
}

function displaypricing(str,element,opt)
{
    var i = str.indexOf(" ");
    var p1 = str.slice(0,i);
    var p2 = str.slice(i+1);
    var sopt;

    if (opt == 6 || opt == 12)
        sopt = 1;
    else
        sopt = document.getElementById("sizingtype").value;

    var fyr = p1*sopt;
    var nyr = p2*sopt;
    
    fyr += (sopt - 1)*.01;
    nyr += (sopt -1)*.01;

    var chgelement = document.getElementById(element+'1');

    chgelement.innerHTML = '&pound;'+fyr.toFixed(2);

    chgelement = document.getElementById(element+'2');
    chgelement.innerHTML = '&pound;'+nyr.toFixed(2);
}

function newpricing(obj)
{
    var bopt = document.getElementById("pricingtype").value;

    var j = bopt.indexOf(":");
    var opt = bopt.slice(0,j);

    if (opt == 6 || opt == 12)
        document.getElementById("sizingtype").style.display = 'none';
    else
        document.getElementById("sizingtype").style.display = 'block';

    bopt = bopt.slice(j+1);
    j = bopt.indexOf(":");
    var prices = bopt.slice(0,j);
    displaypricing(prices,'bprice',opt);

    bopt = bopt.slice(j+1);
    j = bopt.indexOf(":");
    prices = bopt.slice(0,j);
    displaypricing(prices,'ccprice',opt);

    bopt = bopt.slice(j+1);
    j = bopt.indexOf(":");
    prices = bopt.slice(0,j);
    displaypricing(prices,'dprice',opt);

    
    bopt = bopt.slice(j+1);
    j = bopt.indexOf(":");
    prices = bopt.slice(0,j);
    displaypricing(prices,'ddprice',opt);

}
if (window.attachEvent) window.attachEvent("onload", sfHover);