// JavaScript Document

var xmlHttp;
var color1;
var productid;
function displayimage(val,id)
{ 
xmlHttp=GetXmlHttpObject();
color1 = val;
productid = id;
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 

var url = ajax_url+"displayimage_ajax.php";
url=url+"?color="+val+"&iProductId="+id;
//url=url+"&sid="+Math.random();
//alert(url);
//window.location = url;

xmlHttp.onreadystatechange=stateChanged ;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}


function stateChanged() 
{ 
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
	 
  var html ="";
	var xmlDocument=xmlHttp.responseXML;
	
	var totgalleryimage = xmlDocument.getElementsByTagName('galleryimage').length;
	var image = xmlDocument.getElementsByTagName('image').item(0).firstChild.data;
  var largeimage = xmlDocument.getElementsByTagName('largeimage').item(0).firstChild.data;
  var totalsize = xmlDocument.getElementsByTagName('totalsize').item(0).firstChild.data;
  //alert(totalsize);return false;
  if(totalsize > 0)
  {
    removeoption(document.getElementById('sizeattr'));
    
    for(i=0;i<totalsize;i++)
    {
      addOption(document.getElementById('sizeattr'),xmlDocument.getElementsByTagName("sizeoption")[i].childNodes[0].nodeValue,xmlDocument.getElementsByTagName("sizeoption")[i].childNodes[0].nodeValue,xmlDocument.getElementsByTagName("sizestock")[i].childNodes[0].nodeValue);
    }
  }
  else
  {
    removeoption(document.getElementById('sizeattr'));
  }
  
  
  if(totgalleryimage > 0)
  {
  for(i=0;i<totgalleryimage;i++)
  {
    var galleryimage = xmlDocument.getElementsByTagName("galleryimage")[i].childNodes[0].nodeValue
  }
 
  html += "<table style='border:1px solid #2E2E2E;' border='0' cellspacing='0' cellpadding='0'>";
  html += "<tr>";
  html += "<td>";
  html += "<div style='width:360px;height:122px;overflow-x:scroll; overflow-y: hidden; SCROLLBAR-FACE-COLOR:#2C2C2C; SCROLLBAR-HIGHLIGHT-COLOR:#636363; SCROLLBAR-SHADOW-COLOR: #666666; SCROLLBAR-3DLIGHT-COLOR: #2C2C2C; SCROLLBAR-ARROW-COLOR: #F0CBBA; SCROLLBAR-TRACK-COLOR:#333335; SCROLLBAR-DARKSHADOW-COLOR: #2C2C2C; SCROLLBAR-BASE-COLOR: #666666;'>";
  
  html += "<table>";
  html += "<tr><td width='33%'>&nbsp;</td>";
  html += "<td width='33%'>&nbsp;</td>";
  html += "<td width='33%'>&nbsp;</td></tr>";
  
  html += "<tr>";
  for(j=0;j<totgalleryimage;j++)
	{
    html += "<td align='center'>";
	  html += "<table width='115' border='0' cellpadding='0' cellspacing='0'>";
	  html += "<tr><td align='center' id='change_image_td'>";
	  html += '<img name=img_'+j+' id=img_'+j+' style="cursor:pointer; border: 1px solid #7A7A7A;" src='+xmlDocument.getElementsByTagName('galleryimage')[j].childNodes[0].nodeValue+' onclick="return change_image(this.name,'+xmlDocument.getElementsByTagName('id').item(0).firstChild.data+');">';
    html += "</td></tr></table></td>";              
  }
    html += "</tr></table></div></td></tr></table>";
  }
    
  
  document.getElementById('medium-image').src = image;
  document.getElementById('zoom-image').src = largeimage;
  
  formSend();
  /*
  if(totgalleryimage > 0)
  {
    document.getElementById('display_gallery').innerHTML = '';
    document.getElementById('display_gallery').innerHTML = html;
	}
	else
	{
    document.getElementById('display_gallery').innerHTML = '';
  }
  */
	/*
	if(totalsize > 0)
	 {
	   document.getElementById('size_combo').style.display = '' ;
	   document.getElementById('size_combo').innerHTML = sizecombo;
	  }
	 else
	   document.getElementById('size_combo').style.display = 'none' ;
	*/
 } 
}

function addOption(selectbox,text,value,stock )
{
  if(stock == '0')
     text = text + '- Out of stock'; 
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  selectbox.options.add(optn);
}

function removeoption(selectbox)
{
  var i;
  for(i=selectbox.options.length-1;i>0;i--)
  {
    selectbox.remove(i);
  }
}

function GetXmlHttpObject()
{
var xmlHttp=null;

try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
function formSend() 
{	
 var xmlPath = site_url + 'xmlData.php';
 var iPId = productid;
 var iCId= color1;
  document["scroller"].goHome(xmlPath,iPId,iCId);
}

/*
function getFlashMovie(movieName) 
{  
  return document[movieName];
}
function getFlashMovie(movieName) 
{
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function formSend()
{
	var obj = getFlashMovie("scroller");
  obj.loadXml();  
}
function getFlashMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
        return document[movieName];
    }
}
*/
