﻿// JScript File

// gets the basic information about the product
function LoadProductInfo(idProd)
{
    try
	{
		obj=new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			obj = new ActiveXObject("Microsoft.XMLHTTP");			
		}
		catch(e1)
		{
			obj = null;
		}
	}	
	if(obj!=null)
	{
		obj.onreadystatechange = ProcessQuickQuoteInfo;
		obj.open("GET", "get-quickquoteinfo.asp?idpr="+ idProd,  true);
		obj.send(null);         
	}
	return false;
}

function ProcessQuickQuoteInfo()
{
	if(obj.readyState == 4)
	{	
		if(obj.status == 200)
		{
			var dsRoot=obj.responseXML.documentElement; 
			try
			{
				var idProducts = dsRoot.getElementsByTagName('id');
				var imageUrl = dsRoot.getElementsByTagName('img');
				var style = dsRoot.getElementsByTagName('style');
				var title = dsRoot.getElementsByTagName('title');
				document.getElementById('lblStyle').firstChild.data = style[0].firstChild.data;
				var titleLength = title[0].firstChild.data.length;
				if(titleLength > 60)
				{
				    document.getElementById('lblTitle').style.fontSize = '12px';
				}
				document.getElementById('lblTitle').firstChild.data = unescape(title[0].firstChild.data);
				var image = document.getElementById('imgMainImage');
				image.src = '/uploadimages/originalproductimages/' + imageUrl[0].firstChild.data;
    
                var idProduct = idProducts[0].firstChild.data;
                
                LoadProductImageCount(style[0].firstChild.data);
                
                SetProductId(idProduct, title[0].firstChild.data);
			}
			catch(e)
			{
			    alert(e.message);
			}
		}
	}
	return false;
}

// gets the product image count

function LoadProductImageCount(sku)
{
    try
	{
		obj=new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			obj = new ActiveXObject("Microsoft.XMLHTTP");			
		}
		catch(e1)
		{
			obj = null;
			alert(e1.message);
		}
	}	
	if(obj!=null)
	{
		obj.onreadystatechange = ProcessQuickQuoteImageCount;
		obj.open("GET", "get-quickquoteimagecount.asp?sku="+ sku,  true);
		obj.send(null);         
	}
	return false;
}

function ProcessQuickQuoteImageCount()
{
	if(obj.readyState == 4)
	{	
		if(obj.status == 200)
		{
			var imageCount = obj.responseText;
			try
			{
				var lblImageCount = document.getElementById('lblImageCount');
				lblImageCount.firstChild.data = imageCount;
			}
			catch(e)
			{
			}
		}
	}
	return false;
}

