/**
 * Quirystring functions
 */
function getQueryStringParams() {
	var qsParams = new Array();
	var qs = window.location.search;
	var qsa = qs.substring(1).split("&");
	for (var i = 0; i < qsa.length; i++) {
		var qpa = qsa[i].split("=");
		qsParams[qpa[0]] = unescape(qpa[1]);
	}
	return qsParams;
}

function buildQueryString(params) {
	var qs = "";
	for (pName in params) {
		qs += "&"+pName+"="+escape(params[pName]);
	}
	return (qs == "" ? "" : "?"+qs.substring(1));
}



/**
 * Cookie functions
 */
function getCookieValue(name) {
	var label = name+"=";
	var labelLen = label.length;
	var cLen = document.cookie.length;
	var i = 0;
	while (i < cLen) {
		var j = i + labelLen;
		if (document.cookie.substring(i,j) == label) {
			var cEnd = document.cookie.indexOf(";",j)
			if (cEnd == -1) {
				cEnd = cLen;
			}
			return unescape(document.cookie.substring(j,cEnd));
		}
		i++;
	}
	return null;
}

function setCookieValue(name, value, years) {
	var exp = new Date();
	var oneYear = (365 * 24 * 60 * 60 * 1000);
	exp.setTime(exp.getTime() + Math.round(parseInt(years) * oneYear));
	document.cookie = escape(name) + "=" + escape(value) + "; expires=" + exp.toGMTString() + "; path=/";
}



/**
 * Popup functions
 */


/**
 * Opens a new window with the specified url. Example:
 *
 *    openPopup("mypage.html", "myPopupWindow", null, null, 400, null);
 *
 * @param  url            url of the page to load in the popup window, null will
 *                        load 'about:blank' in the popup window.
 * @param  windowName     name of the popup window, use null or '_blank' to open
 *                        a new window every time.
 * @param  windowX        screen x coordinate for the popup window, null will
 *                        center the popup window horizontaly, a negative value
 *                        will align the popup window to the right side of the
 *                        screen keeping the specified amount of space between
 *                        the right side of the window and the right side of the
 *                        screen.
 * @param  windowY        screen y coordinate for the popup window, null will
 *                        center the popup window verticaly, a negative value
 *                        will align the popup window to the bottom of the screen
 *                        keeping the specified amount of space between the bottom
 *                        of the window and the bottom of the screen.
 * @param  windowWidth    (inner) width of the popup window, null will size the
 *                        window to the available screen width minus 20,
 *                        a negative value will subtract that value from the
 *                        available screen width.
 * @param  windowHeight   (inner) height of the popup window, null will size the
 *                        window to the available screen height minus 180,
 *                        a negative value will subtract that value from the
 *                        available screen height.
 * @return                a reference to the opened window.
 */
function openPopup(url, windowName, windowFlags, windowX, windowY, windowWidth, windowHeight) {
	// constants
	var windowBorderV = 25 + 6;
	var windowBorderH = 2 * 6;

	// set defaults
	if (url == null)           url = "about:blank";
	if (windowName == null)    windowName = "_blank";
	if (windowFlags == null)   windowFlags = "resizable,scrollbars";
	if (windowWidth == null)   windowWidth = screen.availWidth - 20;
	else if (windowWidth < 0)  windowWidth = screen.availWidth + windowWidth;
	if (windowHeight == null)  windowHeight = screen.availHeight - 180;
	else if (windowHeight < 0) windowHeight = screen.availHeight + windowHeight;
	if (windowX == null)       windowX = (screen.availWidth - (windowWidth + windowBorderH)) / 2;
	else if (windowX < 0)      windowX = screen.availWidth + windowX - (windowWidth + windowBorderH);
	if (windowY == null)       windowY = (screen.availHeight - (windowHeight + windowBorderV)) / 2;
	else if (windowY < 0)      windowY = screen.availHeight + windowY - (windowHeight + windowBorderV);

	// prepare flags
	/*
	var windowFlags = "resizable";
	windowFlags += ",scrollbars";
	windowFlags += ",directories";
	windowFlags += ",location";
	windowFlags += ",menubar";
	windowFlags += ",status";
	windowFlags += ",toolbar";
	*/
	windowFlags += ",left=" + windowX + ",screenX=" + windowX;
	windowFlags += ",top=" + windowY + ",screenY=" + windowY;
	windowFlags += ",width=" + windowWidth + ",innerWidth=" + windowWidth;
	windowFlags += ",height=" + windowHeight + ",innerHeight=" + windowHeight;
	
	// open popup
	var popupWindow = window.open(url, windowName, windowFlags);
	popupWindow.focus();

	return popupWindow;
}

function openDefaultPopup(url) {
	openPopup(url, null, null, null, null, 600, null);
}

function openFotoPopup(url) {
	openPopup(url, "mccainconsumerfoto", null, null, null, 600, 460);
}

function openCountryPopup(selectBox) {
	if (selectBox.selectedIndex > 0) {
		var url = "http://" + selectBox.options[selectBox.selectedIndex].value;
		openPopup(url, null, "menubar,toolbar,location,status,resizable", 20, 20, -40, -220);
	}
}
