function myAlert(inMensaje, inCampo)
{
	$(document).ready(function() {
		var $dialog = $('<div></div>')
			.html(inMensaje)
			.dialog({
				autoOpen: false,
				title: "Warning",
				modal: true,
				buttons: {
					Ok: function() {
						$( this ).dialog( "close" );
						inCampo.focus();
					}
				}
			});

		$dialog.dialog('open');
	});
}

function validarFrmOperacion()
{	
	if(frmOperacion.chkForSale.checked != true && frmOperacion.chkForRent.checked != true)
	{
		myAlert("Please select Apartments for Sale or/and Rent.", frmOperacion.chkForSale);
		return false;
	}

    return true;
}

function validarDecimalPositivo(inTexto)
{
	var objRegExp  = /^\d+(.\d+)?$/;
	return objRegExp.test(inTexto) && inTexto > 0;
}

function validarFrmPrecio()
{	
	if(frmPrecio.txtPrecioDesde.value != "" && frmPrecio.txtPrecioHasta.value != "")
	{
		if(!validarDecimalPositivo(frmPrecio.txtPrecioDesde.value))
		{
			myAlert("Price From must be a Number.", frmPrecio.txtPrecioDesde);
			return false;
		}
		if(!validarDecimalPositivo(frmPrecio.txtPrecioHasta.value))
		{
			myAlert("Price To must be a Number.", frmPrecio.txtPrecioHasta);
			return false;
		}
		if(parseFloat(frmPrecio.txtPrecioDesde.value) > parseFloat(frmPrecio.txtPrecioHasta.value))
		{
			myAlert("Price From must be less than the Price To.", frmPrecio.txtPrecioDesde);
			return false;
		}
	}
	else
	{
		myAlert("Please enter a Rate / Price Range.", frmPrecio.txtPrecioDesde);
		return false;
	}

    return true;
}

function validarFrmFechas()
{
    if(frmFechas.txtFechaDesde.value == "")
    {
		myAlert("Please enter the date of Check In.", frmFechas.txtFechaDesde);
        return false;
    }
    if(frmFechas.txtFechaHasta.value == "")
    {
		myAlert("Please enter the date of Check Out.", frmFechas.txtFechaHasta);
        return false;
    }
    
    var str1 = frmFechas.txtFechaDesde.value;
    var str2 = frmFechas.txtFechaHasta.value;
    
    var y1 = parseInt(str1.substring(0,4), 10);
    var m1 = parseInt(str1.substring(5,7), 10);
    var d1 = parseInt(str1.substring(8,10), 10);
    
    var y2 = parseInt(str2.substring(0,4), 10);
    var m2 = parseInt(str2.substring(5,7), 10);
    var d2 = parseInt(str2.substring(8,10), 10);
    
    var date1 = new Date(); 
    date1.setFullYear(y1, m1, d1);
    var date2 = new Date();
    date2.setFullYear(y2, m2, d2);
    
    if(date1 > date2)
    {
		myAlert("The Check In date must be less than the Check Out date.", frmFechas.txtFechaDesde);
        return false;
    } 

    return true;
}

function validarFrmContacto()
{	
	if(frmContacto.txtNombre.value == "")
	{
		myAlert("First Name is Required.", frmContacto.txtNombre);
		return false;
	}
	
	if(frmContacto.txtApellido.value == "")
	{
		myAlert("Last Name is Required.", frmContacto.txtApellido);
		return false;
	}
	
	if(frmContacto.cboNacionalidad.selectedIndex == 0)
	{
		myAlert("Nationality is Required.", frmContacto.cboNacionalidad);
		return false;
	}
	
	if(frmContacto.txtEmail.value == "")
	{
		myAlert("E-Mail is Required.", frmContacto.txtEmail);
		return false;
	}
	
	if((frmContacto.txtFechaDesde.value != "") && (frmContacto.txtFechaHasta.value != ""))
    {
		var str1 = frmContacto.txtFechaDesde.value;
		var str2 = frmContacto.txtFechaHasta.value;
		
		var y1 = parseInt(str1.substring(0,4), 10);
		var m1 = parseInt(str1.substring(5,7), 10);
		var d1 = parseInt(str1.substring(8,10), 10);
		
		var y2 = parseInt(str2.substring(0,4), 10);
		var m2 = parseInt(str2.substring(5,7), 10);
		var d2 = parseInt(str2.substring(8,10), 10);
		
		var date1 = new Date(); 
		date1.setFullYear(y1, m1, d1);
		var date2 = new Date();
		date2.setFullYear(y2, m2, d2);
		
		if(date1 > date2)
		{
			myAlert("The Check In date must be less than the Check Out date.", frmContacto.txtFechaDesde);
			return false;
		}
	}
	
	if(frmContacto.cboConocio.selectedIndex == 6)
	{
		if(frmContacto.txtOtro.value == "")
		{
			myAlert("Other is Required.", frmContacto.txtOtro);
			return false;
		}
	}
	
	if(frmContacto.code.value == "")
	{	
		myAlert("Complete with the code of the image.", frmContacto.code);
		return false;
	}
	
	if(frmContacto.chkTerminos.checked == false)
	{
		myAlert("You must read and accept Terms and Conditions.", frmContacto.chkTerminos);
		return false;
	}

	return true;
}

