﻿//****************************************************************//
//Global Ajax Server Page Name
var AjaxServerPageName = window.location;
var requestUrl = AjaxServerPageName;

//Global Declarations
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 )
{
    if(combo.selectedIndex > 0)
        SelectVariation(combo.value);
    else
        return false;        
}
function SelectVariation( pItemGuid ) 
{
    try 
    {
        PopulateItemDetails(pItemGuid);
        //SwapMainImage(pItemGuid);
        //SelectVariationImage( pItemGuid )
        //UpdateChoice( pItemGuid );
    }
    catch(e) 
    { 
        //alert(e);
    }
}

function SwapMainImage( pItemGuid )
{
    var oMainImg = document.getElementById(g_IDs.MainPhoto);
    var oSmallMainImg = document.getElementById(g_IDs.SmallMainPhoto);
    var newSrc = g_IDs.BaseImageUrl.replace( /00000000-0000-0000-0000-000000000000/, pItemGuid );
    
    if (oMainImg.src != newSrc)
    {
        oMainImg.src = newSrc;
        oSmallMainImg.src = newSrc;
    }
}
function ChangeMainImg( pSmallMainImgId )
{
    var oMainImg = document.getElementById(g_IDs.MainPhoto);
    var oSmallMainImg = document.getElementById(pSmallMainImgId);
    oMainImg.src = oSmallMainImg.src;
}
//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;
	// 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)
{
    var ItemCd,ItemTitle,ItemDescription,ItemPrice;
	xmlResponse = XmlHttp.responseXML.documentElement;		
	resData = xmlResponse.firstChild;
	
	//Getting the Required Objects
	ItemCd = document.getElementById(g_IDs.ItemCd);
	//ItemTitle = document.getElementById(g_IDs.ItemTitle);
	//ItemDescription = document.getElementById(g_IDs.ItemDescription);
	ItemPrice = document.getElementById(g_IDs.Price);
	
	//Assigning the response output values (IE)
	ItemCd.innerText = resData.getAttribute("item_cd");
	//ItemTitle.innerText = resData.getAttribute("item_title");
	//ItemDescription.innerHTML = resData.getAttribute("item_description");
	ItemPrice.innerText = resData.getAttribute("price");
	
	//Assigning the response output values (FireFox)
	ItemCd.textContent = resData.getAttribute("item_cd");
	//ItemTitle.textContent = resData.getAttribute("item_title");
	//ItemDescription.textContent = resData.getAttribute("item_description");
	ItemPrice.textContent = resData.getAttribute("price");	
}
//****************************************************************//

