// JavaScript Document
function submitCart(){
	var stateField = document.getElementById('State');
	var selIndex = stateField.selectedIndex;
	if(selIndex > 0){
		return true;
	}else{
		alert("Please select your state.");
		return false;
	}
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}


function updateState(){
	var stateField = document.getElementById('State');
	var selIndex = stateField.selectedIndex;
	var stateElement = document.getElementById('x_state');
	stateElement.value = stateField.options[selIndex].value;
	SetCookie("state",stateField.options[selIndex].value,3);
	SetCookie("taxable","true",3)
	if(stateField.options[selIndex].value=="CA"){
		SetCookie("taxable","true",3);
	}else{
		SetCookie("taxable","false",3);
	}
	var showCartValue = "";
	if(window.location.href.indexOf("showCart") == -1){
		showCartValue = "&showCart=true"
	}
	window.location.href = window.location.href + showCartValue;
	//window.location.reload();
		

}
function policyPop(){
	var policyPosPixel = document.getElementById('policyPosY');
	var policyPosY = findPosY(policyPosPixel);
	
	var policyPop = document.getElementById('policyPop');
	policyPop.style.top = (policyPosY-370) + "px";
	policyPop.style.display="block";
	
	policyPop.innerHTML = "<iframe src='return_policy_pop.htm' width=600 height=349 frameborder=0 scrolling=no>";;
}

function closePolicy(){
	var policyPop = document.getElementById('policyPop');
	policyPop.style.display="none";
	policyPop.innerHTML = "";
}

function viewProductImage(img){
	document.getElementById('mainImage').src="annieadminpanel/cropscale.aspx?do=preview&img=catalog_images/"+img+"&height=300&width=475";
	document.getElementById('zoomImage').src="annieadminpanel/cropscale.aspx?do=preview&img=annieadminpanel/interface_img/pixel.jpg&height=1&width=1";
	document.getElementById('currentImage').value=img;
}

function adjustItemCount(to){
	if(isNaN(parseInt(document.getElementById('itemCount').value))){
		document.getElementById('itemCount').value = 1;
	}else
	if(to=="down" && parseInt(document.getElementById('itemCount').value)> 0){
		 document.getElementById('itemCount').value =  parseInt(document.getElementById('itemCount').value) - 1;
	}else
	if(to=="up"){
		 document.getElementById('itemCount').value =  parseInt(document.getElementById('itemCount').value) + 1;
	}
}

var isRolled = false;
//var diagField = document.getElementById('classVal');

function showHideZoom(toDo){
	var div1 = document.getElementById('prodZoom');
	var prodSpacer = document.getElementById('prodSpacer');
	var proSpacerX = findPosX(prodSpacer);
	var proSpacerY = findPosY(prodSpacer);
	if(toDo=="show"){
		document.getElementById('zoomImage').src="annieadminpanel/cropscale.aspx?do=preview&img=catalog_images/"+document.getElementById('currentImage').value+"&height=700&width=1024";
		viewCart('hide');
		//setTimeout ( "dragTimer()", 100 );
		isRolled = true;
		div1.style.display="block";
		div1.style.top = proSpacerY + "px";
		div1.style.left = proSpacerX + "px";
	}else
	if(toDo=="hide"){
		//clearTimeout ( dragTimer );
		div1.style.display="none";
		isRolled = false;
	}
}

//function dragTimer(e){
	//document.getElementById('classVal').value = getPosition(event).x;
	//document.getElementById('classVal').value = mousePos.x;//(document.getElementById('mainImage').width + findPosX(document.getElementById('mainImage')));
	
//}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


//------------------------------------------------------------------------------
var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

var tempX = 0
var tempY = 0
var xPercent = 0;
function getMouseXY(e) {
	if(isRolled){
	  if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	  } else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX
		tempY = e.pageY
	  }  
	  // catch possible negative values in NS4
	  if (tempX < 0){tempX = 0}
	  if (tempY < 0){tempY = 0}  
	  // show the position values in the form named Show
	  // in the text fields named MouseX and MouseY
	  	xPercent = ((tempX-findPosX(document.getElementById('mainImage')))/document.getElementById('mainImage').width)*100;
		yPercent = ((tempY-findPosY(document.getElementById('mainImage')))/document.getElementById('mainImage').height)*100;
		//document.getElementById('classVal').value = Math.round(((document.getElementById('zoomImage').height*yPercent)/100));
		
		document.getElementById("prodZoom").scrollTop = Math.round(((document.getElementById('zoomImage').height*yPercent)/100))-150;
		document.getElementById("prodZoom").scrollLeft = Math.round(((document.getElementById('zoomImage').width*xPercent)/100)) - 225;

	  return true
	}
}

function addToCart(catID,subCatID,prod,prodID,displayCount,page){
	var itemCount = document.getElementById("itemCount").value;
	if(parseInt(itemCount) > 0 && itemCount != ""){
		document.location.href="cartTools.asp?do=add&category="+catID+"&subcategory="+subCatID+"&displayCount="+displayCount+"&page="+page+"&product="+prod+"&productID="+prodID+"&quantity="+document.getElementById("itemCount").value;
	}else{
		alert("Please enter a valid quantity and try again.");	
	}
}

function viewCart(toDo){
	var cartDiv = document.getElementById("viewCartDiv")
	var cartViewAlign = document.getElementById("cartViewAlign")
	
	var cartViewAlignX = findPosX(cartViewAlign);
	var cartViewAlignY = findPosY(cartViewAlign);
	if(toDo=="show"){
		//setTimeout ( "dragTimer()", 100 );
		isRolled = true;
		cartDiv.style.display="block";
		cartDiv.style.top = (cartViewAlignY-71) + "px";
		cartDiv.style.left = (cartViewAlignX-223) + "px";
	}else
	if(toDo=="hide"){
		//clearTimeout ( dragTimer );
		cartDiv.style.display="none";
		isRolled = false;
	}
}

function checkCart(toDo){
if(toDo=="true"){
	viewCart('show');
	}
}

function updateQuantity(itemQty){
	var validAmounts = true;
	for(i=0;i<parseInt(itemQty);i++){
		if(document.getElementById("itemCount"+i).value < 1 || isNaN(document.getElementById("itemCount"+i).value)){
			validAmounts = false;
		}
	}
	if(validAmounts){
		document.getElementById("cartForm").action = "cartTools.asp?do=updateQuantity&category=" + document.getElementById("category").value + "&subcategory=" + document.getElementById("subcategory").value + "&displayCount=" + document.getElementById("displayCount").value + "&page=" + document.getElementById("page").value + "&product=" + document.getElementById("product").value + "&showCart=true"
		document.cartForm.submit();
	}else{
		alert("Please enter valid quantity amounts for each item");
	}
}

