// JavaScript Document
var Utf8 = {

    // public method for url encoding
    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;
    },

    // public method for url decoding
    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;
    }

}		

var searchString = window.location.search.substring(1); 
var nameValuePairs = searchString.split(/&/);
var nameValuePair;
var name;
var value;
var myDomain = "/";					
myPath = "/";
// set expiry date to 90 days from now.
largeExpDate = new Date ();
largeExpDate.setTime(largeExpDate.getTime() + (90 * 24 * 3600 * 1000));
// delete date
deleteDate = new Date();
deleteDate.setTime(deleteDate.getTime() - (90*24*3600*1000));

//////////////////specific affiliate functions////////////////////////////////////////////////

//SetAFCookie(name,value,largeExpDate,myDomain);
function SetAFCookie (name, value) {
		//first lets make sure that the product isn't a duplicate - and if it is strip off the duplicate tag.
		var myregexp = /\d*!/g;
		name = name.replace(myregexp, "");

        var argv = SetAFCookie.arguments;
        var argc = SetAFCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;       
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape (value) +
                ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
                ((path == null) ? "" : ("; path=" + path)) +
                ((domain == null) ? "" : ("; domain=" + domain)) +
                ((secure == true) ? "; secure" : "");
}

function GetAFCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

function GetAFCookie (name) {  
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                        return GetAFCookieVal (j);
                i = document.cookie.indexOf(" ", i) + 1;
                        if (i == 0)
                                break;
                }		
   return null;
}

function showAffiliateTextAreas(searchValuePairs)
{		
		for (var i = 0; i < searchValuePairs.length; i++) {
		searchValuePairs = searchValuePairs[i].split(/=/);

		name = searchValuePairs[0];    
		value = searchValuePairs[1];
	  
		name = name.toLowerCase();
		
		if (name == "showafil"){
		  if (typeof(value)!="undefined"){
			   value = value.toLowerCase();

				if (value=="true"){
	 			 return true;
				}
		  }       
		}
	}
	return true;
}
function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}
function hidePhone()
{ 		var hidePhone = false;
		if (GetAFCookie ("adnetwork") == "af" || GetAFCookie ("adnetwork") == "wg"){
				hidePhone = true;
				if(document.getElementById("phoneIfyouNeedTo")!=null)
				{
				var phone = document.getElementById("phoneIfyouNeedTo").style.visibility="hidden";
				}				
			} 
			else {
				if(document.getElementById("phoneIfyouNeedTo")!=null)
				{
				var phone = document.getElementById("phoneIfyouNeedTo").style.visibility="visible";
				}
			}
		var allDivsToBeHidden = getElementsByClassName(document,"div","cantBuyCheaper");
		
		
			//now loop through and set to hidden
			var arLen=allDivsToBeHidden.length;
			for ( var i=0, len=arLen; i<len; ++i ){
				//now that I've got them lets turn them invisible
				if(hidePhone==true){
					allDivsToBeHidden[i].style.visibility = 'hidden';
				}else{
					allDivsToBeHidden[i].style.visibility = 'visible';
				}
			}		

}
// ---
// Array support for the push method in IE 5
if(typeof Array.prototype.push != "function"){
	Array.prototype.push = ArrayPush;
	function ArrayPush(value){
		this[this.length] = value;
	}
}
// ---
/////////////////////////////////////////////////////////////////////////////////////

	for (var i = 0; i < nameValuePairs.length; i++) {
		nameValuePair = nameValuePairs[i].split(/=/);

		name = nameValuePair[0];    
		value = nameValuePair[1];
	  
		name = name.toLowerCase();

		if (name == "adnetwork"){
		  if (typeof(value)!="undefined"){
			   value = value.toLowerCase();

				if (value =="af"||value == "wg"){
	  //*********set cookie******************//
	 SetAFCookie(name,value,largeExpDate,myDomain);
	  //*********set cookie******************//  
				}
		  }       
		}
	}
	
//////////////////////////////////////////////////////////////////////////////////////
	
