// JavaScript Document
// used to validate forms in the web site
// the first few fuctions look at what was entered
// into a form field and make sure that the value
// is approriate for the field
// the check functions take an argument named field, the name
// of the field to check.  This would normally be passed with
// the propertie "this", refereing to the form field that was
// calling the fucntion

// Check that something is entered into the field and there
// at least two words in the field

function checkName(field) {
  if (field.value == "") {
    alert("Value is required");
	field.focus();
	field.blur();
	field.select();
  }
  else { // if the field is not blank, then it checks to see if there is a space
    if (field.value.split(" ").length < 2) {
	  alert("Enter a full name");
	  field.focus();
	  field.blur();
	  field.select();
	}
  }
}

// Check that something is entered into the field
function checkRequired(field) {
  if (field.value == "") {
    alert("Please enter a value in this field");
	field.focus();
	field.blur();
	field.select();
  }
}

// Check that there is an at symbol entered into the field
// this effectively checks to see whether there is something
// entered into the firld at all.
function checkEmail(field) {
  if (field.value.indexOf("@") == -1) {
    alert("That was not a valid email address");
	field.focus();
	field.blur();
	field.select();
  }
}

// ****************************************AJAX STUFF

var http = getHTTPObject();
var url = "ajax.lookup.php?type=partno&partno="; // The server-side script
var isWorking = false;

function updatePartNo() {
	if (!isWorking && http) {

		document.getElementById('quickadd').style.display='none';
		var partno = document.getElementById("partno").value;
		http.open("GET", url + escape(partno), true);
		http.onreadystatechange = handleHttpResponse;
		isWorking=true;
		http.send(null);

//		alert(document.getElementById('qa_descrip').firstChild.nodeValue);
		var disp=document.getElementById('qa_descrip')?"block":"none";

		changeDesc(disp);
		document.getElementById('quickadd').style.display='block';
	}
}

function changeDesc(disp) {
		var dest_de	= document.getElementById('qa_descrip');
		var dest_de2= document.getElementById('qa_descrip2');
		var dest_b	= document.getElementById('qa_button');

		dest_de.style.display=disp;
		dest_de2.style.display=disp;
		dest_b.style.display=disp;
}
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function handleHttpResponse() {
  if (http.readyState == 4) {
	  if (http.responseText.indexOf('invalid') == -1) {
		var xmlDocument = http.responseXML;
		var descrip=xmlDocument.getElementsByTagName('descrip').item(0).firstChild.data;
		var descrip2=xmlDocument.getElementsByTagName('descrip2').item(0).firstChild.data;
		var dest_de	= document.getElementById('qa_descrip');
		var dest_de2= document.getElementById('qa_descrip2');

		//set the values
		dest_de.firstChild.nodeValue=descrip;
		dest_de2.firstChild.nodeValue=descrip2;

		isWorking=false;
	  }
  }
}
function enter() {
  r=true;
  if(window.event && window.event.keyCode == 13) {
 	r=false;
	updatePartNo();
  }
  return r;
}

/*Standard function for opening a window. Moved from inctoolbar.php
 on August 28, 2007. All javascript should be kept together if
 possiblt */
function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);

}

function closeBox()
{
/*Hide the box on close event*/
	obj = document.getElementById("qar_informationbox");
	obj.style.visibility = 'hidden';
}

