// JavaScript Document
var div2;
var textMsg;
//var e1;


function makeObject(){
var x; 
var browser = navigator.appName; 
//detect the client browser
if(browser == "Microsoft Internet Explorer"){
x = new ActiveXObject("Microsoft.XMLHTTP");
}else{
x = new XMLHttpRequest();
}
return x;
}

//call the function makeObject()
var request_main = makeObject();

function openAjaxWindow(url)
{
	        request_main.open('get',url,true);
			request_main.onreadystatechange = check_zipComplete; 
			request_main.send('');
}



function check_zipComplete()
{
	if(request_main.readyState == 4 || request_main.readyState == "complete"){ 
		   var answer = request_main.responseText;
		  
		   var div = document.getElementById('popup');
		 window.onscroll = function (e) {
					 if(div.style.visibility == 'visible')
					 {
								   if(window.pageYOffset)
									  deltaY =  window.pageYOffset;
								   else if(document.documentElement.scrollTop)	
									 deltaY =  document.documentElement.scrollTop;
									else if(document.body.scrollTop)
									   deltaY =  document.body.scrollTop;
									else
									deltaY = 0;
									
									 div.style.top = deltaY+"px";
					   }
		 }
				 
          
		  	
		   var mydata = document.getElementById('mydata');
		  // window.pageYOffset=0;
		   document.documentElement.scrollTop=0;
		   document.body.scrollTop=0;
		     //document.body.style.visibility='hidden';
		   div.style.visibility = 'visible';
		 
		  if(answer != '') 
		     mydata.innerHTML = answer;
		   //document.write(answer);
		   
		}
		else
		{
  		   
			if(document.getElementById('sbutton'))
			{
				        document.getElementById('sbutton').innerHTML = "<span class=heading-o>Wait ... </span>";
				
			}
		}
}

function openAjaxWindow1(url)
{
	         document.getElementById('popup').style.visibility='visible';
			 //document.body.style.visibility='hidden';
	         document.getElementById('mydata').innerHTML="<div align=center class=heading-o>Please Wait...</div>";
	        request_main.open('get',url,true);
			request_main.onreadystatechange = openAjaxWindowComplete1; 
			request_main.send('');
}

function openAjaxWindowComplete1()
{
	if(request_main.readyState == 4 || request_main.readyState == "complete"){ 
		   var answer = request_main.responseText;
		  
		   var div = document.getElementById('popup');
		 window.onscroll = function (e) {
					 if(div.style.visibility == 'visible')
					 {
								   if(window.pageYOffset)
									  deltaY =  window.pageYOffset;
								   else if(document.documentElement.scrollTop)	
									 deltaY =  document.documentElement.scrollTop;
									else if(document.body.scrollTop)
									   deltaY =  document.body.scrollTop;
									else
									deltaY = 0;
									
									 div.style.top = deltaY+"px";
					   }
		 }
				 
          
		  	
		   var mydata = document.getElementById('mydata');
		  // window.pageYOffset=0;
		   document.documentElement.scrollTop=0;
		   document.body.scrollTop=0;
		     //document.body.style.visibility='hidden';
		   div.style.visibility = 'visible';
		 
		  if(answer != '') 
		     mydata.innerHTML = answer;
		   //document.write(answer);
		   
		}
		else
		{
  		   
			if(document.getElementById('sbutton'))
			{
				        document.getElementById('sbutton').innerHTML = "<span class=heading-o>Wait ... </span>";
				
			}
		}
}

/***********************************    Dragging Div    ************************************************/////*/*/*/*/*/*/

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser2 = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser2.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser2.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser2.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser2.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser2.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser2.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser2.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser2.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser2.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser2.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser2.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser2.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

function check_zip_international()
{
	//alert('ajax');
	
	var obj = makeObject();
	var myzip2 = document.getElementById('zipCode').value;
	var url = "/mei/ajax_scripts/ajax.php?zip="+myzip2+"&type=zip_check_int";
	//alert(url);
	var check_zip = "sdfds";
	//var answer;
	obj.open('get',url,true);
	obj.onreadystatechange = get_zip_completed; 
	obj.send('');
	
	function get_zip_completed()
    {
		if(obj.readyState == 4 || obj.readyState == "complete")
		{ 
			  var answer = obj.responseText;
			  //alert(answer);
			  //return answer;
			  //check_zip = answer;
			  //ch12(answer);
			 //document.getElementById('zip').value = answer;
			  if(answer == "Not Found")
			  {
				  document.getElementById("zip_message2").innerHTML = "Not in our database";
				  //return false;
				  //document.products_store.submit() = false;
				  return empty_cart_on_start_over();
			  }
			  else
			  {
			   	  document.products_store.submit();
			  }
		}
		
     }
	 //check_zip = document.getElementById('zip').value;
	 //alert(check_zip);
	// return check_zip;
}

function ch12(answer)
{
	return answer;	
}

