/**
 * @author Jason Leach
 */

var progress_bar = new Image();
var foundError = 0;
progress_bar.src = '//www.chairking.com/graphics/4.gif';
//progress_bar.src = 'http://70.88.54.98/images/ajax-loader.gif';	

function getAJAXData(url, type, showLoading, pageElement) {
	if (showLoading == 1) {showObject('loading');}
	if (showLoading == 2) {windowLoading(pageElement);}
	// This funtion sets up the url to request the data from OpenInsight
	// We are using an OECGI.EXE connection to OpenInsight. 
	var sendData = null;
	
	if (type == "POST") 
	{
		sendData = url;
	}
	else
	{
		type = "GET";
	}
		
	try {
		req = new XMLHttpRequest(); /* e.g Firefox */
	} catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
			/* some versions of IE */
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
				/* some versions of IE */
			} catch (e) {
				req = false;
			}
		}
	}
	req.onreadystatechange = function() {responseAJAX(pageElement);};
	req.open(type,url,true);
	req.send(sendData);
}
function responseAJAX(pageElement) {
	var output = '';
	if (req.readyState == 4) {
		if (req.status == 200) {
			var httpResult = req.responseText;
			//var httpResult = req.responseXML;
			handleHttpResponse(httpResult,pageElement);
			//hideObject('loading');
		}
	}
}
function handleHttpResponse (httpResult, pageElement){
	//var pageElement = 'content';
	//var result = httpResult.split('|');
	var result = httpResult;
	
	document.getElementById(pageElement).innerHTML = result;
			
}
function loadXMLString(result,xmlDoc)
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(result);
  }
catch(e)
  {
  try // Firefox, Mozilla, Opera, etc.
    {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(result,"text/xml");
    }
  catch(e)
    {
    alert(e.message);
    return;
    }
  }
//document.getElementById("to").innerHTML= xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
//document.getElementById("from").innerHTML= xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
//document.getElementById("message").innerHTML= xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
	return xmlDoc;
}


function windowLoading(obj){
	var varhtml = "<img src='/graphics/frogjump.gif' alt='Loading...' /><br />Loading...";
	/*varhtml = "loading...";*/
	document.getElementById(obj).innerHTML = varhtml;
}

// Below are functions for page manipulation

function userLogin(){
	var url = '/cgi-bin/oecgi.exe/inet_customer_lookup_div';
	
	var obj = "";
	
	var phonearea = encodeURI(document.getElementById("PHONE_AREA").value);
	var phonepre = encodeURI(document.getElementById("PHONE_PRE").value);
	var phonepost = encodeURI(document.getElementById("PHONE_POST").value);
	var type = encodeURI(document.getElementById("R1").value);
	var password = encodeURI(document.getElementById("CUST_PW").value);
	
	var errorMessage = "";
	if (phonearea == '') {
		errorMessage = "Phone area cannot be left blank!";
		obj = "PHONE_AREA";
	}
	else 
		if (phonepre == '') {
			errorMessage = "Phone pre cannot be left blank!";
			obj = "PHONE_PRE";
		}
		else 
			if (phonepost == '') {
				errorMessage = "Phone post cannot be left blank!";
				obj = "PHONE_POST";
			}
			else 
				if (password == '') {
					errorMessage = "Password cannot be left blank!";
					obj = "CUST_PW";
				}
				else if (isNaN(phonearea) || isNaN(phonepre) || isNaN(phonepost))
				{
					if (isNaN(phonearea))
					{
						errorMessage = "Area code is invalid! Please enter numeric values only."
						obj = "PHONE_AREA";
					}
					else if (isNaN(phonepre))
					{
						errorMessage = "Phone number is invalid! Please enter numeric values only."
						obj = "PHONE_PRE";
					}
					else if (isNaN(phonepost))
					{
						errorMessage = "Phone number is invalid! Please enter numeric values only."
						obj = "PHONE_POST";
					}
				}
	if (errorMessage != "") {
		alert(errorMessage);
		document.getElementById(obj).focus();
		return false;
	}
	else {
		
		var fullphone = phonearea + "-" + phonepre + "-" + phonepost
		
	}
	var poststr = "?CUST_NO=" + fullphone;
	poststr += "&R1=" + type;
	poststr += "&CUST_PW=" + password;

	url += poststr;
	//var res = getXMLData(url);
	var res = getAJAXData(url,'GET',1,'CUSLOGIN');

	return false;
}

function showBlock(obj) {	
	obj = document.getElementById(obj);
	obj.style.display = "block";
}
function hideBlock(obj) {	
	obj = document.getElementById(obj);
	obj.style.display = "none";
}
function showObject(obj) {	
	obj = document.getElementById(obj);
	obj.style.visibility = "visible";
}

function hideObject(obj){
	obj = document.getElementById(obj);
	obj.style.visibility = "hidden";
}
function gotonewpage (obj) 
{
	var x = document.getElementById(obj);
	var theurl = x.value;
	//alert(theurl);
	window.location.assign(theurl);
}

