// Javascript versions of CF functions

// addEvent and removeEvent
// IE5+ |  NS6 | Firefox | Konquerer (Safari)
// Chris Yager

function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 

function removeEvent(elm, evType, fn, useCapture)
{
  if (elm.removeEventListener){
    elm.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.detachEvent){
    var r = elm.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}


// formating functions
// IE3+ |  NS4,6 | Firefox 1.5 (earlier displays negative #'s) | Konquerer (Safari)
// original from javasource.com 1999, cross browser compatibility Chris Yager 

function dollarFormat(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {num = "0";}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) {cents = "0" + cents};
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') +  num + '.' + cents);
}

// Cookie handling functions
// Full browser compliance for cookie supported browsers
// Chris Yager
function saveCookie(name,value,days)
{
  if (days) {
    var d = new Date();
    d.setTime(d.getTime()+(days*24*60*60*1000));
    var ex = "; expires="+d.toGMTString();
  }
  else {
    var ex = "";
  }
  document.cookie = name+"="+value+ex+"; path=/";
}

function readCookie(name)
{
  var eq = 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(eq) == 0) return c.substring(eq.length,c.length);
  }
  return null;
}

function eraseCookie(name)
{
  saveCookie(name,"",-1);
}

// getParams
// Full browser compliance
// Chris Yager
function getQueryArgs()
{
  var args = {};
  var loc = window.location.href;
  var q = loc.indexOf("?");
  if (q==-1) return false;
  loc = loc.substring(q+1);
  var pairs = loc.split("&");
  for (var i=0; i<pairs.length;i++){
    var keyval = pairs[i].split("=");
    args[keyval[0]] = unescape(keyval[1]);
  }
  return args;
}

// Track site functions
// Steve Anderson

function sendToFriend() {
	window.open('', 'sendtofriend', 'TOP=50,LEFT=50,WIDTH=400,HEIGHT=480,RESIZABLE=no,SCROLLBARS=no,STATUS=0').focus();
	document.formSendToFriend.title.value = document.title;
	document.formSendToFriend.target = "sendtofriend";
	document.formSendToFriend.submit();
	return false;
}

var promoCodeURLVar = 'promotioncode'; // URL Variable we are looking for
var promoCodeURLVar2 = 'promotionCode';
var promoCodeURLVar3 = 'PromotionCode';

function setPromtionsCodeToCookie(){
	queryStringArray = getQueryArgs();
	if (queryStringArray[promoCodeURLVar] != undefined){ 
		saveCookie('PromotionCode',queryStringArray[promoCodeURLVar],1); // name,value,days
		document.getElementById('displayCookie').innerHTML = readCookie('PromotionCode');
	}
	if (queryStringArray[promoCodeURLVar2] != undefined){ 
		saveCookie('PromotionCode',queryStringArray[promoCodeURLVar2],1); // name,value,days
		document.getElementById('displayCookie').innerHTML = readCookie('PromotionCode');
	}
	if (queryStringArray[promoCodeURLVar3] != undefined){ 
		saveCookie('PromotionCode',queryStringArray[promoCodeURLVar3],1); // name,value,days
		document.getElementById('displayCookie').innerHTML = readCookie('PromotionCode');
	}
}

//Opt-In Functions by Chris Longson

function clearOptIn()
{
	if(document.getElementById('email').value.toLowerCase() == 'enter email address here')
	{
		document.getElementById('email').value = '';
		document.getElementById('email').style.color = 'Black';
	}
}
	
function populateOptIn()
{
	if(document.getElementById('email').value.toLowerCase() == '')
	{
		document.getElementById('email').value = 'Enter Email Address Here';
		document.getElementById('email').style.color = 'Gray';
	}
}

//Email Sign Up Validation Function added by Chris Longson

function checkEmailSignUp(str) {
	var checks = str.split(',');
	var process = false;
	for(i=0; i<checks.length; i++){
		if(eval('document.aprimo.' + checks[i] + '.checked')){
			process = true;
		}
	}
	var fname = document.aprimo.fname.value;
	var lname = document.aprimo.lname.value;
	var email = document.aprimo.email.value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		alert('Please provide a valid email address');
		document.aprimo.email.focus();
		return false;
	}
	else if(lname == '' || lname == ' '){
		alert('Please enter a valid last name');
		document.aprimo.lname.focus();
		return false;
	}
	else if(!process){
		alert('You must choose atleast one newsletter to sign up');
		return false;
	}
	else{
		return true;
	}
}

function promoGeneric(url, defaultCode)
	{
		var promo = "";
		var promoFind = 'PromotionCode';

		promo = readCookie('PromotionCode'); //check and see if we already have a cookie with the promotion code
		if (promo == null) promo = "";
		
		if (promo == "") //no cookie so go and check the URL
		{
			queryStringArray = getQueryArgs();
			if (queryStringArray[promoFind] != undefined){ 			
				promo = queryStringArray[promoCodeURLVar];
			}
		}

		//check and see if they have a passed in promo or a cookie
		if(promo == "" && defaultCode != "")
		{
			promo = defaultCode; //if not then set it to the default code
		}

		if (promo != "") {
			//update the URL
			
			var q = url.indexOf("?");
			if (q==-1){
				url = url + "?PromotionCode=" + promo;
			}
			else{
				url = url + "&PromotionCode=" + promo;
			}
		}

		promoWindow = window.open(url,"promoGeneric","toolbar=1,width=795,height=575,menubar=1,scrollbars=1");
	}
	

function redirectToLandingPage(url)
{
	var landing = "";
	
	//Check for Cookie
	landing = readCookie('LandingPage');
	
	if(landing == null)
	{
		landing = "";
	}
	
	if(landing == "")
	{
		window.location = url;
	}	
	
}