var httpIs = location.protocol;
var strImgHost = (httpIs == "https:") ? "https://a248.e.akamai.net/f/248/1791/1d/origin.image.coldwatercreek.com" : "http://image.coldwatercreek.com";

function imageChange(imageID,imageName) 
{
	{
		document.images[imageID].src = eval(imageName + ".src");
	}
}

function wireupOnload(init)
{
    if (window.addEventListener)
    {
        window.addEventListener("load", init, false);
    }
    else if (window.attachEvent)
    {
        window.attachEvent("onload", init);
    }
}

function getQueryString(param){
	var begin,end,urlParamStr;
	urlParamStr = self.location.search;
	
	param = param.toLowerCase();
	urlParamStrLower = urlParamStr.toLowerCase();
	
	if(self.location.search.length > 1){
		var paramEq = (urlParamStrLower.indexOf("?" + param + "=") > -1) ? "?" + param + "=" : "&" + param + "=";
		if (urlParamStrLower.indexOf(paramEq) > -1) {
			begin = urlParamStrLower.indexOf(paramEq) + paramEq.length;
			end = urlParamStrLower.indexOf("&",begin);
			if(end==(-1)) {end = urlParamStrLower.length;}
			return(urlParamStr.substring(begin,end));
		}
		else {return("");}
	}

	else if(self.location.hash.length > 1){
		if ((self.location.hash.indexOf("?" + param + "=") > -1) || (self.location.hash.indexOf("&" + param + "=") > -1)) {
			begin=self.location.hash.indexOf(param + "=") + param.length + 1;
			end=self.location.hash.indexOf("&",begin);
			if(end==(-1)) end=self.location.hash.length;
			return(self.location.hash.substring(begin,end));
		}
		else return("");
	}
	else return("");
}

function executeChat(nVer) {
    document.cookie = 'CHAT=' + escape(document.location.href) + ';path=/;';
    document.location.href = 'http://www.coldwatercreek.com/asp/chat.asp?load=&ver=' + nVer;
}

function BTT() {
    document.write ("<div align='right'><a href='#top' class='btt'>back to top</a></div>")
}

function myvoid(){ } 

function showDetailView(url,cardid)
{
	var hreflocation;
	hreflocation = "/GiftCard/GiftCardPopup.aspx?ImageFileName=" + url + "&cid=" + cardid;
	openPicWin(hreflocation,600,350,"directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no")
}

function textCounter(obj, maxlimit) {
	var field = obj;
	var maxrow = obj.rows;
	var maxcol = obj.cols;
	var i;
	var j = 0;
	var arr = field.value.split('\n');

	if (field.value.length > maxlimit || arr.length > maxrow ||(arr.length == maxrow && arr[maxrow-1].length > maxcol)) {
		if (field.value.length > maxlimit){
			field.value = field.value.substring(0, maxlimit);
		}
		else
		{
			for (i=0; i<maxrow; i++) {
				j += arr[i].length;
			}
			field.value = field.value.substring(0, j+2);
		}
	}
}

var popup = null;

function openPicWin(page,IWidth,IHeight,flags) 
{ 
 	if (popup)
	{
		if (!popup.closed)
		{
			popup.close();
		}
	}
	popup = null; 

	var ITop = (screen.height/2)-(IHeight/2);
 	var ILeft = (screen.width/2)-(IWidth/2);
 	
	if (flags != null)
	{
		if (flags.substring(0,1) != ",")
		{
			flags = "," + flags;
		}
	}
	
	popup = window.open(page, "ColdwaterCreek", "TOP=" + ITop + ",LEFT=" + ILeft + ",HEIGHT=" + IHeight + ",WIDTH=" + IWidth + flags);
	popup.focus();
 }
function btnOn(obj) {
	obj.className = obj.className.replace(/_off/i,"_on");
}

