
function validateOrder()
{
	toner = 0;
	if (document.getElementById("toner_cyan").checked)
		toner ++;
	if (document.getElementById("toner_magenta").checked)
		toner ++;
	if (document.getElementById("toner_yellow").checked)
		toner ++;
	if (document.getElementById("toner_black").checked)
		toner ++;
	
	if (document.getElementById("reading_black").value == "")
	{
		alert("Please enter your Meter Reading for Black & White.");
		return false;	
	}
	if (document.getElementById("reading_colour") != null)
	{
		if (document.getElementById("reading_colour").value == "")
		{
			alert("Please enter your Meter Reading for Colour.");
			return false;
		}
	}
	if (toner == 0)
	{
		alert("Please select atleast 1 type of Toner you wish to order.");
		return false;	
	}
	
	return true;
}

function validateNewReadings()
{
	return true;
			
}

function validateProductSelection()
{
	if (document.getElementById("machine_serial").selectedIndex == 0)
	{
		alert("Please select a valid Product from the drop down list.");
		return false;
	}
	else
		return true;
}

function confirmReminderMailout()
{
	if (confirm("Are you sure you want to send a reminder out to all clients who have not submitted their readings for this month?"))
		window.location = "cronreminder.php";
	else
		return false;
}

function confirmMachineDelete(client_id, machine_id)
{
	if (machine_id == undefined)
		return false;
	else
	{
		if (confirm("Are you sure you want to delete this Machine?"))
			window.location = "client.php?id="+ client_id +"&delete_machine="+machine_id;
		else	
			return false;
	}
}

function confirmClientDelete(client_id)
{
	if (client_id == undefined)
		return false;
	else
	{
		if (confirm("Are you sure you want to delete this Client?"))
			window.location = "clients.php?delete_client="+client_id;
		else	
			return false;
	}
}

