﻿//Global Ajax Server Page Name
//var AjaxServerPageName = "http://localhost/HVAppication/product_details.aspx"
var AjaxServerPageName = window.location;
var requestUrl = AjaxServerPageName;

//Global Declarations
//var idRetailPrice,idPrice,idSavings,idFrameStyle,idLensColor,idFlash,idSKU,vItemCode
var g_currentItemGuid;
//Request Xml
var xmlDoc,requestXml,xmlResponse;

//Global XMLHTTP Request object
var XmlHttp;

function loadStart( indicatorId ) {
    var oImageLoadProgress = document.getElementById(indicatorId);
    if (typeof oImageLoadProgress != 'undefined') 
        oImageLoadProgress.style.display = 'block';
        
    window.setTimeout("loadComplete('" + indicatorId + "')", 2000 );
}
function loadComplete( indicatorId ) {
    var oImageLoadProgress = document.getElementById(indicatorId);
    if (typeof oImageLoadProgress != 'undefined') 
        oImageLoadProgress.style.display = 'none';
}

function OnVariationComboChange( combo )
{
    SelectVariation( combo.value );
}

function SelectVariation( pItemGuid ) {
    try {
        PopulateItemDetails(pItemGuid);
        
        SwapMainImage(pItemGuid);
        
        SelectVariationImage( pItemGuid )
        
        UpdateChoice( pItemGuid );
        
        
        //SelectQuantity();
    }
    catch(e) {
        //alert(e);
    }
    
    //return false;
}

function SelectQuantity() 
{
    var oQty = document.getElementById(g_IDs.Quantity);
    if (!oQty)
        return;
        
    oQty.focus();
}


function SwapMainImage( pItemGuid )
{
    var oIDMainVariationImg = document.getElementById(g_IDs.MainPhoto);
    var newSrc = g_IDs.BaseImageUrl.replace( /00000000-0000-0000-0000-000000000000/, pItemGuid );
    
    if (oIDMainVariationImg.src != newSrc)
    {
        oIDMainVariationImg.src = newSrc;
        loadStart('imageLoadProgress');
    }
}

function SelectVariationImage( pItemGuid ) {
    var divVariationImages = document.getElementById(g_IDs.VariationImages);
    var elems = divVariationImages.getElementsByTagName('img');
    for (var i=0; i < elems.length; i++)
    {
        if (elems[i].id.match( /_imgPhoto$/ ))
        {            
            var oImg = elems[i];
            
            if ( oImg.src.match( 'item_guid=' + pItemGuid ) )
            {
                MarkImageSelected( oImg.id );
                return;
            }
        }
    }
}


function PopulateItemDetails( pItemGuid )
{
    if (g_currentItemGuid == pItemGuid)
        return;

    g_currentItemGuid = pItemGuid;
    
	CreateXmlHttp();
	
	if(XmlHttp)
	{
	    loadStart('dataLoadProgress');
	    
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		
		//Initializes the request object with POST (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("POST", requestUrl,  true);
		XmlHttp.setRequestHeader("Content-Type", "text/xml")
		
		var xml = '<reqXml><item_data item_guid="' + pItemGuid + '" /></reqXml>'
		XmlHttp.send(xml);		
	}	    
}

function MarkImageSelected( pVariationPhotoId )
{
    var oVariationPhoto = document.getElementById(pVariationPhotoId);
    if (oVariationPhoto)
    {
        var oVariationPhotoTable = oVariationPhoto.parentNode.parentNode.parentNode.parentNode;
        if (oVariationPhotoTable)
        {
            var elems = oVariationPhotoTable.getElementsByTagName('a');
            for (var i=0; i < elems.length; i++)
                elems[i].className = elems[i].className.replace( /mscSelected/, '' ).replace( /^\s+/, '');
                
            oVariationPhoto.parentNode.className = oVariationPhoto.parentNode.className + ' mscSelected';
        }
    }
}

function UpdateChoice( pItemGuid )
{
    var oItemAddForm = document.getElementById(g_IDs.ItemAdd);
    if (!oItemAddForm)
        return;
        
    var elems = oItemAddForm.getElementsByTagName('select');
    for (var i=0; i < elems.length; i++)
    {
        if (elems[i].id.match( /_cmbVariations$/ ))
        {            
            var oSelect = elems[i];
            oSelect.value = pItemGuid;
        }
    }
}

