/*----------------------------------------------------------------
Standard Javascript Cookie Code
- File name :	cookie.js
- Created by :	Sheree Leibinger
- Created on :	30 April 2003 

COMMENTS :
	This file provides all the standard functions used to read, create and delete a cookie using javascript.
	It also provides the function to correctly link to the Incomeaccess Affiliate program 
	and attach the appropriate bannertag used to track referals with the system.
	
	Editing these functions could severly effect the correct tracking of affiliates and their referals.
	The only changes required by merchants to the file should be to the expDays and IncomeaccessUrl variables

INSTRUCTIONS :
* To change the length time a cookie is valid for
	edit-: var expDays = 30;
	change 30 to how many days the cookie is required to be valid for
	
* To change the url that is returned when linking the the Affiliate program
	edit-: var IncomeaccessUrl = "http://www.incomeaccess.com/registration.asp"
	change the http... to the required url
	
* To Set a cookie call -:					SetCookie ('btag',btag);
	where 'btag' is the name of the cookie
	and btag is the contents of the cookie
	
* To call the contents of a cookie call-:	GetCookie('btag')
	where 'btag' is the name of the cookie
	
* To return a Text link to the Affiliate program with btag in the url
	include "<SCRIPT> returncookielink("Sign Up Now","menuitems")</SCRIPT>" in the pages html
	 replace "Sign Up Now" with the name of the text link 
	 replace "menuitems" with the appropriate style class
	 
* To return a banner linked to the Affiliate program with btag in the url
	include "<SCRIPT> returncookielink("Sign Up Now","menuitems")</SCRIPT>" in the pages html
	 replace "Sign Up Now" with the image tag for the banner to be used
	 replace "menuitems" with the appropriate style class
----------------------------------------------------------------*/

var expDays = 30;
var IncomeaccessUrl = "http://www.incomeaccess.com/registration.asp"
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getArgs() {
	var args = new Array()
	// Get Query String
	var query = location.search.substring(1); 
	// Split query at the comma
	var pairs = query.split("&"); 
	// Begin loop through the querystring
	for(var i = 0; i < pairs.length; i++) {
		// Look for "name=value"
		var pos = pairs[i].indexOf('='); 
		// if not found, skip to next
		if (pos == -1) continue; 
		// Extract the name
		var argname = pairs[i].substring(0,pos); 
		// Extract the value
		var value = pairs[i].substring(pos+1); 
		// Store as a property
		args[i] = new Array(argname,unescape(value));
	}
	return args; // Return the Object
}

// Get the values back
var args = getArgs(); //Get the arguments
var btag =""

for(var i=0; i<args.length; i++){
	switch (args[i][0]){// loop through all the variables passed
	case "btag": 
			btag = args[i][1]
			break;
		}
}

if (btag){
		SetCookie ('btag',btag);
	}

function getCookieVal (offset) {  
var endstr = document.cookie.indexOf (";", offset);  
if (endstr == -1)    
endstr = document.cookie.length;  
return unescape(document.cookie.substring(offset, endstr));

}

function GetCookie (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 getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return "";
}

function SetCookie (name, value) {  
var existingcookie = ""
existingcookie=  GetCookie(name)

// if cookie does not exist
if (existingcookie.length == 0) {
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = exp; //(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=" + exp.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
	
	}
}

function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function returncookielink (linkname,stylename) {
	cookie = GetCookie('btag')
	
	if (cookie.length == 0) {
		document.write ("<a href=\"" + IncomeaccessUrl + "\" target=\"_blank\" class=\"" + stylename + "\">" + linkname + "</a>");
	}
	else {
		document.write ("<a href=\"" + IncomeaccessUrl + "?btag=" + cookie + "\" target=\"_blank\" class=\"" + stylename + "\">" + linkname + "</a>");
		
	}
}