function add_products_into_cart(event1, productID, prodQty, productPrice, productName, td_contents, div1, span_contents, groupName, itemCount) 
{	
	var productQTY = document.getElementById(prodQty).value;
	
	if(productQTY < 1)
	{
		alert('Quantity should not be less than 1');	
	}
	else if(!IsNumeric(productQTY))
	{
		alert('This is invalid Quantity');	
	}
	else
	{
		
		document.getElementById(span_contents).style.backgroundColor = "#F7F8A7";
		var productID = document.getElementById(productID).value;
		var productPrice = document.getElementById(productPrice).value;
		var productName = document.getElementById(productName).value;
		
		
		div2 = div1;
		var text1;

		url = '/ajax_scripts/ajax.php';
		param = 'product_id=' + productID + '&product_qty=' + productQTY + '&product_price=' + productPrice + '&product_name=' + productName + '&group_name=' + groupName + '&type=cart_add&rand='+ Math.floor(Math.random() * 1000000);
		target = document.getElementById('bubble_tooltip_content');	
		myAjax = new Ajax.Updater(target,url,{method: 'get',parameters: param, asynchronous:true, evalScripts:true});
		document.getElementById(div2).style.display = "block";
		
		setTimeout("hideMessage(div2)",5000);
		showToolTip(event1, productName+" product successfully entered into cart");
		setTimeout("hideToolTip()",5000);
	}

}


function add_cabinets_into_cart(event1, cabinetId, cabinetName, cabinetPrice, quantity, div1, tr1, style_name) 
{	
	//document.getElementById(tr1).style.backgroundcolor = "#F7F8A7";
	document.getElementById(tr1).bgColor = "#F7F8A7";
	//alert(style_name);
	//alert(document.getElementById(quantity).value);
	var cabinetID  = document.getElementById(cabinetId).value;
	var cabinetNAM = document.getElementById(cabinetName).value;
	var cabinetPRC = document.getElementById(cabinetPrice).value;
	var cabinetQTY = document.getElementById(quantity).value;
	//var subTotal = cabinetQTY * cabinetPRC;
	//alert(div1);
	div2 = div1;
	var text1;
	//e1 = event1;
	
	url = "/ajax_scripts/ajax.php";
	param = "cabinet_id=" + cabinetID + "&cabinet_name=" + cabinetNAM + "&style_name1=" + style_name + "&cabinet_prc=" + cabinetPRC + "&cabinet_qty=" + cabinetQTY + "&type=cabinet_cart_add&rand="+ Math.floor(Math.random() * 1000000);
	target = document.getElementById('bubble_tooltip_content');	
	myAjax = new Ajax.Updater(target,url,{method: 'get',parameters: param, asynchronous:true, evalScripts:true});
	document.getElementById(div2).style.display = "block";
	  
	 /*new Ajax.Request(url, { 
							   method: 'get',
							   onSuccess : function(resp) 
							   { 
								 text1 = resp.responseText; 
								 newtesting=text1;
								  //alert(newtesting);
								 //showToolTip(e1,text1);
							   }, 
							   onFailure : function(resp) 
							   { 
								 alert("Oops, there\'s been an error."); 
							   }, 
							   parameters : param
       						}
						);*/
	  
//	alert(text1);
//	alert(text1);
	//var text = Ajax.responseText;
	//alert(text);
	setTimeout("hideMessage(div2)",5000);
	showToolTip(event1, style_name+"^"+cabinetNAM+" successfully entered into cart");
	setTimeout("hideToolTip()",5000);
}

function add_parts_into_cart(event1, partId, partName, partPrice, quantity, div1, tr1, style_name) 
{	
	//document.getElementById(tr1).style.backgroundcolor = "#F7F8A7";
	document.getElementById(tr1).bgColor = "#F7F8A7";
	//alert(style_name);
	//alert(document.getElementById(quantity).value);
	var partID  = document.getElementById(partId).value;
	var partNAM = document.getElementById(partName).value;
	var partPRC = document.getElementById(partPrice).value;
	var partQTY = document.getElementById(quantity).value;
	//var subTotal = cabinetQTY * cabinetPRC;
	//alert(div1);
	div2 = div1;
	var text1;
	//e1 = event1;
	
	url = "/ajax_scripts/ajax.php";
	param = "part_id=" + partID + "&part_name=" + partNAM + "&style_name1=" + style_name + "&part_prc=" + partPRC + "&part_qty=" + partQTY + "&type=parts_cart_add&rand="+ Math.floor(Math.random() * 1000000);
	target = document.getElementById('bubble_tooltip_content');	
	myAjax = new Ajax.Updater(target,url,{method: 'get',parameters: param, asynchronous:true, evalScripts:true});
	document.getElementById(div2).style.display = "block";
	  
	 /*new Ajax.Request(url, { 
							   method: 'get',
							   onSuccess : function(resp) 
							   { 
								 text1 = resp.responseText; 
								 newtesting=text1;
								  //alert(newtesting);
								 //showToolTip(e1,text1);
							   }, 
							   onFailure : function(resp) 
							   { 
								 alert("Oops, there\'s been an error."); 
							   }, 
							   parameters : param
       						}
						);*/
	  
//	alert(text1);
//	alert(text1);
	//var text = Ajax.responseText;
	//alert(text);
	setTimeout("hideMessage(div2)",5000);
	showToolTip(event1, partNAM+" successfully entered into cart");
	setTimeout("hideToolTip()",5000);
}

