
/***
 * 
 * getCookie function taken from:
 * 
 * http://www.w3schools.com/js/js_cookies.asp
 * 
 * @param {String} c_name : cookie name
 */

function getCookie(c_name) {
if (document.cookie.length>0)  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1) { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    //alert("getCookie - " + c_name + ":" + document.cookie.substring(c_start,c_end));
    	return unescape(document.cookie.substring(c_start,c_end));
    } //END: cookie name found
  } //END: document has cookies
return "";
} //END: getCookie()

/*
 * if there is promo cookie
 * addJumpParam function changes href from http://www.buyz.com/ or http://www.bidz.com/ to
 * hrefNew=http://bidz.com/bzJapp/Jump.Action?sid=100&promo=   or
 * hrefNew=http://buyz.com/bzJapp/Jump.Action?sid=200&promo=
 * and sets promo parameter for Jump.action
 */
function addJumpParam(atagId, hrefNew) {
    promo = getCookie('promo');
    if (promo != "")
    {
        atag = document.getElementById(atagId);
        atag.href = hrefNew + promo.replace('%0a', '').replace('%0d', '').replace('%0A', '').replace('%0D', '');
    }
}

/*
 * if there is promo cookie
 * updateHrefAddPromo function adds promo parameter to hrer link
 */
function updateHrefAddPromo(atag) {
    promo = getCookie('promo');
    if (promo != "")
    {
    	ampOrQuest = "?";
    	if (atag.href.indexOf('?') > 0) ampOrQuest = "&";
    	atag.href = atag.href + ampOrQuest + 'promo=' + promo.replace('%0a', '').replace('%0d', '').replace('%0A', '').replace('%0D', '');
    }
    return true;
}

/**
 * helper function to check if a var is defined.
 * @param {Object} variable
 */
function isDefined( variable)
{
    return (typeof(variable) == "undefined")?  false: true;
}//EMD: isDefined

// ";expires=Tue, Jul 14 2009 13:10:12 UTC" +
/*
 * Function to set cookie value
 */
function setBidzCookie(cookieName,cookieValue, nDays) {
	var today = new Date();
	var expire = new Date();
	var path="/";
		 
	if (nDays==null || nDays==0) 
	{
		nDays=1;
	}
	
	 expire.setTime(today.getTime() + 3600000*24*nDays);
	 document.cookie = escape(cookieName) + "=" + escape(cookieValue) + 
	 					";expires="+expire.toGMTString() + 
	 					";path=" + path;
}//END: setBidzCookie

function setCookie(name,value,expires){
	//alert("setCookie - inside setCookie");
	document.cookie = name + "=" + value + 
	";path=/" + 
	((expires==null) ? "" : ";expires=" + expires.toGMTString())
}

