var XMLHttp;
var targetDiv;
var divToClear;
var fieldToFocus;

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}


function CreateXMLHttpRequest() {
	if (window.ActiveXObject) {
		ro = new ActiveXObject('Microsoft.XMLHTTP');
	} else if (window.XMLHttpRequest) {
		ro = new XMLHttpRequest();
	}
	return ro;
}

function writeContent(divName,data) {	
	document.getElementById(divName).innerHTML = data;
}

function fillField(theForm,textField,value) {
	document.getElementById(theForm).elements[textField].value = value;
}

function setData() {
	if ((XMLHttp.readyState == 4) || (XMLHttp.readyState == 0)) {
		if (XMLHttp.status == 200) {
			if (XMLHttp.responseText != '') {
				writeContent(targetDiv,XMLHttp.responseText);
			} else {
				setTimeout('setData()', 500);
			}
		}
	}
}

function checkCodeResponse(response) {
	respParts = response.split('|');
	if (respParts[0] == "ok") {
		writeContent(targetDiv,respParts[1]);
	} else {
		writeContent(targetDiv,respParts[1]);
		fieldToFocus.focus();
	}
}

function setDataWithFocus() {
	if ((XMLHttp.readyState == 4) || (XMLHttp.readyState == 0)) {
		if (XMLHttp.status == 200) {
			if (XMLHttp.responseText != '') {
				checkCodeResponse(XMLHttp.responseText);
			} else {
				setTimeout('setDataWithFocus()', 500);
			}
		}
	}
}

function splitResponse(response) {
	respParts = response.split('|');
	if (respParts[0] == "show") {
		writeContent(targetDiv,respParts[1]);
	} else {
		eval(respParts[1]);
	}
}

function setDataWithEval() {
	if ((XMLHttp.readyState == 4) || (XMLHttp.readyState == 0)) {
		if (XMLHttp.status == 200) {
			if (XMLHttp.responseText != '') {
				splitResponse(XMLHttp.responseText);
			} else {
				setTimeout('setDataWithEval()', 500);
			}
		}
	}
}

function setDataAndClear() {
	if ((XMLHttp.readyState == 4) || (XMLHttp.readyState == 0)) {
		if (XMLHttp.status == 200) {
			if (XMLHttp.responseText != '') {
				writeContent(targetDiv,XMLHttp.responseText);
				writeContent(divToClear,'');
			} else {
				setTimeout('setDataAndClear()', 500);
			}
		}
	}
}

function getData(url,arg,target) {
	method = 'GET';
	url += '?arg=' + escape(arg);
	targetDiv = target;
	XMLHttp = CreateXMLHttpRequest();
	XMLHttp.onreadystatechange = setData;
	XMLHttp.open(method,url);
	XMLHttp.send(null);
}

function checkCode(textField,target) {
	method = 'GET';
	url = '../ajax/checkDiscountCode.php?code=' + escape(textField.value);
	targetDiv = target;
	fieldToFocus = textField;
	XMLHttp = CreateXMLHttpRequest();
	XMLHttp.onreadystatechange = setDataWithFocus;
	XMLHttp.open(method,url);
	XMLHttp.send(null);
}

function checkExistingCode(textField,target,codeId) {
	method = 'GET';
	url = '../ajax/checkExistingDiscountCode.php?id=' + codeId;
	url += '&code=' + escape(textField.value);
	targetDiv = target;
	fieldToFocus = textField;
	XMLHttp = CreateXMLHttpRequest();
	XMLHttp.onreadystatechange = setDataWithFocus;
	XMLHttp.open(method,url);
	XMLHttp.send(null);
}

function checkCustomerCode(textField,target,session) {
	method = 'GET';
	url = 'ajax/getcustomerCode.php';
	str = textField.value;
	url += '?arg=' + str + '&s=' + session;
	if (str.length > 0) {
		targetDiv = target;
		XMLHttp = CreateXMLHttpRequest();
		XMLHttp.onreadystatechange = setData;
		XMLHttp.open(method,url);
		XMLHttp.send(null);
	}
}

function getRandomCode(checkBox,textField,target) {
	theForm = checkBox.form.id;
	method = 'GET';
	url = "../ajax/getRandomCode.php";
	if (checkBox.checked == 1) {
		url += "?form=" + escape(theForm);
		url += "&field=" + escape(textField);
		XMLHttp = CreateXMLHttpRequest();
		XMLHttp.onreadystatechange = setDataWithEval;
		XMLHttp.open(method,url);
		XMLHttp.send(null);
	} else {
		fillField(theForm,textField,'');
		writeContent(target,'');
	}
}

function clearBoth(target,callback) {
	writeContent(target,'');
	writeContent(callback,'-');
}

function showDiv(theDiv) {
	document.getElementById(theDiv).style.zIndex = 300;
	document.getElementById(theDiv).style.display = 'block';
}

function hideDiv(theDiv) {
	document.getElementById(theDiv).style.zIndex = -1;	
	document.getElementById(theDiv).style.display = 'none';
}