function setShipping(checked) {
  if (checked) {
    document.getElementById("name").value = document.getElementById("billtoname").innerHTML;
    document.getElementById("addr1").value = document.getElementById("billtoaddress").innerHTML;
    document.getElementById("city").value = document.getElementById("billtocity").innerHTML;
    document.getElementById("state").value = document.getElementById("billtostate").innerHTML;
    document.getElementById("zip").value = document.getElementById("billtozip").innerHTML;
    document.getElementById("phone").value = document.getElementById("billtophone").innerHTML;
  } else {
    document.getElementById("name").value = "";
    document.getElementById("addr1").value = "";
    document.getElementById("city").value = "";
    document.getElementById("state").value = "AZ";
    document.getElementById("zip").value = "";
    document.getElementById("phone").value = "";
  }
}
function showQuickLook(linkid,url) {
  //showObject('salesLayer');
 
  //obj = document.getElementById('quicklook');
  //obj.style.display = "block";
  
  res = getAJAXData(url,'GET',0,'quicklook');
  //alert(url);
  showdeadcenterdiv(linkid,'quicklook');
  
  return false;
}

function hideQuickLook() {
  obj = document.getElementById('quicklook');
  obj.style.display = "none";
  obj.innerHTML = "";
}
function showdeadcenterdiv(linkid,divid) { 
  // First, determine how much the visitor has scrolled
  //alert(divid);
  var scrolledX, scrolledY; 
  if( self.pageYOffset ) { 
  scrolledX = self.pageXOffset; 
  scrolledY = self.pageYOffset; 
  } else if(document.documentElement && document.documentElement.scrollTop ) { 
  scrolledX = document.documentElement.scrollLeft; 
  scrolledY = document.documentElement.scrollTop; 
  } else if(document.body ) { 
  scrolledX = document.body.scrollLeft; 
  scrolledY = document.body.scrollTop; 
  }
  
  // Next, determine the coordinates of the center of browser's window
  
  var centerX, centerY; 
  if( self.innerHeight ) { 
  centerX = self.innerWidth; 
  centerY = self.innerHeight; 
  } else if( document.documentElement && document.documentElement.clientHeight ) { 
  centerX = document.documentElement.clientWidth; 
  centerY = document.documentElement.clientHeight; 
  } else if( document.body ) { 
  centerX = document.body.clientWidth; 
  centerY = document.body.clientHeight; 
  }
  
  // get link location
  var qlink = document.getElementById(linkid);

  linkleft = 0;
  linktop = 0;
  if (qlink.offsetParent) {
  //linkwidth = qlink.style.width;
  qlink = qlink.offsetParent;
  linkleft += qlink.offsetLeft;
  linktop += qlink.offsetTop;
  }
  //alert(linkleft + " " + linktop + " " + centerX + " " + centerY + " " + scrolledX + " " + scrolledY);
  var left = 0;
  var top = 0;
  newcenterX = (scrolledX + centerX) / 2;
  newcenterY = (scrolledY + centerY) / 2;

  var o=document.getElementById(divid);
  var r=o.style; 
  r.position='absolute'; 
  
  newYtop = scrolledY + 100;
  newYbot = scrolledY + centerY - 100;
  if (linktop < newYtop) {
    r.top = linktop + "px";
  } else {
    r.top = scrolledY + 100 + "px";
  }
  
  if (linkleft > newcenterX) {
    r.left = linkleft - 365 + "px";
  } else {
    r.left = linkleft + 50 + "px";
  }
  //alert(r.left + " " + linkleft + " " + centerX);
  //var Xwidth = o.style.width;
  //var Yheight = o.style.height;
  // Xwidth is the width of the div, Yheight is the height of the 
  // div passed as arguments to the function: 
  ///var leftOffset = scrolledX + (centerX - Xwidth) / 2; 
  ///var topOffset = scrolledY + (centerY - Yheight) / 2; 
  // The initial width and height of the div can be set in the 
  // style sheet with display:none; divid is passed as an argument to // the function 

  //var o=document.getElementById(divid);
 // var r=o.style; 
  //alert(Xwidth + ' ' + Yheight);
  //r.position='absolute'; 
  //r.top = '400px'; //topOffset + 'px'; 
  //r.left = '400px'; //leftOffset + 'px'; 
  r.zIndex = 3;
  r.display = "block"; 
  
  return false;
}

// CUSHION SCRIPTS //
function resetcushions() {
	var myDiv = document.getElementById("cushiondiv");
	var inputArr = myDiv.getElementsByTagName("img");
	for (var i = 0; i < inputArr.length; i++) {
		inputArr[i].border="0";
		inputArr[i].style.marginLeft="3px";
		inputArr[i].style.marginTop="3px";
		inputArr[i].style.marginRight="3px";
		inputArr[i].style.marginBottom="3px";
	}
}

function selectcushion(obj,sku,desc,retail) {
	resetcushions();
	//alert(obj +" "+ sku +" "+ desc +" "+ retail);
	var imgobj = document.getElementById(obj);
	imgobj.style.marginLeft="2px";
	imgobj.style.marginTop="2px";
	imgobj.style.marginRight="2px";
	imgobj.style.marginBottom="2px";
	imgobj.border="1";
	document.getElementById("cushion").value=sku;
	var temptxt = '<b>Cushion Qty</b>:&nbsp;<input type="text" name="cushionqty" id="cushionqty" value="1" size="2">';
	document.getElementById("cdesc1").innerHTML=temptxt; //desc;
	document.getElementById("cdesc2").innerHTML=desc;
	document.getElementById("cretail").innerHTML=retail;
}
// END OF CUSHION SCRIPTS //
