
function showLayer(layerName, shadowLayerName)
{
	if (document.getElementById) // Netscape 6 and IE 5+
	{
		var targetElement = document.getElementById(layerName);
		var shadowElement = document.getElementById(shadowLayerName);
		targetElement.style.top = shadowElement.style.top;
		targetElement.style.visibility = 'visible';
	}
}


function hideLayer(layerName)
{
	if (document.getElementById)
	{
		var targetElement = document.getElementById(layerName);
		targetElement.style.visibility = 'hidden';
	}
}
/*
* Trim String function
*/
String.prototype.trim = function() {
	return(this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1'));
}
/*
* Tests if the string is an integer number.
* If the parameter s is defined the string could be and a negative integer.
*/
String.prototype.isInt = function(s)  {
	var digits="1234567890";
	var sign="+";
	if (s != undefined || s != null) sign="-+";

	//is already a non-empty string
	if(digits.indexOf(this.charAt(0)) == -1 && sign.indexOf(this.charAt(0)) == -1) {
		return false;
	}

	if (this.length == 1 && sign.indexOf(this.charAt(0)) != -1)  {
		return false;
	}

	for (var i=1; i < this.length; i++)  {
		if (digits.indexOf(this.charAt(i)) == -1) {
			return false;
		}
	}
	return true;
}

function clean(elem_name)
{
	elem_name = elem_name.replace(/\_+/g, " ")
	return elem_name.replace(/\[\]+/g, " ")
}

/***************************************/
function IsNumber(name,number)
{

	if(number.length>0)
	{

		ret=(number.length>0 && number.trim().length>0 && number.isInt())
		if(!ret)alert('Not a number "'+clean(name)+'"');
		return ret;
	}

	return true;

}


function IsNotEmpty(elem_name,val)
{

	if(val.length==0)
	{

		elem_name = elem_name.replace(/\_+/g, " ")
		elem_name = elem_name.replace(/\[\]+/g, " ")
		alert("Please insert "+elem_name+" !" );
		return false;
	}
	return true;
}

function general_validate(frm,types)
{
	f=document.getElementById(frm);
	for(var j=0 ;j< f.elements.length;j++)
	{

		elemType='';
		for(i=0 ;i< types.length;i++)
		{
			var arr = types[i].split("=");
			elemName = arr[0];

			if(elemName==f.elements[j].name && elemName.length>0)
			{

				elemValue = f.elements[j].value;
				elemType = arr[1];

				ret=true;
				eval("ret="+elemType+"(elemName,elemValue);");

				if(!ret)
				{
					return false;
				}

			}
		}
	}
	return true;
}

function dosubmit(frm,arrp,arrv,types)
{


	//validation
	if(types)
	{
		if(!general_validate(frm,types))
		return false;
	}


	str="";
	for(i=0;i<arrp.length;i++)
	{
		addField(document.getElementById(frm),'hidden',arrp[i],arrv[i]);

	}

	//alert(str);
	document.getElementById(frm).submit();

}
function addField (form, fieldType, fieldName, fieldValue) {
	if (document.getElementById) {
		var input = document.createElement('INPUT');
		if (document.all) { // what follows should work
			// with NN6 but doesn't in M14
			input.type = fieldType;
			input.name = fieldName;
			input.value = fieldValue;
		}
		else if (document.getElementById) { // so here is the
			// NN6 workaround
			input.setAttribute('type', fieldType);
			input.setAttribute('name', fieldName);
			input.setAttribute('value', fieldValue);
		}
		form.appendChild(input);
	}
}

function IsEmail(name,val)
{

	if(!is_email(val))
	{
		alert('Verify the email address format');
		return false;
	}
	return true;
}

function is_email(val)
{
	var at = val.indexOf("@")
	if(at == -1){
		return false
	}
	else
	{
		var at_parts = val.split("@")
		if(at_parts[0].length < 1){
			return false
		}
		else{
			point = at_parts[1].indexOf(".")
			if(point == -1) return false
			else{
				point_parts = at_parts[1].split(".")
				if((point_parts[1].length < 2) || (point_parts[1].length > 3)) return false
				else  return true
			}
		}
	}
}
function accept(str){
	acc="@_-!0123456789abcdefghijklmnoprstuvxzqywABCDEFGHIJKLMNOPRSTUVXZYQW"

	var ch
	var found = true

	if (str.length == 0)
	{
		return true
	}
	for (i = 0; i < str.length ;i++){
		ch = str.charAt(i)
		if(acc.indexOf(ch)==-1){
			found=false
		}
	}
	return found

}

function validate()
{
	f = document.frm
	isOK = true
	//usr_account = f.user_account.value
	usr_pass = f.user_passwd.value
	usr_pass_conf = f.user_passwd_confirm.value
	usr_em = f.user_email.value
	first_name=f.first_name;
	last_name=f.last_name;


	/*if (!accept(usr_account))
	{
	alert("Id is not accepted")
	return false
	}*/

	if (!accept(first_name))
	{
		alert("First name is not accepted")
		return false
	}

	if (!accept(last_name))
	{
		alert("Last name is not accepted")
		return false
	}

	/*if(usr_account.length < 4)
	{
	alert("Id minimum 4 chars")
	return false
	}*/
	if(usr_pass.length < 1)
	{
		alert("Please insert your password")
		return false
	}
	if(!is_email(usr_em))
	{
		alert("Verify the email address format !")
		return false
	}

	if(usr_pass_conf!=usr_pass)
	{
		alert("Please confirm the right password !")
		return false
	}
	return true
}

function formSubmit()
{
	if(validate())
	{
		document.frm.submit();
	}
}

function validate_agent()
{

	f=document.frm;
	usr_em = f.email.value
	first_name=f.first_name;
	last_name=f.last_name;
	mobile=f.mobile;
	fax=frm.fax;

	if(!is_email(usr_em))
	{
		alert("Verify the email address format !")
		return false
	}
	if (!accept(first_name))
	{
		alert("First name is not accepted")
		return false
	}

	if (!accept(last_name))
	{
		alert("Last name is not accepted")
		return false
	}

	if(first_name.length < 1)
	{
		alert("Please insert agent first name")
		return false
	}
	if(last_name.length < 1)
	{
		alert("Please insert agent last name")
		return false
	}
	if(mobile.length < 1)
	{
		alert("Please insert agent mobile number")
		return false
	}
	if(fax.length < 1)
	{
		alert("Please insert agent fax number")
		return false
	}
	document.frm.submit();

}


function validate_new_building()
{
	if(general_validate('frm',new Array("full_address=IsNotEmpty","number_of_units_in_building=IsNumber",
	"number_of_stories_in_building=IsNumber")))
	{
		check_building();
		
	}
return false;
}


function check_building()
{
	
	var fctCall = function (xReq) {
		if (xReq) 
		{					
			var msg = xReq.responseText;		

			if(msg=="0")
			{
				document.frm.submit();
			}
			else
			{
				alert("Building full addrress not unique");
			}
		}
	}
	address=document.getElementById('full_address').value;


	execReq("actions.php",fctCall, 'POST', "action=is_addr_uniq&full_address="+address );

}

function add_unit()
{
	if(general_validate('frm',new Array(
	"ap_on_wich_floor=IsNumber",
	"distance_to_subway=IsNumber",
	"distance_to_bus=IsNumber",
	"distance_to_school=IsNumber",
	"apartment_number=IsNotEmpty")))
	{

		document.frm.submit();
		return true;
	}
	return false;

}