//Creating and setting the instance of appropriate XMLHTTP Request object to a "XmlHttp" variable  
function CreateXmlHttp()
{
    //Creating object of XMLHTTP in IE
    try
    {
	    XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
	    try
	    {
		    XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
	    catch(oc)
	    {
		    XmlHttp = null;
	    }
    }

    //Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof window.XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

//Called when response comes back from server
function HandleResponse()
{

    var resXml,RetailPrice,Price,YourSavings,FrameStyle,LensColor,Flash
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
        loadComplete('dataLoadProgress');

		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{	

			xmlResponse = XmlHttp.responseXML.documentElement;		

            // 'post' out the update to reduce flicker on IE6 whenever innerHTML is touched in the handler
			window.setTimeout( function() { UpdateFromResponse(xmlResponse); }, 10 );
	    }
	}
}


// Finishes up the update of the display from the ajax response.
function UpdateFromResponse(xmlResponse)
{
	xmlResponse = XmlHttp.responseXML.documentElement;		
	resData = xmlResponse.firstChild;
	
	//Getting the Required Objects
	RetailPrice = document.getElementById(g_IDs.RetailPrice);
	Price = document.getElementById(g_IDs.Price);
	YourSavings = document.getElementById(g_IDs.YourSavings);
	//SKU = document.getElementById(g_IDs.SKU);
	var PolarizedImage = document.getElementById(g_IDs.PolarizedImage);
	
	//if (SKU) SKU.innerHTML = resData.getAttribute('item_cd') ? resData.getAttribute('item_cd') : '';
    if (RetailPrice) RetailPrice.innerHTML = resData.getAttribute('retail_price') ? resData.getAttribute('retail_price') : '';
	if (Price) Price.innerHTML = resData.getAttribute('price') ? resData.getAttribute('price') : '';
	if (YourSavings) YourSavings.innerHTML = resData.getAttribute('savings') ? resData.getAttribute('savings') : '';

	for( var i=0; i < resData.childNodes.length; i++) {
	    var attrib = resData.childNodes[i];
	    
	    if(attrib.tagName == 'lableAvailable')
	    {
	        var labelAvailable = document.getElementById(g_IDs.lblAvailable);
	        labelAvailable.innerText = "";
	        if(attrib.getAttribute('value'))
	        {
	            labelAvailable.innerText = attrib.getAttribute('id') + " " + attrib.getAttribute('value');
	        }
	    }
	    
	    if (attrib.tagName == 'attrib') {
	        var trAttrib = document.getElementById('Attribute_' + attrib.getAttribute('id'));
	        var tdValue = document.getElementById('Attribute_' + attrib.getAttribute('id') + '_value');

	        if (trAttrib && tdValue) {
	            tdValue.innerHTML = attrib.getAttribute('value');
                trAttrib.className = ('' == attrib.getAttribute('value') ? 'hidden' : '');
            }
	    }
		if (attrib.getAttribute('id').toLowerCase() == "polarized")
	    {
	        if (attrib.getAttribute('value').toLowerCase() == "yes")
	        {
	            PolarizedImage.src = "http://stage.habervision.com/content/images/b_polarized.png";
	            PolarizedImage.style.display = "";
	        }
	        else if (attrib.getAttribute('value').toLowerCase() == "no")
	        {
	            PolarizedImage.src = "http://stage.habervision.com/content/images/b_nonpolar.png";
	            PolarizedImage.style.display = "";
	        }
	        else
	            PolarizedImage.style.display = "none";
	    }
	}
}


function closeAffinityValidation() {
    if (typeof g_Affinity == 'undefined')
        return false;
        
    var oAffinityValidation = document.getElementById(g_Affinity.ValidationId);
    if (oAffinityValidation)
        oAffinityValidation.style.display = 'none';
}

function show180View() {
    var popup = window.open(g_180Data.ViewerPage + '?item=' + escape(g_180Data.Item), '180View', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=400,width=600', true);
    popup.focus();
}