$(document).ready(function onload(){
	var strPhone = $("#phone")[0].value;
	if ($("#phone")[0].value != '') $("#phone")[0].value = strPhone.substr(0,3) + '-' + strPhone.substr(3,3) + '-' +strPhone.substr(6,4);
	operatedInput('phone','```-```-````');
});

function hideErrorBlock(id, message, display){

	if (message != '')
		$('#ErrorMessages').append('<li class="errorLI">' + message + '</li>'); 

	var html = "";
	if (display == 'inline')
		html = "&nbsp;*";
		
	document.getElementById(id + 'ErrorBox').style.display = display;
	document.getElementById(id + 'ErrorBox').innerHTML = html;
}

function phoneValid(id){
	$.post("/ajax/ajaxsimplycheckphone", { phone: document.getElementById(id).value },
			  function(data){
				Validation();
				if (data.result == false){
					document.getElementById(id).value = '';
					hideErrorBlock(id, data.messages, 'inline')
				} else {
					if (data.messages != '') alert(data.messages);
					hideErrorBlock(id, '', 'none');					
				}
				validForm();
			  }, "json");	
}

function checkRequire(id){
	if (document.getElementById(id).value == ''){
		hideErrorBlock(id, 'This field is required', 'inline');
	} else {
		hideErrorBlock(id, '', 'none');
	}
	validForm();
	Validation();
}

function emailValid(id){
	$.post("/ajax/ajaxsimplycheckemail", { email: document.getElementById(id).value },
			  function(data){
				Validation();
				if (data.result == false){
					document.getElementById(id).value = '';				
					hideErrorBlock(id, data.messages, 'inline');
				} else {					
					hideErrorBlock(id, '', 'none');
				}
				validForm();
			  }, "json");	
}

function validForm(){
	if (document.getElementById('nameErrorBox').innerHTML != '' ||	
		document.getElementById('emailErrorBox').innerHTML != '' ||
		document.getElementById('ReasonIDErrorBox').innerHTML != '' ||
		document.getElementById('commentErrorBox').innerHTML != '' ||
		document.getElementById('phoneErrorBox').innerHTML != ''){
		return false;
	} else {
		$("#errorNotice").css("display", "none");
		return true;
	}
}

function limitPromoDescription(object){
	var str = object.value;
	var lastsum = 800 - str.length
	if (str.length>800) {
		object.value = str.substr(0, 800);
	}
}

function resetCaptcha() {
	$.post("/ajax/ajaxresetcaptcha", {id: "1"},
			  function(data){
				if (data.result == true){
					var img = $("#captcha")[0].getElementsByTagName("img")[0];
					img.src = "/captcha/images/captcha/" + data.id + ".png";
					$("#captcha-id")[0].value = data.id;
				}
			  }, "json"); 	
}

function Validation() {
	$('#ErrorMessages').html("");

		if (document.getElementById('ReasonID').value == '') hideErrorBlock('ReasonID', 'Reason is required', 'inline');
		else hideErrorBlock('ReasonID', '', 'none');
		
		if (document.getElementById('comment').value == '') hideErrorBlock('comment', 'Comments is required', 'inline');
		else hideErrorBlock('comment', '', 'none');
		
		if (document.getElementById('email').value == '') hideErrorBlock('email', 'Email is required', 'inline');		
		else hideErrorBlock('email', '', 'none');
		
		return validForm();
}

function lastValidation() {
	if (Validation()){
		$("#errorNotice").css("display", "none");
		return true;
	} else {
		$("#errorNotice").css("display", "block");
		return false;
	}
}