function openWindow(theURL, winName, features) {
	window.open(theURL, winName, features);
}

function validateSearchForm() {
	if(isEmpty("SearchQuery", "Search Text Box"))
		return false;
	return true;
}

function showPopup(title, body, height, width) {
	document.getElementById("popup_title").innerHTML = title;
	document.getElementById("popup_body").innerHTML = body;
	popup_show(height, width);
}

function validateSendEmailForm() {
	if(isEmpty("from", "From Email") || isEmpty("subject", "Subject") || isEmpty("message", "Message"))
		return false;
	else if(!validateEmail("from"))
		return false;
	xajax.call('/members/sendemail', {method: 'post', parameters:[xajax.getFormValues(xajax.$('email_form'))]});
	return false;
}

function validateFeedbackForm() {
	if(isEmpty("message", "Message"))
		return false;
	xajax.call('/feedback', {method: 'post', parameters:[xajax.getFormValues(xajax.$('email_form'))]});
	return false;
}

function enableBilling(is_checked, startRow) {
	for(var i = startRow; i < startRow + 6; i++) {
		var type = (navigator.appName == "Microsoft Internet Explorer") ? "block" : "table-row";
		document.getElementById("row-" + i).style.display = !is_checked ? "none" : type;
	}
}

function getZipCodeInformation(type) {
	var zip = document.getElementById(type + "Zip").value;
	document.getElementById(type + "State").disabled = true;
	document.getElementById(type + "City").disabled = true;
	document.getElementById(type + "Country").disabled = true;
	xajax.call("/member_addresses/zip_code/" + zip + "/" + type);
}

function rebuild(onUpdate) {
	var img_div = document.createElement("div");
	img_div.style.textAlign = 'center';
	img_div.style.margin = '20px';
	img_div.id = "loading-image";
	var img = document.createElement('img');
	img.src = "http://" + location.hostname + "/img/ajax-loader.gif";
	img_div.appendChild(img);
	document.getElementById(onUpdate).innerHTML = "";
	document.getElementById(onUpdate).appendChild(img_div);
}

function rebuildPerPage(onUpdate, name, selected) {
	rebuild(onUpdate);
	xajax.call("/rebuild/" + name + "/perPage/" + selected, {method: 'post', parameters:[{cakex_update:onUpdate}]});
	return false;
}

function showAnswer(n) {
	var elem = document.getElementById('a' + n);
	if(elem) {
		if(elem.style.display != 'block') {
			elem.style.display = 'block';
			elem.style.visibility = 'visible';
		} else {
			elem.style.display = 'none';
			elem.style.visibility = 'hidden';
		}
	}
}

function setButton(button) {
	if(document.getElementById("button"))
		document.getElementById("button").value = button;
}

function showLabel(value, event) {
	var div = document.createElement("div");
	div.id = "delivery_label";
	div.innerHTML = value;
	div.className = "text-hover";

	var ScrollTop = document.body.scrollTop;
	if(ScrollTop == 0) {
		if(window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}

	div.style.top = event.clientY + 10 + + ScrollTop + "px";
	div.style.left = event.clientX + 10 + "px";
	document.body.appendChild(div);
}

function removeLabel() {
	var div = document.getElementById("delivery_label");
	if(div)
		document.body.removeChild(div);
}

function hoverButton(button, classString) {
	if(navigator.appName != "Microsoft Internet Explorer") {
		if(classString.search(/purple/) != -1)
			button.className = button.className.replace(/purple/, "purple-hover");
		else if(classString.search(/orange/) != -1)
			button.className = button.className.replace(/orange/, "orange-hover");
	}
}

function unhoverButton(button, classString) {
	if(navigator.appName != "Microsoft Internet Explorer") {
		if(classString.search(/purple/) != -1)
			button.className = button.className.replace(/purple-hover/, "purple");
		else if(classString.search(/orange/) != -1)
			button.className = button.className.replace(/orange-hover/, "orange");
	}
}

function loadCategorySelect(id, options) {
	options = options.split("-&&-");
	var select = document.getElementById(id);
	select.options.length = 0;
	for(i = 0; i < options.length; i++) {
		var option = options[i].split("-::-");
		select.options[select.options.length] = new Option(option[1], option[0]);
	}
}

function checkFacebook() {
	if(document.getElementById("ApiLoginFacebookMessageDiv"))
		document.getElementById("ApiLoginFacebookMessageDiv").style.display = "none";
	if(document.getElementById("ApiLoginFacebookCheck").checked) {
		document.getElementById("ApiLoginFacebookCheck").checked = false;
		FB.Connect.showPermissionDialog("status_update", function(r) {
			FB.Facebook.apiClient.users_hasAppPermission("status_update", function(response) {
				document.getElementById("ApiLoginFacebookCheck").checked = (response == 1) ? true : false;
				if(document.getElementById("ApiLoginFacebookMessageDiv"))
					document.getElementById("ApiLoginFacebookMessageDiv").style.display = (response == 1) ? "" : "none";
			});
		});
	}
}

function checkTwitter(memberId, offerId) {
	if(document.getElementById("ApiLoginTwitterCheck").checked) {
		document.getElementById("ApiLoginTwitterCheck").checked = false;
		xajax.call('/api_logins/check/' + memberId + "/T/" + offerId, {method: 'post', parameters:[{cakex_update:'popup_body'}]});
	}
}