﻿// JScript File

//used to position popups
var posleft = 0;
var posright = 0;

//Used to store current stockitem status. 
var stockitemWithOptions = ""; 

function getOptionPosition(curelem)
{    
    for (posleft = 0, postop = 0; curelem.offsetParent; curelem = curelem.offsetParent) 
	{
		posleft += curelem.offsetLeft;
		postop += curelem.offsetTop;
	}
}

//function checks if qty is multiples of boxquantity field
function validateqty(boxQuantity, txtboxqty)
{
    elemqty = document.getElementById(txtboxqty);    

    if (elemqty.value == '' || elemqty.value == 0){
        alert("Please enter the quantity.");            
        elemqty.focus();
        return false;
    }
    
    var qty = parseInt(elemqty.value);
    var qtyextra = qty%boxQuantity;
    if (qtyextra != 0 && boxQuantity != 0){        
        alert("Order must be in multiples of " + boxQuantity + ".\nPlease order " + (qty + (boxQuantity-qtyextra)) + ".");
        elemqty.focus();
        return false;
    }
    
    return true;
}


function validateaddtocart(txtboxqty)
{        
    var curproductid = document.getElementById("productid").value;    
    var boxQuantity = document.getElementById("boxquantity").value;
    var price = document.getElementById("price").value;

    if (stockitemWithOptions == "")//then it was not set with options. So get the stockitem value for this product
        stockitemWithOptions = document.getElementById("stockitem").value;
    
    if (validateqty(boxQuantity, txtboxqty) == true){
        //if not stockitem, then verify that customer wants to buy anyways
		if (stockitemWithOptions == "False"){
			if (!confirm ("This item has a 3-4 week lead time.\nDo you want to order this item anyways?"))
				return false;
		}
    
 		//Check if product exists in cart.
 		var curoptionsid = document.getElementById("hidoptionsid").value;
		var inelem = document.getElementsByTagName("INPUT");				
		var j=0;
		for (i=0;i<inelem.length;i++){
			if (inelem[i].id.indexOf("idproductcart") > -1){
				if (curproductid == document.getElementById("idproductcart" + j).value && curoptionsid == document.getElementById("idoptionscart" + j).value){
					if (!confirm("This product is already in your cart.\nDo you want to add more?"))
						return false;
				}
				j++;
			}
		}
		return true;       
    }
    else
        return false;
}

function scroll()
{
  scrollTo(0,0);
}

function showImageAddOn(curelem,image,classdescription)
{
    if (!image)
        return;
    document.getElementById("imghingeoption").src = "images/products/hingeoptions/" + image + ".jpg";
    document.getElementById("txthingeoption").innerHTML = classdescription;
    getOptionPosition(curelem);    
    document.getElementById("divhingeoption").style.left = (posleft + 50) + "px";
	document.getElementById("divhingeoption").style.top = (postop + 22) + "px";
    document.getElementById("divhingeoption").style.display = "";
}



//begining of options functions   

