$(document).ready(function(){
	$("#City").change(function(){
		$.ajax({
				type: 'GET',
				url: 'include/ajax.php',
				data: 'action=getTownship&SelectClass=DefaultSelectMenu&culture='+$("#lang").val()+'&ParentID='+$("#City").val(),
				dataType: 'html',
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				
				beforeSend: function(result)
				{
					$("#Township").html(selectOptionLoading());
				},
				
				error: function(result)
				{
					//ajaxError(result.responseText);
				},
				
				success: function(result) {
					$("#Township").html(result);
					$("#Area").html(selectOptionDefault());
				}
			});		
	});
	
	$("#Township").change(function(){
		$.ajax({
				type: 'GET',
				url: 'include/ajax.php',
				data: 'action=getArea&SelectClass=DefaultSelectMenu&culture='+$("#lang").val()+'&ParentID='+$("#Township").val(),
				dataType: 'html',
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				
				beforeSend: function(result)
				{
					$("#Area").html(selectOptionLoading());
				},
				
				error: function(result)
				{
					//ajaxError(result.responseText);
				},
				
				success: function(result) {
					$("#Area").html(result);
				}
			});		
	});
	
	
	$("#mail-list a").click(function(){
		var $first_last_name = $("#first_last_name");
		var $email = $("#email");
		var pattern  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
		
		if($first_last_name.val() == '')
		{
			alert(QuestionFirstLastName);
			$first_last_name.focus();
			return false;
		}
		
		if(!pattern.test($email.val()))
		{
			alert(QuestionEmail);
			$email.focus();
			return false;
		}

		$.ajax({
			type: 'POST',
			url: 'include/ajax.php',
			data: 'action=addMailList&first_last_name='+$first_last_name.val()+'&email='+$email.val()+'&culture='+$("#lang").val(),
			dataType: 'html',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			
			beforeSend: function(result)
			{
				$("#mail-list  span").html(''+Processing+'');
			},
			
			error: function(result)
			{
				//ajaxError(result.responseText);
			},
			
			success: function(result) {
				$("#mail-list #first_last_name").val("");
				$("#mail-list #email").val("");	
				$("#mail-list  span").html(result);
			}
		});
		return false;
	});
	
	$("#poll .vote").click(function(){
	   	$("#frmPoll").submit();
		return false;
	});
	
	$("#right-credit #BankName").change(function(){
		
		var $ParentID = $("#right-credit #BankName");
		$.ajax({
			type: 'GET',
			url: 'include/ajax.php',
			data: 'action=getCreditCurrency&ParentID='+$ParentID.val()+'&culture='+$("#lang").val(),
			dataType: 'html',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			error: function(result)
			{
				ajaxError(result.responseText);
			},
			
			success: function(result) {
				$("#right-credit  #CreditCurrency").html(result);
			}
		});
	});
	
	$("#right-credit #CreditCurrency").change(function(){
		var $ParentID = $("#right-credit #BankName");
		var $Currency = $("#right-credit #CreditCurrency");
		$.ajax({
			type: 'GET',
			url: 'include/ajax.php',
			data: 'action=getCreditFinancing&ParentID='+$ParentID.val()+'&Currency='+$Currency.val()+'&culture='+$("#lang").val(),
			dataType: 'html',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			error: function(result)
			{
				ajaxError(result.responseText);
			},
			
			success: function(result) {
				$("#right-credit  #CreditFinancing").html(result);
			}
		});
	
	});
	
	$("#right-credit .Calculate").click(function(){
		
		var $BankName = $("#right-credit #BankName");
		var $Amount = $("#right-credit #Amount");
		var $Currency = $("#right-credit #CreditCurrency");
		var $CreditFinancing = $("#right-credit #CreditFinancing");
		
		if($BankName.val() == -1)
		{
			alert(''+QuestionBankName+'');
			$BankName.focus();
			return false;
		}
		
		if($Amount.val() == 0 || $Amount.val() == '' || $Amount.val() < 1)
		{
			alert(''+QuestionAmount+'');
			$Amount.focus();
			return false;
		}
		
		if($Currency.val() == -1)
		{
			alert(''+QuestionCurrency+'');
			$Currency.focus();
			return false;
		}
		
		if($CreditFinancing.val() == -1)
		{
			alert(''+QuestionRate+'');
			$CreditFinancing.focus();
			return false;
		}
		
		$.ajax({
			type: 'GET',
			url: 'include/ajax.php',
			data: 'action=getCreditResult&BankID='+$BankName.val()+'&Amount='+$Amount.val().replace(".","")+'&Currency='+$Currency.val()+'&CreditFinancing='+$CreditFinancing.val()+'&culture='+$("#lang").val(),
			dataType: 'html',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			error: function(result)
			{
				ajaxError(result.responseText);
			},
			
			success: function(result) {
				$("#right-credit  #CreditResult").css("display", "block");
				$("#right-credit  #CreditResult").html(result);
			}
		});
	return false;
	});
	
	
	$("#currency-converter .calculate-convert").click(function()
	{
		var $FromAmount = $("#currency-converter #FromAmount");
		var $CurrencyFrom = $("#currency-converter #CurrencyFrom");
		var $CurrencyTo = $("#currency-converter #CurrencyTo");
		
		if($FromAmount.val() == 0 || $FromAmount.val() == '' || $FromAmount.val() < 1)
		{
			alert(''+QuestionAmount+'');
			$FromAmount.focus();
			return false;
		}
		
		if($CurrencyFrom.val() == -1)
		{
			alert(''+QuestionCurrency+'');
			$CurrencyFrom.focus();
			return false;
		}
		
		if($CurrencyTo.val() == -1)
		{
			alert(''+QuestionCurrencyTo+'');
			$CurrencyTo.focus();
			return false;
		}
		
		$.ajax({
			type: 'GET',
			url: 'include/ajax.php',
			data: 'action=getCurrencyConverter&FromAmount='+$FromAmount.val().replace(".","")+'&CurrencyFrom='+$CurrencyFrom.val()+'&CurrencyTo='+$CurrencyTo.val()+'&culture='+$("#lang").val(),
			dataType: 'html',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			error: function(result)
			{
				ajaxError(result.responseText);
			},
			
			success: function(result) {
				$("#currency-converter  #ConvertResult").css("display", "block");
				$("#currency-converter  #ConvertResult").html(result);
			}
		});
	return false;
	});
	
	$("#more-info-send").click(function(){
		var $first_last_name_more = $("#first_last_name_more");
		var $Phone_more = $("#Phone_more");
		var $Email_more = $("#Email_more");
		var $InfoMessages_more = $("#InfoMessages_more");
		var $image_text_more = $("#image_text_more");
		
		if($first_last_name_more.val() == '')
		{
			alert(''+QuestionFirstLastName+'');
			$first_last_name_more.focus();
			return false;
		}
		
		if($Phone_more.val() == '')
		{
			alert(''+QuestionPhone+'');
			$Phone_more.focus();
			return false;
		}
		
		var pattern  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
		if(!pattern.test($Email_more.val()))
		{
			alert(QuestionEmail);
			$Email_more.focus();
			return false;
		}
		
		if($InfoMessages_more.val() == '')
		{
			alert(''+QuestionMess+'');
			$InfoMessages_more.focus();
			return false;
		}
		
		if($image_text_more.val() == '')
		{
			alert(''+QuestionConfirmCode+'');
			$image_text_more.focus();
			return false;
		}
		
		$.ajax({
			type: 'GET',
			url: 'include/ajax.php',
			data: $("#frmInfo").serialize().replace("action_more","action"),
			dataType: 'html',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			
			error: function(result)
			{
				ajaxError(result.responseText);
			},
			
			beforeSend: function(result)
			{
					$("#more-information .process-more").css("display", "block");
			},
			
			success: function(result) {
				$("#more-information .process-more").css("display", "none");
				if(result.search("{:err:}") == 0)
				{
					
					alert(result.replace("{:err:}",""));
				}
				else
				{
					$("#more-information .module-content").html(result);
				}
			}
		});
		return false;
	});
	
	$("#more-info-link").click(function(){
		$("#first_last_name_more").val("");
		$("#Phone_more").val("");
		$("#Email_more").val("");
		$("#InfoMessages_more").val("");
		$("#image_text_more").val("");
	});

	$("#send-friend-send").click(function(){
		
		var $first_last_name_friend = $("#first_last_name_friend");
		var $first_last_name_friend_friend = $("#first_last_name_friend_friend");
		var $Email_friend = $("#Email_friend");
		var $Email_friend_friend = $("#Email_friend_friend");
		var $image_text_friend = $("#image_text_friend");
		
		if($first_last_name_friend.val() == '')
		{
			alert(''+QuestionFirstLastName+'');
			$first_last_name_friend.focus();
			return false;
		}
		
		if($first_last_name_friend_friend.val() == '')
		{
			alert(''+QuestionFriendName+'');
			$first_last_name_friend_friend.focus();
			return false;
		}
		
		var pattern  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
		if(!pattern.test($Email_friend.val()))
		{
			alert(QuestionEmail);
			$Email_friend.focus();
			return false;
		}
		
		if(!pattern.test($Email_friend_friend.val()))
		{
			alert(QuestionFriendEmail);
			$Email_friend_friend.focus();
			return false;
		}
		
		if($image_text_friend.val() == '')
		{
			alert(''+QuestionConfirmCode+'');
			$image_text_friend.focus();
			return false;
		}
		
		$.ajax({
			type: 'GET',
			url: 'include/ajax.php',
			data: $("#frmFriend").serialize().replace("action_friend","action"),
			dataType: 'html',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			
			error: function(result)
			{
				ajaxError(result.responseText);
			},
			
			beforeSend: function(result)
			{
					$("#send-friend-main .process-friend").css("display", "block");
			},
			
			success: function(result) {
				$("#send-friend-main .process-friend").css("display", "none");
				if(result.search("{:err:}") == 0)
				{
					
					alert(result.replace("{:err:}",""));
				}
				else
				{
					$("#send-friend-main .module-content").html(result);
				}
			}
		});
		return false;
	});
	
	$("#send-friend-link").click(function(){
		$("#first_last_name_friend").val("");
		$("#first_last_name_friend_friend").val("");
		$("#Email_friend").val("");
		$("#Email_friend_friend").val("");
		$("#image_text_friend").val("");
	});
	
	$("#advert-detail-map").click(function(){
		window.open(''+$("#advert-detail-map a").attr("href")+'', 'GoogleMap', "location=0,status=0,scrollbars=0,width=730,height=500");
		return false;
	});
	
	$("#advert-detail-video a").click(function(){
		window.open(''+$(this).attr("href")+'', 'Video', "location=0,status=0,scrollbars=0,width=600,height=450");
		return false;
	});
	
	
	$("#contact-form a").click(function(){
		var $first_last_name = $("#FirstLastName");
		var $messages = $("#Messages");
		var $img_text = $("#image_text");
		
		if($first_last_name.val() == '')
		{
			alert(''+QuestionFirstLastName+'');
			$first_last_name.focus();
			return false;
		}
		
		if($messages.val() == '')
		{
			alert(''+QuestionMess+'');
			$messages.focus();
			return false;
		}
		
		if($img_text.val() == '')
		{
			alert(''+QuestionConfirmCode+'');
			$img_text.focus();
			return false;
		}
		
		$.ajax({
			type: 'GET',
			url: 'include/ajax.php',
			data: $("#frmContact").serialize().replace("action_friend","action"),
			dataType: 'html',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			
			error: function(result)
			{
				ajaxError(result.responseText);
			},
			
			beforeSend: function(result)
			{
					$("#contact-form .process-contact").css("display", "block");
			},
			
			success: function(result) {
				$("#contact-form .process-contact").css("display", "none");
				if(result.search("{:err:}") == 0)
				{
					alert(result.replace("{:err:}",""));
				}
				else
				{
					$("#contact-form").html(result);
				}
			}
		});
		return false;
	});
	
	$("#add_City").change(function(){
		$.ajax({
				type: 'GET',
				url: 'include/ajax.php',
				data: 'action=add-getTownship&SelectClass=DefaultSelectMenu&culture='+$("#lang").val()+'&ParentID='+$("#add_City").val(),
				dataType: 'html',
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				
				beforeSend: function(result)
				{
					$("#add_Township").html(selectOptionDefaultSelect());
				},
				
				error: function(result)
				{
					ajaxError(result.responseText);
				},
				
				success: function(result) {
					$("#add_Township").html(result);
					$("#add_Area").html(selectOptionDefaultSelect());
				}
			});		
	});
	
	$("#add_Township").change(function(){
		$.ajax({
				type: 'GET',
				url: 'include/ajax.php',
				data: 'action=add-getArea&SelectClass=DefaultSelectMenu&culture='+$("#lang").val()+'&ParentID='+$("#add_Township").val(),
				dataType: 'html',
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				
				beforeSend: function(result)
				{
					$("#add_Area").html(selectOptionLoading());
				},
				
				error: function(result)
				{
					ajaxError(result.responseText);
				},
				
				success: function(result) {
					$("#add_Area").html(result);
				}
			});		
	});
	
	$("#city_register").change(function(){
		$.ajax({
				type: 'GET',
				url: 'include/ajax.php',
				data: 'action=getTownship1&SelectClass=DefaultSelectMenu&culture='+$("#lang").val()+'&ParentID='+$("#city_register").val(),
				dataType: 'html',
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				
				beforeSend: function(result)
				{
					$("#township_register").html(selectOptionLoading());
				},
				
				error: function(result)
				{
					ajaxError(result.responseText);
				},
				
				success: function(result) {
					$("#township_register").html(result);
					$("#area_register").html(selectOptionDefault());
				}
			});		
	});
	
	$("#township_register").change(function(){
		$.ajax({
				type: 'GET',
				url: 'include/ajax.php',
				data: 'action=getArea1&SelectClass=DefaultSelectMenu&culture='+$("#lang").val()+'&ParentID='+$("#township_register").val(),
				dataType: 'html',
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				
				beforeSend: function(result)
				{
					$("#area_register").html(selectOptionLoading());
				},
				
				error: function(result)
				{
					ajaxError(result.responseText);
				},
				
				success: function(result) {
					$("#area_register").html(result);
				}
			});		
	});
	
	$("#register .module-button-ext a").click(function(){
		Error = "";
		validateNewEmail($("#email_register"), ""+QuestionEmail+"");
		if (Error== '')
		validatePassword($("#passwd"), ""+QuestionPassword+"");
		if (Error == '')
		validateMatchStr($("#passwd"), $("#re_passwd"), ""+PasswdMatch+"");
		if (Error == '')
		validateNewNotEmpty($("#name"), ""+QuestionName+"");
		if (Error == '')
		validateNewNotEmpty($("#last_name"), ""+QuestionLastName+"");
		
		if (Error == '')
		{
			if($("#user_type").val() == "company")
			{
				validateNewNotEmpty($("#company_name"), ""+QuestionCompanyName+"");
				if (Error == '')
				validateNewNotEmpty($("#address"), ""+QuestionAddress+"");
			}
		}
		if (Error == '')
		validateSelectMenu($("#city_register"), ""+QuestionSelectCity+"");
		if (Error == '')
		validateSelectMenu($("#township_register"), ""+QuestionSelectTownShip+"");
		if (Error == '')
		validateSelectMenu($("#area_register"), ""+SelectArea+"");
		if (Error == '')
		validateNewNotEmpty($("#phone"), ""+QuestionPhone+"");
		if (Error == '')
		validateNewNotEmpty($("#gsm"), ""+QuestionGsm+"");
		if (Error == '')
		validateNewNotEmpty($("#image_text"), ""+QuestionConfirmCode+"");
		
		
		if (Error=='')
			$("#frmRegister").submit();
		else 
			alert(Error);
		return false;
	});
	
	$(".login-button").click(function(){
		Error = "";
		validateNewEmail($("#email_login"), ""+QuestionEmail+"");
		if (Error == '')
		validateNewNotEmpty($("#passwd_login"), ""+ValidatePassword+"");
		if (Error=='')
			$("#frmLogin").submit();
		else 
			alert(Error);
		return false;
	});
	
	$(".add-user-advert-link").click(function(){
		$("#frmUserAddAdvert").submit();
		return false;
	});
	
	$(".confirm-delete").click(function(){
		return confirm(""+QuestionDeleteRecord+"");
	});
	
	$(".confirm-images").click(function(){
		return confirm(""+QuestionDeleteImage+"");
	});
	
	$(".confirm-stop").click(function(){
		return confirm(""+QuestionRemoveRecord+"");
	});
	
	$(".confirm-start").click(function(){
		return confirm(""+QuestionPublishRecord+"");
	});
	
	$("#forgot-password a").click(function(){
		Error = "";
		validateNewEmail($("#email_forgot"), ""+QuestionEmail+"");
		if (Error == '')
		validateNewNotEmpty($("#image_text"), ""+QuestionConfirmCode+"");
		if (Error=='')
			$("#frmForgotEmail").submit();
		else 
			alert(Error);
		return false;
	});
	
	$("#profile .module-button-ext a").click(function(){
		Error = "";
		validateNewNotEmpty($("#name"), ""+QuestionName+"");
		if (Error == '')
		validateNewNotEmpty($("#last_name"), ""+QuestionLastName+"");
		
		if (Error == '')
		{
			if($("#user_type").val() == "company")
			{
				validateNewNotEmpty($("#company_name"), ""+QuestionCompanyName+"");
				if (Error == '')
				validateNewNotEmpty($("#address"), ""+QuestionAddress+"");
			}
		}
		if (Error == '')
		validateSelectMenu($("#city_register"), ""+QuestionSelectCity+"");
		if (Error == '')
		validateSelectMenu($("#township_register"), ""+QuestionSelectTownShip+"");
		if (Error == '')
		validateSelectMenu($("#area_register"), ""+SelectArea+"");
		if (Error == '')
		validateNewNotEmpty($("#phone"), ""+QuestionPhone+"");
		if (Error == '')
		validateNewNotEmpty($("#gsm"), ""+QuestionGsm+"");
		
		if (Error=='')
			$("#frmProfile").submit();
		else 
			alert(Error);
		return false;
	});
	
	$("#passwd .module-button-ext a").click(function(){
		Error = "";
		/*
		validatePassword($("#old_passwd"), ""+QuestionPassword+"");
		if (Error== '')
		validatePassword($("#new_passwd"), ""+QuestionPassword+"");
		if (Error == '')
		validateMatchStr($("#new_passwd"), $("#re_passwd"), ""+PasswdMatch+"");
		*/
		if (Error=='')
			$("#frmPasswd").submit();
		else 
			alert(Error);
		return false;
	});
	
	$("#user-image .module-button-ext a").click(function(){
		Error = "";
		if (Error=='')
			$("#frmUserImage").submit();
		else 
			alert(Error);
		return false;
	});
	
});
