/*
/ display contact us category tabs
*/
function	set_tab_display	(id)
{
//	alert('id = ' + id);
	var	e_type_obj	=	document.getElementById(id);
	var s_index = e_type_obj.selectedIndex;
	var selected_val = e_type_obj.options[s_index].value;

	hide_show_div(selected_val);
}

/*
/	Show the selected category and hide all the rest
*/
function	hide_show_div	(show_val)
{
//	alert('shoe val = ' + show_val);
	var	div_ids	=	new Array();
	div_ids['Rent_estate']	=	'rental';
	div_ids['Rent_for_holiday']	=	'holiday_rental';
	div_ids['Sale_estate']	=	'sale';
	div_ids['Buy_estate']	=	'buy';
	div_ids['none']	=	'none';

	for (var key in div_ids)
	{
		if (key == show_val)
		{
		//	alert('id = ' + div_ids[key]);
//			alert('key = ' + key);
			var	div_obj	= document.getElementById(div_ids[key]);
			div_obj.style.display='block';
		} else	{
			var	div_obj	= document.getElementById(div_ids[key]);
			div_obj.style.display='none';
		}
	}
}

function	check_email_address	(email)
{
//	alert('email: ' + email);
	var at="@";
	var dot=".";
	var lat=email.indexOf(at);
	var lemail=email.length;
	var ldot=email.indexOf(dot);
	if (email.indexOf(at)==-1){
	   alert("Invalid E-mail address");
	   return false;
	}

	if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lemail){
	   alert("Invalid E-mail address");
	   return false;
	}

	if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lemail){
		alert("Invalid E-mail address");
		return false;
	}

	 if (email.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail address");
		return false;
	 }

	 if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail address");
		return false;
	 }

	 if (email.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail address");
		return false;
	 }
	
	 if (email.indexOf(" ")!=-1){
		alert("Invalid E-mail address");
		return false;
	 }

	return true;
}

/*
*/
function	checkForm	()
{
	// transaction types for this contact form
	var	div_ids	=	new Array();
	div_ids['Rent_estate']	=	'rental';
	div_ids['Rent_for_holiday']	=	'holiday_rental';
	div_ids['Sale_estate']	=	'sale';
	div_ids['Buy_estate']	=	'buy';
	div_ids['none']	=	'none';

	// Getting the contact us type selected by the customer (Sell,Buy,...)
	var con_type_obj	=	document.getElementById('contact_type');
	var s_index = con_type_obj.selectedIndex;
	var selected_val = con_type_obj.options[s_index].value;
	var arr_data_name	=	div_ids[selected_val];

//alert('selected val = ' + selected_val);
	// Check that "from date" is smaller then "to date"
	if (selected_val == 'Rent_for_holiday' )
	{
		var from_date	=	document.getElementById('hr_from_date');
		var to_date		=	document.getElementById('hr_to_date');
		var	from_date_val	=	getDateFromFormat(from_date.value,'dd/MM/yyyy');
		var	to_date_val	=	getDateFromFormat(to_date.value,'dd/MM/yyyy');
//		alert(from_date_val + ' -- ' + to_date_val);
		if (from_date_val > to_date_val)
		{
			alert('Invalid Duration Period, please reselect the dates');
			return false;
		}
//		alert(from_date_val + ' -- ' + to_date_val);
	} else if (selected_val == 'Rent_estate') {
		var from_date	=	document.getElementById('r_from_date');
		var to_date		=	document.getElementById('r_to_date');
		var	from_date_val	=	getDateFromFormat(from_date.value,'dd/MM/yyyy');
		var	to_date_val	=	getDateFromFormat(to_date.value,'dd/MM/yyyy');
//		alert(from_date_val + ' -- ' + to_date_val);
		if (from_date_val > to_date_val)
		{
			alert('Invalid Duration Period, please reselect the dates');
			return false;
		}
	}

	var field_name;
	var	field_val;
	var	field_id;
	var	phones;	// for checking that at least one phone number was filled
// Assembling the selected data array string
	var re	=	new RegExp(arr_data_name,"i");
//	alert('re = ' + re);
	for(i=0; i<document.forms['contact_us'].elements.length; i++)
	{
		field_name	=	document.forms['contact_us'].elements[i].name;
		if (re.test(field_name))
		{
//			alert('field name = ' + field_name);
			field_val	=	document.forms['contact_us'].elements[i].value;
			send_params	+=	field_name + '=' + field_val + '&';
		}
	}

// Assembling the personal details array string
	re	=	new RegExp("personal","i");
	for(i=0; i<document.forms['contact_us'].elements.length; i++)
	{
		field_name	=	document.forms['contact_us'].elements[i].name;
		if (re.test(field_name))
		{
			field_val	=	document.forms['contact_us'].elements[i].value;
			field_id	=	document.forms['contact_us'].elements[i].id;
//			alert('id = ' + field_id);
			send_params	+=	field_name + '=' + field_val + '&';
		// Checking values for requeried fields
			switch	(field_id)
			{
				case	"first_name":
				case	"last_name":
				case	"email":
					if (field_val	==	'')
					{
						alert('Name and Email are required fields !!! Please fill them up.');
						return false;
					}
					// For confirming email address
					if (field_id == 'email')
					{
						var email	=	field_val;
					}
				break;
				case	"o_phone":
				case	"h_phone":
				case	"m_phone":
					phones	+=	field_val;
				break;
				case	"contact_type":
					if (field_val == 'none')
					{
						alert('You must select contact type !!!');
						return	false;
					}
				break;
				case	"confirm_email":
						var confirm_email	=	field_val;
				break;
			}
		}
		if (phones == '')
		{
			alert('You must fill at least one phone number !!!');
			return false;
		}

	}
	// Check that Email and confirm email are matching and that email address is in right format
	if (email != confirm_email)
	{
		alert('Email addresses do not match, please re-enter your email address.');
		return false;
	}
	// Check for correct email address format
	if (check_email_address(email)==false)
	{
		return false;
	}

//	alert('send params = ' + send_params);
	//send_params	+=	"type=add_cust_from_web&trans_type=" + arr_data_name;
	// Assembling the array data accurding to selected contact value

//	send_server_request('GET');

	return true;
}