//function disables options that cannot be mixed with selected option. ex HT does not get NRP   
//function also adds/removes to hidoptionsid the current selected options
function updateOptions(curelem)
{
    var seloptionitemid = "";
    var totaloptionsid = "";
    var notavailable = "";  
    var stockitem = document.getElementById("stockitem").value;  
    
    var optiondetails = "";
    var optionimage = "";

    for (i=0; i<document.aspnetForm.elements.length; i++) 
    { 
        elem = document.aspnetForm.elements[i];
        if (elem.type == "checkbox" || elem.type == "radio")
        {
	        if (elem.checked)
	        { 
	            //Checkbox - optionid is in hidden field
	            if (elem.id.indexOf("uxOptionCheckBox") > -1)
	            {
	                chkelem = elem.id.replace("uxOptionCheckBox", "uxCheckboxIDHidden");	
	                seloptionitemid = document.getElementById(chkelem).value; 
	                if (totaloptionsid == "")
	                    totaloptionsid = seloptionitemid;
	                else
	                    totaloptionsid += "," + seloptionitemid;         
                
                    //get options that need to be disabled
                    for (var x=0;x<optionArray.length;x++)
                    {
                        if (optionArray[x][0] == seloptionitemid)
                        {                            
                            notavailable = optionArray[x][1];
                        }
                    }                
                }
                //radio - optionid is radio value
                else if (elem.id.indexOf("uxOptionRadioButtonList") > -1)
                {
                    chkelem = elem.id;	
	                seloptionitemid = document.getElementById(chkelem).value;  
	                
	                if (seloptionitemid != "")//check if option selected has a value (not standard)
	                {
                        if (totaloptionsid == "")
	                        totaloptionsid = seloptionitemid;
	                    else
	                        totaloptionsid += "," + seloptionitemid;    
	                } 
	                
	                //get options that need to be disabled
                    for (var x=0;x<optionArray.length;x++)
                    {
                        if (optionArray[x][0] == seloptionitemid)
                        {
                            notavailable = optionArray[x][1]; 
                            optiondetails = optionArray[x][4]; 
                            optionimage = optionArray[x][3];
                        }
                    }  
                }                
	        }
	    }
	}
	
	var curoptionitemid = "";
	
	//disable options that cannot be not mixed with selected option
	for (i=0; i<document.aspnetForm.elements.length; i++) 
    { 
        elem = document.aspnetForm.elements[i];
        if (elem.id.indexOf("uxOptionCheckBox") > -1)
        {
	        chkelem = elem.id.replace("uxOptionCheckBox", "uxCheckboxIDHidden");	
	        curoptionitemid = document.getElementById(chkelem).value; 
	        
	        if (curoptionitemid == notavailable)
	        {
	            //if (seloptionitemid = 104 && curoptionitemid == 1 && elem.checked == true)//customer previously checked NRP and now checked HT 
	            //{
	            //    alert("Hospital Tips do not have the NRP option. \nIt is cross pinned, which is regarded by \nANSI/BHMA standard as more secure than NRP.");
	            //}
	            elem.checked = false;
	            elem.disabled = true;	            
	        }
	        else	            
	            elem.disabled = false;
	    } 
    }
    
    //adds/remove to hidoptionsid the current selected options
	if (totaloptionsid == "")//then no options
	{    
	    document.getElementById("hidoptionsid").value = "";	    
	}
	else//there are options
	{	    
	    document.getElementById("hidoptionsid").value = totaloptionsid;
	    
	    //since there are options, set stockitem to false unless we will find a match.
	    stockitem = "False"; 
	    	    
	    //if selected options is a match with a stock item, then set sockitem to true
	    //totaloptionsid is sorted by optionitemid. And the options in stockOptionArray is sorted by optionitemid.
        for (var x=0;x<stockOptionArray.length;x++)
        {
            if (stockOptionArray[x][1] == totaloptionsid)
            {
                stockitem = "True"; 
                break;  
            }          
        }  
	}		
	
	//update stock item message
	var stockitemleadtime = document.getElementById("leadtime").value;	
	
	//if stock item or option lead time is also 3-4 weeks, then lead time remains the same
	if (stockitem == "True" || stockitemleadtime == "Ships within 3-4 weeks") 
	{
	    document.getElementById("spnStock").innerHTML = "<span>Lead Time: " + stockitemleadtime + "</span>";
	    document.getElementById("spnStock").className = "txtorange";
	    	    
	    stockitemWithOptions = "True";  
	}
	else //show new lead time
	{
	    document.getElementById("spnStock").innerHTML = "<span>Lead Time: Ships within 3-4 weeks</span>";
        document.getElementById("spnStock").className = "txtred";
        
        stockitemWithOptions = "False";
    }
    
    //display option on top under description and lead time
	document.getElementById("spnLatch").innerHTML = optiondetails;
	
    document.getElementById("imgLatchImage").src = optionimage;    
    if (optionimage != "")
        document.getElementById("spnLatchImage").style.display = "";
    else
        document.getElementById("spnLatchImage").style.display = "none";
}

//Show image of option
function showImage(curelem, fullimageurl, description)
{
    curelem.className = "txtLinkOver";
    
    //clear the for attribute so that a click on the label does not select the radio element
    curelem.parentNode.htmlFor = "";
    
    intxt = "<table cellpadding='0' cellspacing='0' align='center'>";
    intxt += "<tr><td><img src='" + fullimageurl + "'></td></tr>";
    intxt += "<tr><td align='center'><b>" + description + "</b></td></tr>";
    intxt += "</table>";
    document.getElementById("divdescription").innerHTML = intxt;
    
    //position the popopup
    getOptionPosition(curelem);	
	
	document.getElementById("divdescription").style.left = (posleft) + "px";
	document.getElementById("divdescription").style.top = (postop - 135) + "px";
    
    document.getElementById("divdescription").style.display = "";    
}

function hideImage(curelem)
{
    curelem.className = "txtLink";   
    document.getElementById("divdescription").style.display = "none";
}
//end of options functions


function changeTab()
{ 
    //Get a Handle to the Tab Behavior 
    var tabBehavior = $get('<%=TabContainer1.ClientID%>').control; 
    //Set the Currently Visible Tab 
    tabBehavior.set_activeTabIndex(1); 
}


