var showGiftProducts = function(){
	var div = $('giftoptions').setStyles({
		display:'block',
		opacity: 0
	});
	new Fx.Style(div, 'opacity', {duration: 800} ).start(1);
};

function checkoptions(someform) {
	if (typeof(someform.optionname)=='object') {
		if (someform.myoption.selectedIndex==0) {
			alert('Please select a ' + someform.optionname.value);
			return false;
		} else {
			return true;
		}		
	} else {
		return true;
	}
}

function openLiveZoom (productcode,productimageid)
{
	liveZoomWin=window.open('/zoom3.asp?pid='+productcode+'&piid='+productimageid,'liveZoomWindow','width=460,height=600,location=no,toolbars=no,scrollbars=no,resize=no,status=yes');
	liveZoomWin.focus();
}

function ecsearchgroup(thegroupid) {
	if (document.getElementById(thegroupid).style.display=='none') {
		document.getElementById(thegroupid).style.display='';
		document.getElementById(thegroupid+'_img').src='/images/yellowarrowleftnav-down.gif';	
	} else {
		document.getElementById(thegroupid).style.display='none';
		document.getElementById(thegroupid+'_img').src='/images/yellowarrowleftnav-in.gif';
	}
	var cookiename=thegroupid
	var cookievalue=(document.getElementById(thegroupid).style.display=='');
	document.cookie=cookiename+"="+cookievalue;

}

function toggleid(theid) {
	if (document.getElementById(theid).style.display=='none') {
		document.getElementById(theid).style.display='';
	} else {
		document.getElementById(theid).style.display='none';
	}
}

function popup(url, width, height)
{
	newwindow=window.open(url,'name','height='+height+',width='+width+',scrollbars=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
}

function v12infopopup(url, width, height, right)
{
	newwindow=window.open(url,'name','height='+height+',width='+width+',right='+right+',scrollbars=yes,resizable=no');
	if (window.focus) {newwindow.focus()}
}

// switch styles

function setActiveStyleSheet(title) {
  var i, a, main;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel") &&
        a.getAttribute("rel").indexOf("style") != -1 &&
        a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel") &&
        a.getAttribute("rel").indexOf("style") != -1 &&
        a.getAttribute("title") &&
        !a.disabled
        ) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel") &&
        a.getAttribute("rel").indexOf("style") != -1 &&
        a.getAttribute("rel").indexOf("alt") == -1 &&
        a.getAttribute("title")
        ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}


var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser is incompatible with this site!!");
	}
}

var searchReq = getXmlHttpRequestObject();
var compReq = getXmlHttpRequestObject();

function searchSuggest() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('txtSearch').value);
		searchReq.open("GET", '/searchSuggest.asp?search=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}

//Click function
function setSearch(value) {
	document.getElementById('txtSearch').value = value;
	document.getElementById('search_suggest').innerHTML = '';
}

function logcompare(tproductid,tval) {
	if (compReq.readyState == 4 || compReq.readyState == 0) {
		var str = escape(tproductid);
		var str1 = escape(tval);
		compReq.open("GET", '/elements/compare_log.asp?pid=' + str + '&val=' + str1, true);
		compReq.onreadystatechange = null; 
		compReq.send(null);
	}		
}

function limitText(limitField, limitNum) {
	//next two lines are for alerting user than input has been truncated
	if (limitField.value.length > limitNum + 1)
	alert('Only ' + limitNum + ' characters allowed');
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}
