/*
 * gallery
 */
function swapImg(imgIndex, swapFile){
	var newImg = new Image();
	newImg.src=swapFile;
	newImg.onload = function () { document.images["img" + imgIndex].src = newImg.src; }
	newImg.src=swapFile;
}

function swapCode(swapDivId, swapHtmlCode)
{
    it = document.getElementById(swapDivId);

    it.innerHTML = swapHtmlCode;
    
}
/*
 * showMe
 */
function ShowMeImg(img,w,h){
	Result=window.open(img,"",(" width="+w+",height="+h+" ,status=yes"))
}
function ShowMeDoc(doc,w,h){
	Result=window.open(doc,"",(" width="+w+",height="+h+",scrollbars=yes"))
}

/*
 * xsTooltip
 */
function xstooltip_findPosX(obj) 
{
  var curleft = 0;
  if (obj.offsetParent) 
  {
    while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function xstooltip_findPosY(obj) 
{
    var curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function xstooltip_show(tooltipId, parentId, posX, posY)
{
    it = document.getElementById(tooltipId);
    
    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
        
        img = document.getElementById(parentId); 
    
        // if tooltip is too wide, shift left to be within parent 
        if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
        if (posX < 0 ) posX = 10; 
        
        x = xstooltip_findPosX(img) + posX;
        y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y + 'px';
        it.style.left = x + 'px';
    }
    
    it.style.visibility = 'visible'; 
}

function xswatch_show(swatchFile, swatchMsg, swatchId, swatchClass, parentId, posX, posY)
{
    it = document.getElementById(swatchId);
    if (it.className != swatchClass) it.className = swatchClass;


    it.innerHTML = '<img style="border-bottom:1px solid black; margin-left:0px; padding-left:0px; width:200px; height:120px;" src="shopimages/products/swatches/'+swatchFile+'.jpg" alt="'+swatchMsg+'"/><br /><img src="images/close_icon.gif" onclick="xstooltip_hide('+"'"+swatchId+"'"+');" style="border:none;padding:2px;" align="right" width="9" height="9" alt="Hide this swatch"/><span class="jssSmallerText">&nbsp;'+swatchMsg+'</span>';
    
    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
    }
    
    img = document.getElementById(parentId); 

    // if tooltip is too wide, shift left to be within parent 
    if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
    if (posX < 0 ) posX = 10; 
        
    x = xstooltip_findPosX(img) + posX;
    y = xstooltip_findPosY(img) + posY;
        
    it.style.top = y + 'px';
    it.style.left = x + 'px';
    it.style.visibility = 'visible'; 
}

function xsimg_show(imgFile, imgMessage, imgdivId, imgdivClass, parentId, posX, posY, imgWidth, imgHeight)
{
    it = document.getElementById(imgdivId);
    if (it.className != imgdivClass) it.className = imgdivClass;

    it.style.width = imgWidth + 'px';

    it.innerHTML = '<img style="border-bottom:1px solid black; margin-left:0px; padding-left:0px; width:'+imgWidth+'px; height:'+imgHeight+'px;" src="'+imgFile+'" alt="Magnifyed image"/><br /><img src="images/close_icon.gif" onclick="xstooltip_hide('+"'"+imgdivId+"'"+');" style="border:none;padding:2px;" align="right" width="9" height="9" alt="Hide this image"/><span class="jssSmallerText">&nbsp;'+imgMessage+'</span>';
    
    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
    }
    
    img = document.getElementById(parentId); 

    // if tooltip is too wide, shift left to be within parent 
    if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
    if (posX < 0 ) posX = 10; 
        
    x = xstooltip_findPosX(img) + posX;
    y = xstooltip_findPosY(img) + posY;
        
    it.style.top = y + 'px';
    it.style.left = x + 'px';
    it.style.visibility = 'visible'; 
}

function xstooltip_hide(id)
{
    it = document.getElementById(id); 
    it.style.visibility = 'hidden'; 
}

function xswatch_swap(swatchFile, swatchMsg, swatchId)
{
    it = document.getElementById(swatchId);

    if (swatchFile != '')
    {
    	it.innerHTML = '<img style="border-bottom:1px solid black; margin-left:0px; padding-left:0px; width:200px; height:120px;" src="shopimages/products/swatches/'+swatchFile+'.jpg" alt="'+swatchMsg+'"/><br /><span class="jssSmallerText">&nbsp;'+swatchMsg+'</span>';
    }
    else
    {
    	it.innerHTML = '<div style="text-align:center;"><span class="jssSmallerText">&nbsp;'+swatchMsg+'</span></div>';
    }
    
    if (it.style.visibility != 'visible') it.style.visibility = 'visible'; 
}

function xswatch_click(formName, colorField, selectValue){
	var colorSelect;
	var colorValue;
	if (!doesFormElementExist(formName, colorField)){
		return true;
	}
	colorValue =  getFormElementValue(formName,colorField);
	if (colorValue != selectValue){
		colorSelect = getFormElement(formName,colorField);
		for (var i=0; i<colorSelect.length; i++){
		 if (colorSelect.options[i].value==selectValue){
		  colorSelect.options[i].selected = true;
		  break;
		 }
		}
	}
	return true;
}

/*
 * validateState
 */ 
function validateState(formName, countryField, stateField){
	var countryValue;
	var stateSelect;
	if (!doesFormElementExist(formName, countryField)){
		return true;
	}
	if (!doesFormElementExist(formName, stateField)){
		return true;
	}
	countryValue =  getFormElementValue(formName,countryField);
	if (countryValue != 223 && countryValue != 39 && countryValue != 13){
		stateSelect = getFormElement(formName,stateField);
		stateSelect.options[stateSelect.length - 1].selected = true;
	}
	return true;
}
