var COOKIE_PATH = '/';
var DAYS_TO_EXPIRE = 365;
if(window.COOKIE_DOMAIN === undefined){ var COOKIE_DOMAIN = 'aaa.com'; }
if(window.ZIP_COOKIE_NAME === undefined){ var ZIP_COOKIE_NAME = 'zipcode'; }
if(window.ZIP_ERROR_MSG === undefined){ var ZIP_ERROR_MSG = ''; }
function displayZipCodeBox(url){
url = url+'?width=420&height=225&modal=true';
tb_show('Welcome to AAA.com',url, null); 
}

// gets the zipcode cookie data from the club's cookie
function getCookie(CookieName) {
	var search = CookieName + "=";
	if(document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if(offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(";", offset);

			if(end == -1) {
				end = document.cookie.length;
			}
			return document.cookie.substring(offset, end);
		}
	}
}

function setZipCookie(zip) {
        var cookie_value = zip + '|' + ASSOCIATION + '|' + CLUB;
        setCookie(ZIP_COOKIE_NAME, cookie_value, DAYS_TO_EXPIRE, 
                  COOKIE_PATH, COOKIE_DOMAIN, '');
}

function setCookie(name, value, expires, path, domain, secure)
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + value +
        ((expires) ? ";expires=" + expires_date.toGMTString() : "" ) +
        ((path) ? ";path=" + path : "" ) +
        ((domain) ? ";domain=" + domain : "" ) +
        ((secure) ? ";secure" : "" );
}

function checkForZipCodeCookie() {
	var pipe = "";
	var cookieData = getCookie("zipcode");
	var startIndex = 0;
	// found a ZipCode cookie
	// for it to be valid it must be zipcode(>4 digits)|Association(3 char)|Club(>0 digits)
	if(cookieData > "") {
		// get the first marker		
		pipe = cookieData.indexOf("|");
		// get the zipcode, which is a minimum of 5 digits
		if(pipe > 4) {
			foundZipCode = cookieData.substring(startIndex, pipe);
            		return isValidZip(foundZipCode);
		}
	}
	return false;
}

function validInterrogation(state){
if(google.loader.ClientLocation && google.loader.ClientLocation.address.region){
if(google.loader.ClientLocation.address.region == state){
		return true; }}
return false;
}

function checkZip(zipCodeField,callBack){

	if (!document.getElementById(zipCodeField)) return;
	var thisField = document.getElementById(zipCodeField);

	var strZipCode = trim(thisField.value);
	if (strZipCode=="") {
		inlineMsg(zipCodeField,'Please enter a valid 5 digit Zip Code',10);
		thisField.focus();
		return false;
	} else if(isNaN(strZipCode)) {
		inlineMsg(zipCodeField,'Please use only numbers for the ZIP code',3);
		thisField.focus();
		return false;
	} else if(strZipCode.length > 5 ) {
		inlineMsg(zipCodeField,'Length of ZIP code should not be greater than 5 digits',3);
		thisField.focus();
		return false;
	} else if(strZipCode.length < 5 ) {
		inlineMsg(zipCodeField,'Length of ZIP code should not be less than 5 digits',3);
		thisField.focus();
		return false;
	} else {
		var isValid = isValidZip(strZipCode);
		var strState ="";
		if (isValid) {
			var str = strZipCode.substring(0,3);
			var i = parseInt(str);
			if (callBack) {
				callBack();
			}
			setZipCookie(strZipCode)
			return true;
		} else { 
		if(ZIP_ERROR_MSG == ''){ ZIP_ERROR_MSG = 'We\'re sorry, it appears you\'ve entered a Zip Code which is outside our territory. Please try again, or let us help you <a href="http://www.aaa.com/?ZipCodeEdit=' + strZipCode + '">find your local AAA club</a>.'; }
			inlineMsg(zipCodeField,ZIP_ERROR_MSG,10);
			thisField.focus();
			return false;
		}
	}
}

function isValidZip(strZipCode){
	var isValid = false;
	for (i = 0; i < ZipCodeArray.length; i++) {
		if (ZipCodeArray[i] == strZipCode){
			isValid = true;
			break;
		} else {
			isValid = false;
		}
	}
	return isValid;
}

function trim(value) {
	var temp = value;
	var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
	var obj = / +/g;
	temp = temp.replace(obj, " ");
	if (temp == " ") { temp = ""; }
	return temp;
}

function enter(e) {
	var key;
	if(window.event) {
		key = window.event.keyCode;     //IE
	} else {
		key = e.which;     //firefox
	}

	if(key == 13) {
		checkZip();
	} else {
		return false;
	}
}

// START OF MESSAGE SCRIPT //
// These numbers may be modified to change the length of display, speed of display, //
// the position (distance) from the input box, and the fade rate //
var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
	var msg;
	var msgcontent;
	if(!document.getElementById('msg')) {
		msg = document.createElement('div');
		msg.id = 'msg';
		msgcontent = document.createElement('div');
		msgcontent.id = 'msgcontent';
		document.body.appendChild(msg);
		msg.appendChild(msgcontent);
		msg.style.filter = 'alpha(opacity=0)';
		msg.style.opacity = 0;
		msg.alpha = 0;
	} else {
		msg = document.getElementById('msg');
		msgcontent = document.getElementById('msgcontent');
	}
	msgcontent.innerHTML = string;
	msg.style.display = 'block';
	var msgheight = msg.offsetHeight;
	var targetdiv = document.getElementById(target);
	targetdiv.select();
	var targetheight = targetdiv.offsetHeight;
	var targetwidth = targetdiv.offsetWidth;
	var topposition = topPosition(targetdiv) - ((targetheight + 20) / 2);
	var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
	msg.style.top = topposition + 'px';
	msg.style.left = leftposition + 'px';
	clearInterval(msg.timer);
	msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
	if(!autohide) {
		autohide = MSGHIDE;  
	}
	window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
	var msg = document.getElementById('msg');
	if(!msg.timer) {
		msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
	}
}

// fade the message box //
function fadeMsg(flag) {
	if(flag == null) {
		flag = 1;
	}
	var msg = document.getElementById('msg');
	var value;
	if(flag == 1) {
		value = msg.alpha + MSGSPEED;
	} else {
		value = msg.alpha - MSGSPEED;
	}
	msg.alpha = value;
	msg.style.opacity = (value / 100);
	msg.style.filter = 'alpha(opacity=' + value + ')';
	if(value >= 99) {
		clearInterval(msg.timer);
		msg.timer = null;
	} else if(value <= 1) {
		msg.style.display = "none";
		clearInterval(msg.timer);
	}
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
	var left = -100;
	if(target.offsetParent) {
		while (1) {
			left += target.offsetLeft;
			if (!target.offsetParent) {
				break;
			}
			target = target.offsetParent;
		}
	} else if(target.x) {
		left += target.x;
	}
	return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
	var top = 42;
	if(target.offsetParent) {
		while(1) {
			top += target.offsetTop;
			if(!target.offsetParent) {
				break;
			}
			target = target.offsetParent;
		}
	} else if(target.y) {
		top += target.y;
	}
	return top;
}

// preload the arrow //
if(document.images) {
	arrow = new Image(170,9); 
	arrow.src = "http://aca.aaa.com/img/home_msg_arrow.gif"; 
}