var submitedForm = null;

function mainSearchSubmit() {
	var el = $("searchfield");
	var v = el.value.replace(/^\s+|\s+$/g,"");
	if(v == "") {		
		alert('Enter the search keyword');
		el.focus();
		return false;
	}
	return true;
}

function ajaxStart(form) {
	submitedForm = form;
}

function ajaxStop(form) {	
	submitedForm = null;
}

function setFieldError(field, msg) {
	if($("#" + field + "_errors", submitedForm).size()) {
		$("#" + field + "_errors", submitedForm).after('<span class="error"><br />' + msg + '</span>');
	}
	else {
		$("#" + field, submitedForm).after('<span class="error"><br />' + msg + '</span>');
	}
}

function markError(field) {
	$("#" + field, submitedForm).after('<img class="mark_error" src="images/a_darlings/error_marker.gif" alt="error_marker" width="15" height="15" />');
}

function onSubmiAjaxForm(form, saveAction) {
	var query_string_obj = {};
	
	$("input,textarea,select", form).each( function() {
			var id = $(this).attr("id");
			
			if(($(this).attr("type") == "radio")) {
				if($(this).attr("checked") ) 
					query_string_obj[$(this).attr("name")] = $(this).val();
			}
			else if($(this).attr("type") == "checkbox") {
				if($(this).attr("checked") )
					if($(this).attr("name").substring(0,3)=='_R_')
						query_string_obj[$(this).attr("name").substring(3)] = $(this).val();
					else
						query_string_obj[id] = $(this).val();
			}
			else
			{
				query_string_obj[id] = $(this).val();
			}
	});
	
	$(".error, .mark_error", form).remove();
	
	
	query_string_obj["ajax"] = 1;
	query_string_obj["doFAct"] = "save";
	
	ajaxStart(form);
	
	$.post(saveAction, query_string_obj, 
		function(response) {			
			eval(response);
			ajaxStop(form);
	});
	
	return false;
}


function popupWnd(script, width, height) {
	
	if(typeof(width) == 'undefined') width = 700;
	if(typeof(height) == 'undefined') height = 700;
	var winl = (screen.width - width) / 2;
	var wint = (screen.height - height) / 2;
	var winprops = "scrollbars=yes,resizable=yes,fullscreen=no,height="+height+",width="+width+",top="+wint+",left="+winl+"";
	window.open(script, "popup", winprops);
}

function popupOrderFAQ() {
	popupWnd('popup.php?page=order_faq', 650, 600);
}

function popupOrderPrivacy() {
	popupWnd('popup.php?page=privacy_popup', 650, 600);
}

function popupOrderTerms() {
	popupWnd('popup.php?page=terms_popup', 650, 600);
}

$(document).ready(function(){
	applesearch.init();
});


