function filterSpam(input) {
	if(input.search(/href/i)==-1) {return true;}
	else {return false;}
}

function emailCheck(emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if (emailStr=='') {
	return false 
}
if (matchArray==null) {
	return false
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null) {
    return false
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
		return false
	    }
    }
    return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null) {
    return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>6) {
   return false
}
if (len<2) {
   var errStr="This address is missing a hostname!"
   return false
}
return true;
}

$(function(){
    $('input').keydown(function(e){								
        if (e.keyCode == 13) {			
            valid_submission(this);
            return false;
        }
    });
});

function valid_submission(inp){
	if($(inp).val()!='') {
		search_id_submit($(inp).parents('form').attr('name'),$(inp).parents('form').attr('action'));
	}
	
}

function infoRequestSubmit(type,ID) {
	
	var valid = true;
	
	$("div#add-block-notice").html("");
	$("div#noname").html("");
	$("div#nomail").html("");
	$("div#invcomment").html("");
	
	if($("input#name").val()=="") {
			$("div#noname").html("Missing name !");
			valid = false;
	}
	
	if(!emailCheck($("input#mail").val())) {
			$("div#nomail").html("Missing e-mail address / invalid e-mail address !");
			valid = false;
	}
	
	if(!filterSpam($("#comment").val())) {
			$("div#invcomment").html("Invalid symbols in text comment. Spam attempt ?");
			valid = false;
	}

	if(valid) {
		$.ajax(
				 {  
					type: "POST",  
					url: "/scripts/mailer.php",  
					data: "name="+$("input#name").val()+"&mail="+$("input#mail").val()+"&phone="+$("input#phone").val()+"&comment="+$("#comment").val()+"&type="+type+"&ID="+ID+"&ReferURL="+$("input#ReferURL").val()+"&request=infoRequest",
					dataType: "html",
					success: function(result) {
							$("div#add-block-notice").html(result);
						}
				 }
			  );
	}

}

function commonRequestSubmit() {
	
	var valid = true;
	
	$("div#add-block-notice").html("");
	$("div#noname").html("");
	$("div#nomail").html("");
	$("div#invcomment").html("");
	
	if($("input#name").val()=="") {
			$("div#noname").html("Missing name !");
			valid = false;
	}
	
	if(!emailCheck($("input#mail").val())) {
			$("div#nomail").html("Missing e-mail address / invalid e-mail address !");
			valid = false;
	}
	
	if(!filterSpam($("#comment").val())) {
			$("div#invcomment").html("Invalid symbols in text comment. Spam attempt ?");
			valid = false;
	}
	
	type = $("#property_typeB option:selected").html();
	loc = $("#locationB option:selected").html();
	area_min = $("#area_min").val();
	area_max = $("#area_max").val();
	price_min = $("#price_min").val();
	price_max = $("#price_max").val();
	comment = $("#comment").val();
	
	if(valid) {
		$.ajax(
				 {  
					type: "POST",  
					url: "/scripts/mailer.php",  
					data: "name="+$("input#name").val()+"&mail="+$("input#mail").val()+"&comment="+$("#comment").val()+"&type="+type+"&location="+loc+"&area_min="+area_min+"&area_max="+area_max+"&price_min="+price_min+"&price_max="+price_max+"&comment"+comment+"&ReferURL="+$("input#ReferURL").val()+"&request=infoRequestCommon",
					dataType: "html",
					success: function(result) {
							$("div#add-block-notice").html(result);
						}
				 }
			  );
	}
}

function callback_submit()
{
	var valid = true;
	
	$("div#add-block-notice").html("");
	$("div#noname").html("");
	$("div#nomail").html("");
	$("div#invcomment").html("");
	
	if($("input#name").val()=="") {
			$("div#noname").html("Missing name !");
			valid = false;
	}
	
	if($("input#phone").val()=="") {
			$("div#nophone").html("Missing name !");
			valid = false;
	}
	
	if(!emailCheck($("input#mail").val())) {
			$("div#nomail").html("Missing e-mail address / invalid e-mail address !");
			valid = false;
	}
	
	
	if(valid) {
		$.ajax(
				 {  
					type: "POST",  
					url: "/scripts/mailer.php",  
					data: "name="+$("input#name").val()+"&mail="+$("input#mail").val()+"&phone="+$("input#phone").val()+"&city="+$("input#city").val()+"&ReferURL="+$("input#ReferURL").val()+"&request=callback",
					dataType: "html",
					success: function(result) {
							$("div#add-block-notice").html(result);
						}
				 }
			  );
	}
}


function search_submit(form,fieldID,mode) {

var url = new Array;

if(mode=='detail')
{
	url['apartment'] = '/apartments-in-Bulgaria.html?order=price';
	url['house'] = '/houses-in-Bulgaria.html?order=price';
	url['village'] = '/private-villages-in-Bulgaria/';
	url['land'] = '/land-plots-in-Bulgaria.html?order=price';
	url['hotel'] = '/hotels-in-Bulgaria.html?order=price';
	url['comm'] = '/commercial-premises-in-Bulgaria/';
	url['building'] = '/buildings-for-sale-in-Bulgaria/';
}
else
{
	url['apartment'] = '/scripts/apts-redirect.php';	
	url['house'] = '/scripts/house-redirect.php';
	url['land'] = '/scripts/land-redirect.php';
}

action = url[''+$('#'+fieldID+' option:selected').val()+''];

$('#'+form).attr('action',action);
$('#'+form).submit();

}

function search_form_submit(formName,url,page,numBegin,begin,order,type)
{
	var containerName = "focus";
	$("form[name='"+formName+"']").attr('action',url+"?page="+page+"&numBegin="+numBegin+"&begin="+begin+"&order="+order+"&type="+type+"#"+containerName);
	$("form[name='"+formName+"']").submit();
}

function search_id_submit(formName,url)
{
	ID = $("input[name='ID']").val();
	$("form[name='"+formName+"']").attr('action',url+"/Bulgaria/"+ID);
	$("form[name='"+formName+"']").submit();
}

function pricerange_submit(formName,url,price_range,fields)
{
	page = 1;
	numBegin = 1;
	begin = 0;
	order = 'price';
	type = 0;
	$("input[name='"+fields[0]+"']").val(price_range[0]);
	$("input[name='"+fields[1]+"']").val(price_range[1]);
	search_form_submit(formName,url,page,numBegin,begin,order,type);
}

function validate_and_submit(formName, JSONobject)
{	
	var json = $.parseJSON(JSONobject);
	var valid = Array;
	
	$.each(json.fields,function(i,items) {

			valid[items.field_name] = false;
			
			switch(items.type)
			{
				case 'text':
				case 'mail':
					type = "input";
				break;
				case 'textarea':
					type = "textarea";
				break;
			}
			
			value = $(type+"[name='"+items.field_name+"']").attr('value');
			
			if(items.notempty)
			{
				if(value != '') 
				{
					if(items.regex != 'undefined')
					{
						if(value.match(new RegExp(items.regex))) {valid[items.field_name] = false;} else {valid[items.field_name] = true;}	
					} else {
						valid[items.field_name] = true;
					}
					if(items.type == 'mail')
					{
						if(emailCheck(value))
						{
							valid[items.field_name] = true;	
						}
					}
				}
			} else {valid[items.field_name] = true;}
			
			if(!valid[items.field_name]) 
			{
				$('#'+items.field_name+'_alert').html(items.errmessage);
			} else {
				$('#'+items.field_name+'_alert').html('');
			};
			
	});
	
	for(var val in valid)
	{
		if(!valid[val]) {return false;}
	}
	
	$("form[name='"+formName+"']").submit();
}