function validarFrmBR()
{
	if(frm.chkVender.checked == false && frm.chkAlquilar.checked == false)
	{
		myAlert("Please select Apartments for Sale or/and Rent.", frm.chkVender);
		return false;
	}
	
    if(frm.txtFechaDesde.value == "" && frm.txtFechaHasta.value != "")
    {
		myAlert("Please enter the date of Check In.", frm.txtFechaDesde);
        return false;
    }
    if(frm.txtFechaHasta.value == "" && frm.txtFechaDesde.value != "")
    {
		myAlert("Please enter the date of Check Out.", frm.txtFechaHasta);
        return false;
    }
	
	if(frm.txtFechaHasta.value != "" && frm.txtFechaDesde.value != "")
    {
		var str1 = frm.txtFechaDesde.value;
		var str2 = frm.txtFechaHasta.value;
		
		var y1 = parseInt(str1.substring(0,4), 10);
		var m1 = parseInt(str1.substring(5,7), 10);
		var d1 = parseInt(str1.substring(8,10), 10);
		
		var y2 = parseInt(str2.substring(0,4), 10);
		var m2 = parseInt(str2.substring(5,7), 10);
		var d2 = parseInt(str2.substring(8,10), 10);
		
		var date1 = new Date(); 
		date1.setFullYear(y1, m1, d1);
		var date2 = new Date();
		date2.setFullYear(y2, m2, d2);
		
		if(date1 > date2)
		{
			myAlert("The Check In date must be less than the Check Out date.", frm.txtFechaDesde);
			return false;
		} 
	}
	
	if(frm.txtPrecioDesde.value != "" && frm.txtPrecioHasta.value != "")
	{
		if(!validarDecimalPositivo(frm.txtPrecioDesde.value))
		{
			myAlert("Price From must be a Number.", frm.txtPrecioDesde);
			return false;
		}
		if(!validarDecimalPositivo(frm.txtPrecioHasta.value))
		{
			myAlert("Price To must be a Number.", frm.txtPrecioHasta);
			return false;
		}
		if(parseFloat(frm.txtPrecioDesde.value) > parseFloat(frm.txtPrecioHasta.value))
		{
			myAlert("Price From must be less than the Price To.", frm.txtPrecioDesde);
			return false;
		}
	}

    return true;
}

function validarFrmSignUp(opcion)
{
	if (frm.username.value == "")
	{
		myAlert("Username is Required.", frm.username);
		return false;
	}
	if(opcion == 1 || opcion == 2)
	{
		if (frm.password.value == "")
		{
			myAlert("Password is Required.", frm.password);
			return false;
		}
		if (frm.repitaPassword.value == "")
		{
			myAlert("Re-enter Password is Required.", frm.repitaPassword);
			return false;
		}
	}
	if(frm.password.value != frm.repitaPassword.value)
	{
		myAlert("The Passwords provided do not match.", frm.password);
		return false;
	}
	if(frm.nombre.value == "")
	{
		myAlert("First Name is Required.", frm.nombre);
		return false;
	}
	if(frm.apellido.value == "")
	{
		myAlert("Last Name is Required.", frm.apellido);
		return false;
	}
	if(frm.email.value == "")
	{
		myAlert("E-Mail is Required.", frm.email);
		return false;
	}
	if(frm.code.value == "")
	{	
		myAlert("Complete with the code of the image.", frm.code);
		return false;
	}
	
	return true;
}

function validarFrmLogIn()
{
	if (frm.username.value == "")
	{
		myAlert("Username is Required.", frm.username);
		return false;
	}
	if (frm.password.value == "")
	{
		myAlert("Password is Required.", frm.password);
		return false;
	}
	if(frm.code.value == "")
	{	
		myAlert("Complete with the code of the image.", frm.code);
		return false;
	}
	
	return true;
}

function validarFrmResetPassword()
{
	if(frm.email.value == "")
	{
		myAlert("E-Mail is Required.", frm.email);
		return false;
	}
	
	return true;
}

function showOther() 
{ 
	var val = frmContacto.cboConocio.options[frmContacto.cboConocio.selectedIndex].value; 
	
	if (val == "Other")
	{
		document.getElementById("lblOtro").style.display = 'block';
		document.getElementById("txtOtro").style.display = 'block';
		frmContacto.txtOtro.focus();
	}
	else
	{
		document.getElementById("lblOtro").style.display = 'none';
		document.getElementById("txtOtro").style.display = 'none';
	}
} 