function add_cabinets_into_cart_from_desc(event1, cabinetId, cabinetName, cabinetPrice, quantity, div1, tr1, style_name)
{	
	//document.getElementById(tr1).style.backgroundcolor = "#F7F8A7";
	document.getElementById('tr1').bgColor = "#F7F8A7";
	//alert(style_name);
	//alert(document.getElementById(quantity).value);
	var cabinetID  = cabinetId;
	var cabinetNAM = cabinetName;
	var cabinetPRC = cabinetPrice;
	var cabinetQTY = quantity;
//	alert(style_name); return false;
	//var subTotal = cabinetQTY * cabinetPRC;
	//alert(div1);
	div2 = div1;
	var text1;
	//e1 = event1;
	
	url = "/ajax_scripts/ajax.php";
	param = "cabinet_id=" + cabinetID + "&cabinet_name=" + cabinetNAM + "&style_name1=" + style_name + "&cabinet_prc=" + cabinetPRC + "&cabinet_qty=" + cabinetQTY + "&type=cabinet_cart_add&rand="+ Math.floor(Math.random() * 1000000);
	target = document.getElementById('bubble_tooltip_content');	
	myAjax = new Ajax.Updater(target,url,{method: 'get',parameters: param, asynchronous:true, evalScripts:true});
	document.getElementById(div2).style.display = "block";
	
	 /* new Ajax.Request(url, { 
							   method: 'get',
							   onSuccess : function(resp) 
							   { 
								 text1 = resp.responseText; 
								 //alert(text1);
								 //showToolTip(e1,text1);
							   }, 
							   onFailure : function(resp) 
							   { 
								 alert("Oops, there\'s been an error."); 
							   }, 
							   parameters : param
       						}
						);*/
	//alert(text1);
	//alert(text1);
	//var text = Ajax.responseText;
	//alert(text);
	setTimeout("hideMessage(div2)",5000);
	showToolTip(event1, style_name+"^"+cabinetNAM+" successfully entered into cart");
	setTimeout("hideToolTip()",5000);
}

function hideMessage(div1)
{
	document.getElementById(div1).style.display = "none"; 
	
}

function IsNumeric(strString)//  check for valid numeric strings 
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	  {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
		 {
		 blnResult = false;
		 }
	  }
	return blnResult;
}

function update_cart(id, elementID, zipCode) 
{
	var productQTY = document.getElementById(elementID).value;	
	if(productQTY < 1)
	{
		alert('Quantity should not be less than 1');	
	}
	else if(!IsNumeric(productQTY))
	{
		alert('This is invalid Quantity');	
	}
	else
	{
		//	alert(id + '--------------' + productQTY);
		url = "/ajax_scripts/ajax.php";
		param = "id=" + id + "&product_qty=" + productQTY + "&type=cart_update&rand="+ Math.floor(Math.random() * 1000000);
		target = "my_div";	
		myAjax = new Ajax.Updater(target,url,{method: 'get',parameters: param, asynchronous:true, evalScripts:true});
		document.getElementById('my_div').style.display = "block";
		setTimeout("hideMessage()",5000);
		setTimeout('refresh('+zipCode+');', 1*1000);
		//setTimeout('Hide(('+num+');'),500);
	}
}

function delete_cart_item(id, zipCode) 
{
	var agree = confirm("Are you sure you want to remove this item from your shopping cart?");

	if(agree)
	{
		//	alert(id + '--------------' + productQTY);
		url = "/ajax_scripts/ajax.php";
		param = "id=" + id + "&type=delete_cart_item&rand="+ Math.floor(Math.random() * 1000000);
		target = "my_div";	
		myAjax = new Ajax.Updater(target,url,{method: 'get',parameters: param, asynchronous:true, evalScripts:true});
		document.getElementById('my_div').style.display = "block";
		setTimeout("hideMessage()",5000);
		setTimeout('refresh('+zipCode+');', 1*1000);
		//setTimeout('Hide(('+num+');'),500);
	}
	else
		return false;
}

function refresh(zipCode) {
	window.location.href = 'cart.php';
	if(zipCode)
		document.products_store.submit();
}

function get_logged_in(user_name, user_pass)
{
	
	//document.getElementById('out_forgot_password').innerHTML = "&nbsp;<img src='../images/working.gif' title='Processing your Request'>"; // show loading icon
	//alert(document.myform.email_address.value);return false;
	url = "../ajax_scripts/ajax.php";
	param = "user_name=" + user_name + "&user_pass=" + user_pass + "&type=get_logged_in&rand="+ Math.floor(Math.random() * 1000000);
	target = "out_login_box"; // span or div id, where the value will be returned
		
	myAjax = new Ajax.Updater(target,url,{method: 'get',parameters: param, onComplete: getResponse, asynchronous:true, evalScripts:true});
}

/* ajax.Response */  
function getResponse(oReq)
{
	if(oReq.responseText == 'Please wait. You are getting logged in...')
		window.location = 'quote_project_name.php';
}