function SetProductId(prodId, title)
{
    //anchorImage, anchorProductDetail
    var anchorImage = document.getElementById('anchorImage');
    var anchorContact = document.getElementById('anchorContact');
    var productDetail = document.getElementById('anchorProductDetail');
    var imgSubmitQuote = document.getElementById('imgSubmitQuote');
    var imgUpdateQuote = document.getElementById('imgUpdateQuote');
    var idProduct = document.getElementById('idProduct');
    var hidtitle = document.getElementById('title');

    var a1 = "var win = window.open('productimage.asp?idpr=" + prodId + "','gallery','scrollbars=yes,status=no,width=530,height=650');win.focus();return false;";
    anchorImage.onclick = function(){var win = window.open("productimage.asp?idpr=" + prodId ,'gallery','scrollbars=yes,status=no,width=530,height=650');win.focus();return false;};

    var a2 = "view-product.asp?idpr=" + prodId;
    productDetail.setAttribute('href', a2);
    
    //imgSubmitQuote
    var a3 = "getQuickQuoteInfo(" + prodId + ");";
    imgSubmitQuote.onclick = function() {getQuickQuoteInfo(prodId);};
    
    var a4 = "submitQuickQuote(" + prodId + ")";
    imgUpdateQuote.onclick = function() {submitQuickQuote(prodId);};
    
    anchorContact.onclick = function() {var win = window.open("request-quote.asp?idpr=" + prodId + "&p=" + title,'gallery','scrollbars=yes,status=no,width=530,height=650');win.focus();return false;};
    
    //idproduct
    idProduct.value = prodId;
    hidtitle.value = title;
    
}


        function hideIt() {
            if (dom) 
            {
                document.getElementById("quickview-wrapper").style.visibility='hidden';
                document.getElementById("quickview").style.visibility='hidden';
                document.getElementById("embroid").style.visibility='hidden';
                document.getElementById("screen").style.visibility='hidden';
                document.getElementById("preQuoteSection").style.visibility='hidden';
                document.getElementById("postQuoteSection").style.visibility='hidden';
                document.getElementById("sizeOptionSection").style.visibility='hidden';
                document.getElementById("colorDropdown").style.display = "none";
                var topColor = document.getElementById("topLevelColor");
                removeChildNodes(topColor);
              
            }
        }

        function showIt(productId) {
            var y1 = 100;   // change the # on the left to adjuct the Y co-ordinate
            (document.getElementById) ? dom = true : dom = false;
            if (dom) 
            {
                document.getElementById("quickview-wrapper").style.visibility='visible';
                document.getElementById("quickview").style.visibility='visible';
                document.getElementById("screen").style.visibility='visible';
                document.getElementById("preQuoteSection").style.visibility='visible';
                LoadProductInfo(productId);
                LoadColorInfo(productId);
                
            }
        }

        function placeIt() {
            var y1 = 30;   // change the # on the left to adjuct the Y co-ordinate
            (document.getElementById) ? dom = true : dom = false;
            if (dom && !document.all) {document.getElementById("quickview-wrapper").style.top = window.pageYOffset + (window.innerHeight - (window.innerHeight-y1)) + "px";}
            if (document.all) {document.all["quickview-wrapper"].style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight - (document.documentElement.clientHeight-y1)) + "px";}
            window.setTimeout("placeIt()", 10); 
            
            document.onclick = function (e) {
	            e = e || event;
	            var srcElement = e.srcElement ? e.srcElement : e.target;
	            //alert(srcElement.id || srcElement.name);
	            var quickview = document.getElementById("quickview");
	            do {
		            if (quickview == srcElement) 
		            {
			            // Click occured inside the box, do nothing.
			            return;
		            }
		            srcElement = srcElement.parentNode;
	            } while (srcElement);
	            srcElement = e.srcElement ? e.srcElement : e.target;
	            if(srcElement.id != "imgGetQuote")
	            { // Click was outside the box, hide it.
	                //alert('test');
	                hideIt();
	            }
            }           
        }
        
        function LoadSizes(strColor,idProd)
{
    var hidSelectedColor = document.getElementById("hidSelectedColor");
    hidSelectedColor.value = strColor;
    var sizeDrpDwn=document.getElementById("productsize");
    var sColor=strColor;
	var idProduct=idProd;

	try
	{
		obj=new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			obj = new ActiveXObject("Microsoft.XMLHTTP");			
		}
		catch(e1)
		{
			obj = null;
		}
	}	
	if(obj!=null)
	{
		obj.onreadystatechange = ProcessSizeResult;
		obj.open("GET", "get-sizes.asp?idProduct="+ idProduct +"&sColor=" + sColor,  true);
		obj.send(null);         
	}
	return false;
}
function getQuickQuoteInfo(idProd)
{
    submitQuickQuote(idProd);
    
}
function submitQuickQuote(idProd)
{
    var prodColor = document.getElementById("hidSelectedColor");
    var sColor=prodColor.value;
	var idProduct=idProd;
	var workType = "";
	
	//get current sizes and quantities
    var sizes = document.getElementsByName('hSize');
    var sizeQuantities = document.getElementsByName('txtproductsize');
    var strSizes = "";
    var locations = "";
    var i;
    for (i = 0; i< sizes.length; i++)
    {
        strSizes += sizes[i].value + "|" + sizeQuantities[i].value + "|";
    }
    strSizes = strSizes.substring(0, strSizes.length - 1);
    var hidQSizes = document.getElementById('hidQSizes');
    var hidQLocColor = document.getElementById('hidQLocColor');
    hidQSizes.value = strSizes;
    
    //get work type
    var radWorkType = document.getElementsByName('workType');
    if (radWorkType[0].checked) 
    {
        workType = "Screen"
        var scrFront = document.getElementById('scrFrontColor');
        var scrBack = document.getElementById('scrBackColor');
        var scrLSleeve = document.getElementById('scrLSleevColor');
        var scrRSleeve = document.getElementById('scrRightColor');
        locations = scrFront.options[scrFront.selectedIndex].value + '|' + 
            scrBack.options[scrBack.selectedIndex].value + '|' + 
            scrLSleeve.options[scrLSleeve.selectedIndex].value + '|' + 
            scrRSleeve.options[scrRSleeve.selectedIndex].value;
        
    } 
    else 
    {
        workType = "Embroidery";
        var chkFront = document.getElementById('embFrontColor');
        if(chkFront.checked)
            locations += '1|';
        else
            locations += '0|';
        var chkBack = document.getElementById('embBackColor');
        if(chkBack.checked)
            locations += '1|';
        else
            locations += '0|';
            
        var chkHat = document.getElementById('embHatColor');
        if(chkHat.checked)
            locations += '1';
        else
            locations += '0';

        
    }
    hidQLocColor.value = locations;
    
    var val = checkQuickQuoteForProduct();
    if(val)
    {
        var q = document.getElementById('quote');
        q.value = "yes";
        
        try
        {
            obj=new XMLHttpRequest();
        }
        catch(e)
	    {       
		    try
		    {
			    obj = new ActiveXObject("Microsoft.XMLHTTP");			
		    }
		    catch(e1)
		    {
			    obj = null;
		    }
	    }	
	    if(obj!=null)
	    {
		    obj.onreadystatechange = ProcessQuoteResult;
		    obj.open("GET", "get-quote.asp?idProduct="+ idProduct +"&sColor=" + sColor + "&qty=" + strSizes + "&wt=" + workType + "&loc=" +locations,  true);
		    obj.send(null);         
	    }
	    return false;
    }
    else
    {
        alert('A quantity of 12 items is required for the quote. Please check your entries and try again.');
    }
    
}

