/**
 * Set csref cookie to build bridge(s)
 */
function setCSRefBridgeCookie(name, value) {
    var expires = new Date ();
	expires.setTime (expires.getTime() + (1000 * 60 * 30));
    document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() +  "; path=/";    
}   
/**
 * Get csref cookie to build bridge(s)
 */
function getCSRefBridgeCookie(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;		
}
/**
 * Tries to get the csref from the URL
 * This is the first method of detecting the csref
 **/
function getCSRefFromUrl()
{
	var q_str = new String(window.location.search);
	var csref = null;
	if(q_str.indexOf('csref=')>-1)
	{
		csref = q_str.substring(q_str.indexOf('csref='),q_str.length);
		csref = csref.substring('csref='.length);
		if(csref.indexOf('&')>-1)
		{
			csref = csref.substring(0,csref.indexOf('&'));	
		}
		
		return csref;
	}	
}
/**
 * Update the href elements in the links to include the csref parameter
 */
function setCSRefInLinks(csref)
{
	setCSRefInHref(document.getElementsByTagName("a"),csref);
    setCSRefInHref(document.getElementsByTagName("area"),csref);
}
/**
 * Update the href elements in the supplied array
 */
function setCSRefInHref(links, csref)
{
	
	for(var i = 0;i<links.length;i++)
	{
		var link = links[i];
		var org = link.innerHTML;
		if(link.href.indexOf('cphzt')==-1 && link.href.indexOf('dev.rezidorsas')==-1 && link.href.indexOf('csref=')==-1 && link.href.indexOf('javascript:')==-1)
		{
			var href=link.href;
			if(href.substr(href.length-'/'.length)=='/')
			{
				href=href.substring(0,href.length-1);
			}
			if(href.indexOf('?')==-1)
			{
				href=href+'?csref='+csref;	
			}else
			{
				href=href+'&csref='+csref;
			}
			link.href=href;
            if(org) link.innerHTML = org;
		}
	}
}
/**
 * Adds csref as a hidden field in all of the forms on the page
 */
function setCSRefInForms(csref)
{
	var forms = document.forms;
	for(var i = 0;i<forms.length;i++)
	{
		setCSRefInForm(forms[i],csref);
	}
}
/**
 * Adds the csref parameter to a form
 */
function setCSRefInForm(currentForm, csref)
{
	
	var csRefElement = getCSRefElement(currentForm);
	if(csRefElement==null)
	{
		//lets inject a csref element in the form
		var doc = currentForm.ownerDocument;
		csRefElement = doc.createElement('INPUT');
		csRefElement.type='hidden';
		csRefElement.name='csref';
		csRefElement.value=csref;
		currentForm.appendChild(csRefElement);
	}
}
/**
 * Adds the csref parameter to a lcation.href
 */
function setCSRefLocationHref(LocationHref)
{
	var link = LocationHref.toLowerCase();
	var csref = getCSRefFromUrl();
	if(csref==null) {csref = getCSRefBridgeCookie("bridge");}
	if(csref!=null && link.indexOf('http')!=-1 && link.indexOf('csref=')==-1) {
		if(LocationHref.indexOf('?')==-1) 
			{location.href=LocationHref+'?csref='+csref;}
		else
			{location.href=LocationHref+'&csref='+csref;}
	} else {location.href=LocationHref;}
}
/**
 * tries to get the csref parameter from the forms.
 */
function getCSRefElement(currentForm)
{
	var formElements = currentForm.elements;
	for(var i = 0;i<formElements.length;i++)
	{
		var element = formElements[i];
		if(element.name=='csref')
		{
			return element;
		}
	}
}
/**
 * adds the csref parameter to all links and forms on the page
 */
function setCSRef() {
	var csref = getCSRefFromUrl();
	if(csref==null) {
		//we did not find it in a form. lets try teh cookie
		csref = getCSRefBridgeCookie("bridge");
		if(csref==null) {
			//we did not find it anywhere lets set it as the refferer
			if (document.referrer) {
				csref = document.referrer;
				setCSRefBridgeCookie("bridge",csref);
			}
    	}
	} else {setCSRefBridgeCookie("bridge",csref);}
	if(csref!=null) {		
    	setCSRefInForms(csref);	
    	setCSRefInLinks(csref);
	}
}