var CAMPAIGN_COOKIE_NAME = "UFSB_CC";
var CAMPAIGN_CODE_PARAMETER_NAME = "cmpgn_cd";
var defaultValue = "UFDEFAULT1";
var campaignCode = "";
var hostName = document.location.hostname;
var queryString = document.location.search.substring(1).toLowerCase();
var expireDaysValue = 90;

function openAccount(n){ 
	// jms 10.29.2009 added mortggage url so that index page learn more link will redriect to mortgages page.
	var myUrl = 'https://secure.andera.com/index.cfm?fiid=0E5C7D7F2E1B4E8B921C3723575EB7D9&promotionalCode=' + campaignCode;
	var myMortgagaeUrl = '/mortgages.html';
	var cfgToken=''; 
	var w=766; 
	var h=screen.height*0.75; 
	var winl=(screen.width-w)/2; 
	var wint=((screen.height-h)/2)*0.75; 
	if(openAccount.arguments.length) {cfgToken="&selectedProducts="+openAccount.arguments[0];}
	
	
	if (n) {
		if (n == 100) {
			return myUrl;
		} else {
			return myMortgagaeUrl;
		}
	}
	else {
		window.open(myUrl,'openAccount','height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,menubar=no,status=yes,toolbar=no,resizable=no'); 
	}
}

/*
//added by Tom Maher FMC 5/23/08
function getParameter( parameterName ) {
	var defaultValue = "UFDEFAULT1";
	
	// The search property is a string beginning with a question mark that specifies any query information in an HTTP URL
	var queryString = window.location.search.substring(1).toLowerCase();
	
	//declare an Array to hold the parameter=value substrings
	var parameters = new Array();
	
	//split creates an array of strings that "split" at the character specified (in this case &)
	parameters = queryString.split('&');
	
	//iterate over the substrings  until the 'parameterName' argument value is found.
	for(var i = 0; i < parameters.length; i++) {
		if (parameters[i].indexOf(parameterName.toLowerCase())>=0) {
			//create an Array to hold the value(s) of the parameter
			var parameterValue = new Array();
			
			//split the string into name/value pairs at the "=" i.e. foo=bar
			parameterValue = parameters[i].split('=');
		
			//return the value that was found
			return parameterValue[1];
		}
	}
	
	//value was not found so return the default
	return defaultValue;
}
*/

function getValueByName(key,stringToSearch){
	var defaultValue = "";
	
	//declare an Array to hold the parameter=value substrings
	var nameValuePair = new Array();
	
	//split creates an array of strings that "split" 
	//at the character specified (in this case &)
	nameValuePair = stringToSearch.split('&');
	
	//iterate over the substrings  until the 'parameterName' argument value is found.
	for(var i = 0; i < nameValuePair.length; i++) {
		if (nameValuePair[i].indexOf(key.toLowerCase())>=0) {
			//create an Array to hold the value(s) of the parameter
			var parameterValue = new Array();
			
			//split the string into name/value pairs at the "=" i.e. foo=bar
			parameterValue = nameValuePair[i].split('=');
		
			//return the value that was found
			return parameterValue[1];
		}
	}
	
	return defaultValue;	
}
	
//http://snipplr.com/view/1666/javascript-validation--isempty/
function isEmpty( inputStr ){ 
	if ( null == inputStr || "" == inputStr ) { 
		return true; 
	} return false; 
}

function isURLDecorated(){
	//check for presence/absence of cmpgn_cd and web_ad_id
	return !(isEmpty(getValueByName(CAMPAIGN_CODE_PARAMETER_NAME,queryString)));
}

function createOrUpdateCookie(cookieName){
	//setup the value
	var value = CAMPAIGN_CODE_PARAMETER_NAME + '=' + escape(campaignCode);

	//overwrite only
	setCookie(cookieName,value,expireDaysValue)
}

function assignTrackingData(){
	//check to see if an unexpired cookie exists
	var cookieData = getCookie(CAMPAIGN_COOKIE_NAME);

	if(!isEmpty(cookieData)){
		campaignCode = getValueByName(CAMPAIGN_CODE_PARAMETER_NAME,cookieData);
	}else{
		//there was no cookie so get value from the querystring
		if(isURLDecorated()){
			//user came from placement
			campaignCode = getValueByName(CAMPAIGN_CODE_PARAMETER_NAME,queryString);
		}else{
			//there was no value in the querystring so default it
			campaignCode = defaultValue;
		}
		
		createOrUpdateCookie(CAMPAIGN_COOKIE_NAME);
	}
	
	return;
}

function setCookie(c_name,value,expiredays){
	//create a new Date object for the expiration date
	var exdate=new Date();
	
	//add the expiredays to today
	exdate.setDate(exdate.getDate()+expiredays);
	
	//write the cookie
	//document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	document.cookie=c_name+ "=" + value + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name){
	//find out if cookies exist for this host
	if (document.cookie.length>0){
		//document.cookie returns a string so find the
		//starting position of the cookie we're interested in
		c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1){ 
				c_start=c_start + c_name.length+1;
				
				//individual cookies are separated by ;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
					return unescape(document.cookie.substring(c_start,c_end));
			} 
	}
	return "";
}    

function deleteCookie(c_name){
	var tmp = getCookie(c_name);
	if(tmp){ 
		setCookie(c_name,tmp,(new Date(1))); 
	}
}