function checkQuickQuoteForProduct()
{
    var isValid;
    var totalSizeCount = 0;
    var sizeCount = document.getElementsByName('txtproductsize');
    try
    {
        for (var i = 0; i< sizeCount.length; i++)
        {
            if(sizeCount[i].value != "")
            {
                totalSizeCount += parseInt(sizeCount[i].value);
            }
        }
        if(totalSizeCount >= 12)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    catch(e)
    {
        alert('There is a problem with the number of sizes that you have entered for the quote.  Please check these values and try again.');
    }
}

function submitQuantityOptions(idProd)
{
    var prodColor = document.getElementById("hidSelectedColor");
    var sColor=prodColor.value;
	var idProduct=idProd;
	var workType = "";
	
	//get current sizes and quantities
    var sizes = document.getElementsByName('hSize');
    var sizeQuantities = document.getElementsByName('txtproductsize');
    var strSizes = "";
    var locations = "";
    var i;
    for (i = 0; i< sizes.length; i++)
    {
        strSizes += sizes[i].value + "|" + sizeQuantities[i].value + "|";
    }
    strSizes = strSizes.substring(0, strSizes.length - 1);
    
    //get work type
    var radWorkType = document.getElementsByName('workType');
    if (radWorkType[0].checked) 
    {
        workType = "Screen"
        var scrFront = document.getElementById('scrFrontColor');
        var scrBack = document.getElementById('scrBackColor');
        var scrLSleeve = document.getElementById('scrLSleevColor');
        var scrRSleeve = document.getElementById('scrRightColor');
        locations = scrFront.options[scrFront.selectedIndex].value + '|' + 
        scrBack.options[scrBack.selectedIndex].value + '|' + 
        scrLSleeve.options[scrLSleeve.selectedIndex].value + '|' + 
        scrRSleeve.options[scrRSleeve.selectedIndex].value;
        
    } 
    else 
    {
        workType = "Embroidery";
        var chkFront = document.getElementById('embFrontColor');
        if(chkFront.checked)
            locations += '1|';
        else
            locations += '0|';
        var chkBack = document.getElementById('embBackColor');
        if(chkBack.checked)
            locations += '1|';
        else
            locations += '0|';
            
        var chkHat = document.getElementById('embHatColor');
        if(chkHat.checked)
            locations += '1';
        else
            locations += '0';   
    }    
        try
        {
            obj1=new XMLHttpRequest();
        }
        catch(e)
	    {       
		    try
		    {
			    obj1 = new ActiveXObject("Microsoft.XMLHTTP");			
		    }
		    catch(e1)
		    {
			    obj1 = null;
		    }
	    }	
	    if(obj1!=null)
	    {
		    obj1.onreadystatechange = ProcessOptionResult;
		    obj1.open("GET", "get-size-options.asp?idProduct="+ idProduct +"&sColor=" + sColor + "&qty=" + strSizes + "&wt=" + workType + "&loc=" +locations,  true);
		    obj1.send(null);         
	    }
	    return false;
}
function ProcessSizeResult()
{
	
	if(obj.readyState == 4)
	{	
		if(obj.status == 200)
		{
			
			if (obj.responseText != "")
			{
			    
				var dsRoot=obj.responseXML.documentElement; 
				try
				{
					var idColorSize = dsRoot.getElementsByTagName('idColorSize');
					
					var sSizeName= dsRoot.getElementsByTagName('sSize');
					var val,text; 
					val="";
					text="";
					
					var sizeArea = document.getElementById('productSizeArea');

					for (var count = 0; count < idColorSize.length; count++)
					{
					    val += (idColorSize[count].textContent || idColorSize[count].innerText || idColorSize[count].text) + "<br/>";
						
						text += "<span style='display:block;text-align:center;float:left;padding-left:25px;font-weight:bold;'>" + (sSizeName[count].textContent || sSizeName[count].innerText || sSizeName[count].text) +
						 "<br/><input name='txtproductsize' id='txtproductsize_" + (idColorSize[count].textContent || idColorSize[count].innerText || idColorSize[count].text) + 
						 "' type='text' style='border:solid 1px black;width:20px;' /><input type='hidden' name='hSize' value='" + (sSizeName[count].textContent || sSizeName[count].innerText || sSizeName[count].text) + "' /></span>";
						
					}
					
					sizeArea.innerHTML = text;
				}
				catch(e)
				{
					//alert(e)
					var val,text; 
					val="";
					text="Select";
					alert(e);
				}
			}
			else
			{
				var val,text; 
				val="";
				text="Select";
				sizeArea.innerHtml = text;
			}
		}
	}
	return false;
}	

function ProcessQuoteResult()
{
	if(obj.readyState == 4)
	{	
		if(obj.status == 200)
		{
			if (obj.responseText != "")
		    {
			    var responseArray = new Array();
			    responseArray = obj.responseText.split('|');
			    var text;
			    text = responseArray[3];
			    var quoteArea = document.getElementById('quoteArea');
			    var hidQuoteContact = document.getElementById('quoteprice');
			    hidQuoteContact.value = responseArray[0] + '|' + responseArray[1] + '|' + responseArray[2];
				
				try
				{			
					quoteArea.innerHTML = "<b>" + text + "</b>";
					
					var preQuoteSection = document.getElementById("preQuoteSection");
				    var postQuoteSection = document.getElementById("postQuoteSection");
				    var sizeOptionSection = document.getElementById("sizeOptionSection");
				    preQuoteSection.style.visibility = "hidden";
				    postQuoteSection.style.visibility = "visible";
				    
				    var idProd = document.getElementById('idProduct');
				    submitQuantityOptions(idProd.value);
				    
				}
				catch(e)
				{
				    alert(e);
				    var preQuoteSection = document.getElementById("preQuoteSection");
				    var postQuoteSection = document.getElementById("postQuoteSection");
				    var sizeOptionSection = document.getElementById("sizeOptionSection");
				    preQuoteSection.style.visibility = "hidden";
				    postQuoteSection.style.visibility = "visible";
					text="Cannot retrieve quote at this time.";
					quoteArea.innerHTML = text;
				}
			}
			else
			{
				text="Cannot retrieve quote at this time.";
				quoteArea.innerHtml = text;
			}
		}
	}
}

function ProcessOptionResult()
{
	if(obj1.readyState == 4)
	{	
		if(obj1.status == 200)
		{
			if (obj1.responseText != "")
			{
			    var sizeOptionSection = document.getElementById('sizeOptionSection');
				try
				{			
					sizeOptionSection.innerHTML =  obj1.responseText;
					
					var preQuoteSection = document.getElementById("preQuoteSection");
				    var postQuoteSection = document.getElementById("postQuoteSection");
				    var sizeOptionSection = document.getElementById("sizeOptionSection");
				    preQuoteSection.style.visibility = "hidden";
				    postQuoteSection.style.visibility = "visible";
				    sizeOptionSection.style.visibility = "visible";
				}
				catch(e)
				{
				    var preQuoteSection = document.getElementById("preQuoteSection");
				    var postQuoteSection = document.getElementById("postQuoteSection");
				    var sizeOptionSection = document.getElementById("sizeOptionSection");
				    preQuoteSection.style.visibility = "hidden";
				    postQuoteSection.style.visibility = "visible";
					sizeOptionSection.innerHTML = text;
				}
			}
			else
			{
				sizeOptionSection.innerHtml = '';
			}
		}
	}
}










