// PerfectForms Referrer Management

function getCookie(NameOfCookie){
    if (document.cookie.length > 0) {              
    begin = document.cookie.indexOf(NameOfCookie+"=");       
    if (begin != -1) {           
      begin += NameOfCookie.length+1;       
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin, end));
    } 
  }
  return null;
}

//http://techpatterns.com/downloads/javascript_cookies.php
function setCookie(NameOfCookie, value, expiredays) {
    var today = new Date ();
    today.setTime(today.getTime());
    var path = "/";
    var domain = "www.quask.com";
    var secure = null;

    var expires = expiredays * 1000 * 60 * 60 * 24;
    //alert(expires);
    var expiresDate = new Date(today.getTime() + (expires));
    //alert("expires Date: " + expiresDate);

	//document.cookie = NameOfCookie + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expiresDate.toGMTString());

	document.cookie = NameOfCookie + "=" + escape(value) + ((expires) ? ";expires=" + expiresDate.toGMTString() : "")+((path) ? ";path=" + path : "")+((domain) ? ";domain=" + domain : "")+((secure) ? ";secure" : "");
}

function getQueryParam(query, param) {
	if ( query != null && param != null ) {
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == param) {
				return pair[1];
			}
		}	
	}
	
	return '';
} 

function getDecodeQueryParam(query, param) {
	if ( query != null && param != null ) {
		var indexOfq = query.indexOf("q%3D"); // This checks for URL Escape, which translates to q=.
		if (indexOfq == -1) {
			return URLDecode(getQueryParam(query, param));
		}
		else {
			var querySubStr = query.substr(indexOfq);
			return URLDecode(querySubStr.substr(0, querySubStr.indexOf("&")));
		}
	}
	
	return '';
} 

function setPFCookies() {
	//alert("referrer=" + document.referrer);
    var refs = document.referrer.split("?");
//    var otherObjInnerHTML = document.getElementById("hidden-for-test-other").innerHTML;
//    otherObjInnerHTML = otherObjInnerHTML.concat("@@@@@" + refs);
//    document.getElementById("hidden-for-test-other").innerHTML = otherObjInnerHTML;
	if ( getCookie('PFReferrer') == null )
	{
		setCookie('PFReferrer', refs[0], 1);
	}

	if ( getCookie('PFKeywordsQ') == null )
	{
		var keywordsQ = getQueryParam(refs[1], 'searchq');
		
		if ( keywordsQ == '' ) {
			keywordsQ = getDecodeQueryParam(refs[1], 'q');
		}
		
		setCookie('PFKeywordsQ', keywordsQ, 1);
	}
	
	if ( getCookie('PFKeywordsP') == null )
	{
		var keywordsP = getQueryParam(refs[1], 'p') + '; ' + getQueryParam(refs[1], 'searchp');
		
		setCookie('PFKeywordsP', keywordsP, 1);
	}

	if ( getCookie('PFLandingPage') == null )
	{
		setCookie('PFLandingPage', location.href, 1);
	}
	
	var pfUrlHistory = getCookie('PFurlHistory');
	pfUrlHistory = (pfUrlHistory != null && pfUrlHistory != "null" && pfUrlHistory != "")?(pfUrlHistory + "; "):"";
	setCookie('PFurlHistory', pfUrlHistory + location.href, 1);
}

function URLDecode(psEncodeString) {
	// Create a regular expression to search all +s in the string
	var lsRegExp = /\+/g;
	// Return the decoded string
	return unescape(String(psEncodeString).replace(lsRegExp, " "));
}

function getLP() {
	return getCookie('PFLandingPage');
}
			
function getReferrerURL() {
	return getCookie('PFReferrer');
}


function getGoogleBingSearchQ() {
	return getCookie('PFKeywordsQ');
}
	
function getYahooSearchQ() {
	return getCookie('PFKeywordsP');
}

function getUrlHistory() {
	return getCookie('PFurlHistory') + "; " + navigator.userAgent;
}

function getDomainName() {
    return "quask.com";
}
