// K&L Wines Scripts
function checkUncheckAll(theElement) { 
     var theForm = theElement.form, z = 0; 
      for(z=0; z<theForm.length;z++) 
      { 
          if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') 
          { 
               theForm[z].checked = theElement.checked; 
          } 
     } 
}

var toggle = 0;
function displayPop(which,toggle) {
	varDiv = document.getElementById("popcontainer"+which);
	if(varDiv.style.visibility == 'hidden' || toggle == 1) {
		varDiv.style.visibility = 'visible';
	} else {
		varDiv.style.visibility = 'hidden';
	}
}

function toggleBlock(which) {
	varDiv = document.getElementById("popcontainer"+which);
	if(varDiv.style.display == 'none') {
		varDiv.style.display = 'block';
	} else {
		varDiv.style.display = 'none';
	}
}

function toggleLayer(whichLayer, testCheckBox) {
	if (testCheckBox.checked) {
		document.getElementById(whichLayer).style.display = "";
	} else {
		document.getElementById(whichLayer).style.display = "none";
	}
}


function displayVPop(pos,which) {
	varDiv = "popcontainer"+which;
	setLyr(pos,varDiv,325);
	displayPop(which,1);
}

function displayGCVPop(pos,which) {
	varDiv = "popcontainer"+which;
	setLyr(pos,varDiv,275);
	displayPop(which,1);
}

function setLyr(obj,lyr,offset) {
	var newY = findPosY(obj);
	newY = newY - offset;
	document.getElementById(lyr).style.top = newY + 'px';
}

function findPosY(obj) {
	var curtop = 0;
	var printstring = '';
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			printstring += ' element ' + obj.tagName + ' has ' + obj.offsetTop;
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


// Inventory AJAX
function loadXMLDoc(url, formVars) {
	url = url + "?" + formVars;
	if (window.XMLHttpRequest) {
		// branch for native XMLHttpRequest object
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open('GET', url, true);
		req.send(null);
    } else if (window.ActiveXObject) {
		// branch for IE/Windows ActiveX version
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open('GET', url, true);
			req.send(null);
		}
	} else {
		alert("Not supported by your browser.");
	}
}	

function processReqChange() {
	//show loading...
	if(req.readyState == 2) {
		writeInv('popcontainer1',"<div class='pop-content'><div class='pop-title'><h2>Loading...</h2></div></div>");
	}
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			var result = req.responseXML.getElementsByTagName('result')[0].firstChild.data.toString();
			/*
			var response = req.responseXML.documentElement;
			var result = response.getElementsByTagName('result')[0].firstChild.data.toString();
			*/
			writeInv('popcontainer1',result);
		} else 	{
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function displayInventory(pos,urlstring,prodlink) {
	varDiv = "popcontainer1";
	writeInv(varDiv,'');
	url = "/MultiLoc.asp";
	strQuery = "sku="+urlstring+"&prodlink="+prodlink;
	/*alert(url+"?"+strQuery);*/
	
	loadXMLDoc(url,strQuery);
	setLyr(pos,varDiv,325);
	displayPop(1,1);
	
}

function writeInv(which,code) {
	if(document.all) {
		var z=eval("document.all."+ which +"")
        z.innerHTML=code;
	}else if(document.getElementById) {
		document.getElementById(which).innerHTML=code;
	}else{
		x=eval("document."+which)
		x.document.open()
		document.write(code)
		x.close()
	}
}

//GREG 2007-06-25 brought over from basket.asp
function ShowPopup(windowname, width, height) 
{
  	window.open(windowname, null, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+width+',height='+height);
	return;
}

//Greg - This function handles default enter behavior
//you will need to add event code to each control in the form
//requiring the event to fire:
//onkeypress="return defaultEnter(this,event)"
//It is essential that the button that should have default Enter behavior is named "Continue"
function defaultEnter(input, event, defaultControlName)
{   
    var bEnterClick = false;   
    if (defaultControlName == undefined)
    {
        defaultControlName = "Continue";
    }
    if (input.type != "image" && input.type != "submit")
    { 
        if (document.all)
        {
            if (event.keyCode == 13)
            {
                bEnterClick=true;
            }
        }
        else if (document.getElementById)
        {
            if (event.which == 13)
            {
                bEnterClick=true;
            }
        }
        else if(document.layers)
        {
            if(event.which == 13)
            {
                bEnterClick=true;            
            }
        }
        if (bEnterClick)
        {
            //fire the default enter code
            event.returnValue=false;
            event.cancel = true;
            document.getElementById(defaultControlName).click();
        }
        else
        {
            //click the control
            input.click();
        }
    }
}

//Greg - This function ensures that events are concatenated if 
//more than one happens in a single page
function updateOmnitureEvents(sEvents, sNewEvent) { 
     var retEvents = "";
     if (sEvents.length ==0)   
     {
        retEvents = sNewEvent;
     }
     else
     {
        retEvents = sEvents + "," + sNewEvent;
     }
     
     return retEvents;
}