function btnOff(obj) {
	obj.className = obj.className.replace(/_on/i,"_off");
}
function getQueryStringParam(pStr)
{
	var queryString = location.search;
	var startPos = 0;
	var stopPos = 0;
	var tempPos = 0;
	var pVal = "";
	
	if (pStr) 
	{				
		if(queryString.indexOf(pStr) != -1)
		{
			startPos = queryString.indexOf(pStr) + pStr.length + 1;
			stopPos = ((tempPos = (queryString.indexOf("&", startPos))) == -1) ? queryString.length : tempPos;
			pVal = queryString.substring(startPos, stopPos);
			pVal = unescape(pVal);
		}
	}
	return pVal;	
}
function disableButton(objName)
{
	document.getElementById(objName + "_Disabled").style.display = "block";
	document.getElementById(objName + "_Enabled").style.display = "none";
}
		var navloaded = false;

		AddEvent(window, 'load', LoadNav);

		function $(obj)
		{
			return document.getElementById(obj);
		}
		function AddEvent(obj, evt, func)
		{
			if(obj.addEventListener)
			{
				obj.addEventListener(evt, func, false);
				return true;
			}
			else if (obj.attachEvent)
			{
				return obj.attachEvent("on"+evt, func);
			}
			else
			{
				return false;
			}
		}
		
		function LoadNav(evt)
		{
			if(!navloaded)
			{
				ele = $(document.navnode);
				
				if(ele)
				{
					SelectNode(ele);
				}
				
				navloaded = true;
			}
		}
		
		function SelectNode(ele)
		{						
			if(ele.parentNode.className == 'lvl2')
			{
				ele.style.color = '#C44506';	
				for(var i=0;i<ele.parentNode.childNodes.length;i++)
				{ 
					if(ele.parentNode.childNodes[i].nodeName == 'DIV')
					{
						ele.parentNode.childNodes[i].style.display = 'block';					
					}
				}
			}
			
			ele.parentNode.className += '_on';		
			for(var i=0;i<ele.parentNode.parentNode.childNodes.length;i++)
			{ 
				if(ele.parentNode.parentNode.childNodes[i].nodeName == 'DIV')
				{
					ele.parentNode.parentNode.childNodes[i].style.display = 'block';					
				}
			}
			
			if(ele.parentNode.parentNode.className.substr(0,3)=='lvl')
			{
			   SelectNode(ele.parentNode);
			}			
		}

var imgObject;
var strNewImgPath = "";
var finalAltImg = strImgHost + "/Spcr.gif";
function testImage(_imgObj) {
	imgObject = _imgObj;
	var imgPath = imgObject.src.toLowerCase();
	strNewImgPath = newImagePath(imgPath);
	
	var tester = new Image();
    tester.onload = isGoodImgPath;
    tester.onerror = isBadImgPath;
    tester.src = strNewImgPath;
}
function isGoodImgPath()
{
	imgObject.src = strNewImgPath;
}
function isBadImgPath()
{
	var strSpcImg = finalAltImg;
	if (strNewImgPath != strSpcImg)
	{
		imgObject.src = strSpcImg;
	}
}
function newImagePath(_imgPath)
{
	var strNewImgPath = "";
	if (_imgPath.match("36x45") != null)
	{strNewImgPath = strImgHost + "/Products/36x45/missing_36x45.gif"}
	else if (_imgPath.match("104x130") != null)
	{strNewImgPath = strImgHost + "/Products/104x130/missing_104x130.gif"}
	else if (_imgPath.match("144x180") != null)
	{strNewImgPath = strImgHost + "/Products/144x180/missing_144x180.gif"}
	else if (_imgPath.match("220x275") != null)
	{strNewImgPath = strImgHost + "/Products/220x275/missing_220x275.gif"}
	else if (_imgPath.match("296x370") != null)
	{strNewImgPath = strImgHost + "/Products/296x370/missing_296x370.gif"}
	else if (_imgPath.match("524x655") != null)
	{strNewImgPath = strImgHost + "/Products/524x655/missing_524x655.gif"}
	else
	{strNewImgPath = finalAltImg}
	return strNewImgPath;
}
function setSecureImage(imgObj,imgPath)
{
	imgObj.src = strImgHost + imgPath;
}
function renderSecureImage(iPath,iAttributes)
{
	var strImgTag = "";
	strImgTag = "<img src=\"" + strImgHost + iPath + "\" " + iAttributes + " />";
	
	document.write(strImgTag);
}

function assignUrl(url, isSecure)
{
    if(document.location.hostname != "localhost")
    {
        if(isSecure)
        {
            document.location.href = "https://" + document.location.host + url;
        }
        else
        {
            document.location.href = "http://" + document.location.host + url;
        } 
    }
    else
    {
        document.location.href = url;
    }
}

function setHttpSecure(isSecure, aLink)
{
	if (document.location.hostname != "localhost")
	{
		if (isSecure)
		{
			aLink.href = aLink.href.replace(/http:/i,"https:");
		}
		else
		{
			aLink.href = aLink.href.replace(/https:/i,"http:");
		}
	}
}

var Url = {
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},

	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}