function meterpopup(machine_id)
{
	if (machine_id == undefined)
		url = 'showallreadings.php';	
	else	
		url = 'viewreadings.php?id='+machine_id;
	
	
	newwindow=window.open(url,'name','height=500,width=550,resizable=yes,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// JavaScript Document
currentpage = "";

links = new Array();
	links[0] = "home"
	links[1] = "aboutus"
	links[2] = "products"
	links[3] = "services"
	links[4] = "temp"
	links[5] = "donations"	
	links[6] = "careers"
	links[7] = "contact"
	links[8] = "request"
	links[9] = "referral"
	
	is = new browserCheck();
	
	myObj = new Object();
	imageObj = new Object();
	for(i=0;i<links.length; i++){
		imageObj[links[i]+'_over']=new Image();
		imageObj[links[i]+'_over'].src='images/nav_'+links[i]+'_over.jpg';
		imageObj[links[i]+'_off']= new Image();
		imageObj[links[i]+'_off'].src='images/nav_'+links[i]+'_off.jpg';
		imageObj[links[i]+'_select']= new Image();
		imageObj[links[i]+'_select'].src='images/nav_'+links[i]+'_selected.jpg';

	}
	
	function browserCheck() {
		this.ns4 = (document.layers)? true:false;
		this.ie = (document.all&&(!window.opera))? true:false;
		this.dom = (document.getElementById)? true:false;
		this.ns6 = (window.sidebar)? true:false;
		this.moz = (window.sidebar||navigator.userAgent.indexOf('Gecko')!=-1)? true:false;
		this.opera = (window.opera)? true:false;
		this.mac = (navigator.userAgent.indexOf('Mac')!=-1)? true:false;
	}
	
	function getObjectName(nameOfObject){
		Obj = null;
		
		if (is.ie) Obj = document.all[nameOfObject];
		else if (is.dom) Obj = document.getElementById(nameOfObject);
		else if (is.ns4) findLayer(window,nameOfObject);
		
		if (!Obj || ( is.ns4 && Obj == window ) ) Obj = "Object not found"
		
		return Obj;
	}
	
	function getObjectStyle(nameOfObject){
		
		Obj = null;
		
		if (is.ie && document.all[nameOfObject] != null) Obj = document.all[nameOfObject].style;
		else if (is.dom && document.getElementById(nameOfObject) != null) Obj = document.getElementById(nameOfObject).style;
		else if (is.ns4) findLayer(window,nameOfObject);
		
		return Obj;
	}
	
	function findLayer(node,nameOfObject) {
	
		if ( node.name == nameOfObject ) Obj = node;
		
		for ( var counter = 0; counter < node.document.images.length; counter++ ) {
			if (node.document.images[counter].name==nameOfObject) Obj = node.document.images[counter];
		}
		
		for ( var i = 0; i < node.document.layers.length; i++ ) {
			findLayer(node.document.layers[i],nameOfObject);
		}
	}
	
	function turnOffOthersNavs(name){
		for(i=0; i<links.length; i++){
			if(name != links[i]){
				if(myObj['timer_'+links[i]]){
					clearTimeout(myObj['timer_'+links[i]]);
				}
				doHide(links[i]);
			}
		}
	}
	
	function showNav(name){
		turnOffOthersNavs(name);
		if(myObj['timer_'+name]){
			clearTimeout(myObj['timer_'+name]);
		}
		
		myStyle = getObjectStyle('div_'+name);
		if(myStyle!=null) myStyle.visibility = 'visible';
		
		if(document.images[name]) document.images[name].src=imageObj[name+'_over'].src;
		
					
	}
		
	function hideNav(name){
		myObj['timer_'+name] = window.setTimeout("doHide('"+name+"')", 100);
	}
	
	function doHide(name){
		myStyle = getObjectStyle('div_'+name);
		if(myStyle!=null) myStyle.visibility = 'hidden';
		
		if(document.images[name]){
  		 if(name != currentpage)
		  document.images[name].src=imageObj[name+'_off'].src;
		}
	
	}
	
	function changeBg(name, col){
		myStyle = getObjectStyle(name);
		myStyle.backgroundColor = col;
	}
	
	function changeIm(name, state){
		document.images[name].src=imageObj[name+'_'+state].src;
	}
	
	
		function changeColor(name, col){
		
		name.style.backgroundColor = col;
	}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


var editing=0;
function formData2QueryString(docForm) {

  var submitContent = '';
  var formElem;
  var lastElemName = '';
  
  for (i = 0; i < docForm.elements.length; i++) {
    
    formElem = docForm.elements[i];
    switch (formElem.type) {
      // Text fields, hidden form elements
      case 'text':
      case 'hidden':
      case 'password':
      case 'textarea':
      case 'select-one':
        submitContent += formElem.name + '=' + escape(formElem.value) + '&'
        break;
        
      // Radio buttons
      case 'radio':
        if (formElem.checked) {
          submitContent += formElem.name + '=' + escape(formElem.value) + '&'
        }
        break;
        
      // Checkboxes
      case 'checkbox':
        if (formElem.checked) {
          // Continuing multiple, same-name checkboxes
          if (formElem.name == lastElemName) {
            // Strip of end ampersand if there is one
            if (submitContent.lastIndexOf('&') == submitContent.length-1) {
              submitContent = submitContent.substr(0, submitContent.length - 1);
            }
            // Append value as comma-delimited string
            submitContent += ',' + escape(formElem.value);
          }
          else {
            submitContent += formElem.name + '=' + escape(formElem.value);
          }
          submitContent += '&';
          lastElemName = formElem.name;
        }
        break;
        
    }
  }
  // Remove trailing separator
  submitContent = submitContent.substr(0, submitContent.length - 1);
  return submitContent;
}


function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(strURL,strSubmitContent) {
    http.open('POST', strURL, true);
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    http.onreadystatechange = handleResponse;
    http.send(strSubmitContent);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();
		
		if(response == "denied")
		 document.getElementById('loading').innerHTML = "<span style=\"color:#FF0000\">incorrect login..</span>";
		else if(response == "granted"){
		 window.location = 'login.php';
		}
    }
	else
	  document.getElementById('loading').innerHTML = "<span style=\"color:#1698A1\">loging in...</span>";
	  
}

function validateSubmit() {

	  var strSubmitContent = '';
	  strSubmitContent = formData2QueryString(document.forms[0]);
	  sndReq('loginres.php', strSubmitContent);

}