var adv = {
	options: {
		//target: "#adv_message", // target element(s) to be updated with server response 
		beforeSubmit: function(formData, jqForm, options){
			//var pars = $('#' + formId).serialize();
			document.getElementById('adv_ver_web').className = "load";
			document.getElementById('adv_ver_link').className = "load";
			$('#adv_message').hide();
			$('#adv_ver_web').show();
			$('#adv_ver_link').show();
			$('#status_banner').html('');
			$('#status_name').html('');
			$('#status_email').html('');
			$('#status_web').html('');
		},
		success: function(responseText, statusText){
			if (statusText == 'success') {
				if (responseText.LINK) {
					document.getElementById('adv_ver_web').className = "load checked";
					document.getElementById('adv_ver_link').className = "load checked";
					document.location = responseText.LINK;
				} else if (responseText.UPDATE) {
					$('#adv_ver_link').hide();
					$('#adv_ver_web').hide();
					
					for (key in responseText.UPDATE) {
						$('#' + responseText.UPDATE[key].id).html(responseText.UPDATE[key].html);
					}
				} else if (responseText.MSG) {
					if (responseText.UNCHECKED) {
						document.getElementById('adv_ver_link').className = "load unchecked";
						$('#adv_ver_web').hide();
						$('#adv_ver_link').html(responseText.MSG);
					} else {
						$('#adv_ver_link').hide();
						$('#adv_ver_web').hide();
						$('#adv_message').html(responseText.MSG);
						$('#adv_message').show();
					}
				}
			} else {
				$('#adv_ver_web').hide();
				$('#adv_ver_link').hide();
				$('#adv_message').html("System error!");
			}
		},
		dataType: "json",
		type: "post"
	}
}

var confirmPay = {
	del: function(id) {
		if (!confirm('Do you really want to delete this record?')) return false;
		var params = 'id=' + id;
		jQuery.post('confirm_pay.htm', params,
			function(response){
				if(response.ok) {
					$("#" + response.ok).remove();
					$("#total_cost").text(response.total);
				}
			},
			'json'
		)
	}
}

var advSep = {
	selectedCountry: null,
	selectedCity: null,
	beforeSubmit: null,
	success: null,
	fid: null,
	vpath: null,
	options: {
		//target: "", // target element(s) to be updated with server response 
		beforeSubmit: function(formData, jqForm, options) {
			$('#error_messages').html('');
			//$('#\\[ADV_MESSAGE\\]').html("");
			//$('#\\[ADV_CONTENT\\]').css('visibility','hidden');
			//$('#\\[ADV_LOADING\\]').width($('#\\[ADV_CONTENT\\]').attr('offsetWidth')); 
			//$('#\\[ADV_LOADING\\]').height($('#\\[ADV_CONTENT\\]').attr('offsetHeight')); 
			//$('#\\[ADV_LOADING\\]').show(); 
			
		},
		success: function(responseText, statusText) {
			if (statusText == 'success') {
				if (responseText.error) {
					if (responseText.error == 'session') {
						document.location = responseText.url
					}
					$('#error_messages').html(responseText.error);
				} else {
					document.location = responseText.url;
				} 
			} else {
				$('#error_messages').html("System error!");
			}
		},
		dataType: "json",
		type: "post"
	},
	findValue: function(sValue) {
		$('#cityAjax').val('');
		$('#adv_sep_region').val('');
		if (!sValue) {
			$('#cityArea').hide();
			$('#regionArea').hide();
		}
		else {
			//alert("The value you selected was: " + sValue);
			advSep.selectedCountry = sValue;
			if (!document.getElementById('country_city')) {
				var cc = document.createElement('input');
				cc.setAttribute('type', 'hidden');
				cc.name = "country_city";
				cc.id = "country_city";
				document.getElementById('cityArea').parentNode.appendChild(cc);
			}
			$('#cityArea').show();
			$('#regionArea').show();
			$("#cityAjax").autocomplete("../ajax/adv_sep_form.php", {
				delay: 5,
				minChars: 2,
				matchSubset: 1,
				matchContains: 1,
				cacheLength: 10,
				onItemSelect: advSep.selectCityItem,
				onFindValue: advSep.findCityValue,
				formatItem: advSep.formatItem,
				autoFill: true,
				extraParams: {
					flag: 'city',
					c_id: advSep.selectedCountry
				}
			})
		}
	},
	selectItem: function(li) {
		advSep.findValue(li);
	},
	findCityValue: function(li) {
		if(!li) return alert("No match!");
	
		if( !!li.extra ) var sValue = li.extra[0];
	
		//alert("The value you selected was: " + sValue);
		advSep.selectedCity = sValue;
		
		$("#country_city").val(advSep.selectedCity);
	},
	selectCityItem: function(li) {
		advSep.findCityValue(li);
	},
	formatItem: function(row) {
		return row[0];//should be addded + " (id: " + row[1] + ")";
	}
	
}
var search = {
	vpath: null,
	selectedCountry: null,
	findValue: function(sValue) {
		$('#cityAjax').val('');
		if (!sValue) {
			//$('#cityArea').hide();
		} else {
			//alert("The value you selected was: " + sValue);
			search.selectedCountry = sValue;
			if (!document.getElementById('country_city')) {
				var cc = document.createElement('input');
				cc.setAttribute('type', 'hidden');
				cc.name = "country_city";
				cc.id = "country_city";
				document.getElementById('cityAjax').parentNode.appendChild(cc);
			}
			$("#cityAjax").autocomplete("ajax/search_form.php", {
				delay: 5,
				minChars: 2,
				matchSubset: 1,
				matchContains: 1,
				cacheLength: 10,
				onItemSelect: search.selectCityItem,
				onFindValue: search.findCityValue,
				formatItem: search.formatItem,
				autoFill: true,
				extraParams: {
					co_id: search.selectedCountry
				}
			})
		}
	},
	selectItem: function(li) {
		search.findValue(li);
	},
	findCityValue: function(li) {
		if (!li) {
			$("#country_city").val('');
		}
		if( !!li.extra ) var sValue = li.extra[0];
		//alert("The value you selected was: " + sValue);
		search.selectedCity = sValue;
		$("#country_city").val(search.selectedCity);
	},
	selectCityItem: function(li) {
		search.findCityValue(li);
	}
}
var advSal = {
	options: {
		//target: "", // target element(s) to be updated with server response 
		beforeSubmit: function(formData, jqForm, options) {
			$('#error_messages').html('');
			
		},
		success: function(responseText, statusText) {
			if (statusText == 'success') {
				if (responseText.error) {
					if (responseText.error == 'session') {
						document.location = responseText.url
					}
					$('#error_messages').html(responseText.error);
				} else {
					document.location = responseText.url;
				} 
			} else {
				$('#error_messages').html("System error!");
			}
		},
		dataType: "json",
		type: "post"
	}
}