var loading_msg="Sending...<img src='images/loading.gif' style='vertical-align:middle'/>";
//trim functions
function trim(str) {
	return ltrim(rtrim(str));
}
function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}
//ends trim functions
//email validation

function checkEmail(email) {

  var str = new String(email);
  var isOK = true;
  rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
  if( rExp.test(str) )
    isOK = false;
  if( str.indexOf('.') == -1 || str.indexOf('@') == -1 )
    isOK = false;
  if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
    isOK = false;
  if( str.slice(0,str.indexOf('@')).length < 1 )
    isOK = false;
  if( str.slice(str.indexOf('@')+1,str.lastIndexOf('.')).length < 1 )
    isOK = false;

  if( !isOK )
    return false;

  return isOK;
}

function clear_field(obj_id,default_val)
{
obj=document.getElementById(obj_id);
len=trim(obj.value);
if(len.length)
if(default_val==obj.value)
document.getElementById(obj_id).value='';

}


function check_value(obj_id,val)
{
obj=document.getElementById(obj_id);
len=trim(obj.value);
if(!len.length)
obj.value=val;
}

function release(id_textbox,sts_id)
{
  elmnt=document.getElementById(id_textbox);
  if(elmnt.value!='')
  elmnt.style.border='1px solid #CCCCCC';
 if(document.getElementById(sts_id))
  document.getElementById(sts_id).innerHTML='';   
}


function validate_submit()
{
document.getElementById('contact_number').style.border='1px solid #CCCCCC';		
g_fname=trim(document.getElementById('fname').value);
g_email=trim(document.getElementById('email').value);
g_country=trim(document.getElementById('country').value);
g_tel=trim(document.getElementById('contact_number').value);

var str;
var msg='Please enter ';
sts=0;
str = g_fname;
if(str=='' || str==null){
msg += 'Name, ';
sts++;
document.getElementById('fname').style.border='1px solid #FF3300';
}
str = g_email;
if(str=='' || str==null || checkEmail(str)==false){
msg += 'Email, ';
sts++;
document.getElementById('email').style.border='1px solid #FF3300';
}

str = g_tel;
if(str=='' || str==null || checkInternationalPhone(str)==false){
msg += 'Contact number, ';
sts++;
document.getElementById('contact_number').style.border='1px solid #FF3300';
}

str = g_country;
if(str=='' || str==null){
msg += 'Country of residence, ';
sts++;
document.getElementById('country').style.border='1px solid #FF3300';
}



msg= msg.substring(0, msg.length-2);
if(sts==0)
{
document.getElementById('status_msg').innerHTML=loading_msg;
document.buyers_guide.submit()
}
else
document.getElementById('status_msg').innerHTML=msg;

}



function validate_investment_report()
{
document.getElementById('contact_number').style.border='1px solid #CCCCCC';		
g_fname=trim(document.getElementById('fname').value);
g_email=trim(document.getElementById('email').value);
g_country=trim(document.getElementById('country').value);
g_tel=trim(document.getElementById('contact_number').value);



var str;
var msg='Please enter ';
sts=0;
str = g_fname;
if(str=='' || str==null){
msg += 'Name, ';
sts++;
document.getElementById('fname').style.border='1px solid #FF3300';
}
str = g_email;
if(str=='' || str==null || checkEmail(str)==false){
msg += 'Email, ';
sts++;
document.getElementById('email').style.border='1px solid #FF3300';
}

str = g_tel;
if(str=='' || str==null || checkInternationalPhone(str)==false){
msg += 'Contact number, ';
sts++;
document.getElementById('contact_number').style.border='1px solid #FF3300';
}


str = g_country;
if(str=='' || str==null){
msg += 'Country of residence, ';
sts++;
document.getElementById('country').style.border='1px solid #FF3300';
}


msg= msg.substring(0, msg.length-2);
if(sts==0)
{
document.getElementById('status_msg').innerHTML=loading_msg;	
document.investment_report_frm.submit()
}
else
document.getElementById('status_msg').innerHTML=msg;

}



function validate_submit_enquiry_left()
{
document.getElementById('enq_tel').style.border='1px solid #CCCCCC';	

g_fname=trim(document.getElementById('enq_fname').value);
g_email=trim(document.getElementById('enq_email').value);

g_tel=trim(document.getElementById('enq_tel').value);

var str;
var msg='Please enter ';
sts=0;
str = g_fname;
if(str=='' || str==null){
msg += 'Name, ';
sts++;
document.getElementById('enq_fname').style.border='1px solid #FF3300';
}

str = g_tel;
if(str=='' || str==null || checkInternationalPhone(str)==false){
msg += 'Telephone number, ';
sts++;
document.getElementById('enq_tel').style.border='1px solid #FF3300';
}


str = g_email;
if(str=='' || str==null || checkEmail(str)==false){
msg += 'Email, ';
sts++;
document.getElementById('enq_email').style.border='1px solid #FF3300';
}


msg= msg.substring(0, msg.length-2);
if(sts==0)
{
document.getElementById('status_msg_enq_left').innerHTML=loading_msg;
document.enquiry.submit()
}
else
document.getElementById('status_msg_enq_left').innerHTML=msg;
}



function validate_submit_list_property()
{
document.getElementById('contact_number').style.border='1px solid #CCCCCC';	
fname=trim(document.getElementById('fname').value);
email=trim(document.getElementById('email').value);
country=trim(document.getElementById('country').value);
g_tel=trim(document.getElementById('contact_number').value);


var str;
var msg='Please enter ';
sts=0;
str = fname;
if(str=='' || str==null){
msg += 'Name, ';
sts++;
document.getElementById('fname').style.border='1px solid #FF3300';
}
str = email;
if(str=='' || str==null || checkEmail(str)==false){
msg += 'Email, ';
sts++;
document.getElementById('email').style.border='1px solid #FF3300';
}

str = country;
if(str=='' || str==null){
msg += 'Country of residence, ';
sts++;
document.getElementById('country').style.border='1px solid #FF3300';
}

str = g_tel;
if(str=='' || str==null || checkInternationalPhone(str)==false){
msg += 'Contact number, ';
sts++;
document.getElementById('contact_number').style.border='1px solid #FF3300';
}


p_owned=0;
for (var i=0; i < document.list_property_frm.property_owned.length; i++)
if (document.list_property_frm.property_owned[i].checked)
p_owned=1;

if(p_owned==0){
msg += 'Property owned, ';
sts++;
}

available=0;
for (var i=0; i < document.list_property_frm.available_for.length; i++)
if (document.list_property_frm.available_for[i].checked)
available=1;

if(available==0){
msg += 'Available for, ';
sts++;
}

msg= msg.substring(0, msg.length-2);
if(sts==0)
{
document.getElementById('status_msg').innerHTML=loading_msg;	
document.list_property_frm.submit()
}
else
document.getElementById('status_msg').innerHTML=msg;

}

function validate_news_letter_req()
{
email_news=trim(document.getElementById('news_signup_email').value);
email_news_contact=document.getElementById('news_signup_contact').value;

var str;
var msg='Please enter ';
sts=0;

str = email_news;
if(str=='' || str==null || checkEmail(str)==false){
msg += 'Email, ';
sts++;
document.getElementById('news_signup_email').style.border='1px solid #FF3300';
}


str = email_news_contact;
if(str=='' || str==null || checkInternationalPhone(str)==false){
msg += 'Contact number, ';
sts++;
document.getElementById('news_signup_contact').style.border='1px solid #FF3300';
}

msg= msg.substring(0, msg.length-2);


if(sts==0)
{
document.getElementById('status_msg_news').innerHTML=loading_msg;		
document.news_letter_frm.submit()
}
else
document.getElementById('status_msg_news').innerHTML=msg;

}


function validate_contact_us()
{
document.getElementById('c_tel').style.border='1px solid #CCCCCC';		
g_fname=trim(document.getElementById('c_fname').value);
g_email=trim(document.getElementById('c_email').value);
g_country=trim(document.getElementById('c_country').value);
g_tel=trim(document.getElementById('c_tel').value);


var str;
var msg='Please enter ';
sts=0;
str = g_fname;
if(str=='' || str==null){
msg += 'Name, ';
sts++;
document.getElementById('c_fname').style.border='1px solid #FF3300';
}
str = g_email;
if(str=='' || str==null || checkEmail(str)==false){
msg += 'Email, ';
sts++;
document.getElementById('c_email').style.border='1px solid #FF3300';
}

str = g_tel;
if(str=='' || str==null || checkInternationalPhone(str)==false){
msg += 'Contact number, ';
sts++;
document.getElementById('c_tel').style.border='1px solid #FF3300';
}


str = g_country;
if(str=='' || str==null){
msg += 'Country, ';
sts++;
document.getElementById('c_country').style.border='1px solid #FF3300';
}


msg= msg.substring(0, msg.length-2);
if(sts==0)
{
document.getElementById('msg_contact_us_left').innerHTML=loading_msg;		
document.contact_us_frm.submit()
}
else
document.getElementById('msg_contact_us_left').innerHTML=msg;
}

function validate_callback()
{
g_fname=trim(document.getElementById('r_fname').value);
g_email=trim(document.getElementById('r_email').value);
g_tel=trim(document.getElementById('r_tel').value);
g_country=trim(document.getElementById('r_country').value);

var str;
var msg='Please enter ';
sts=0;
str = g_fname;
if(str=='' || str==null){
msg += 'Name, ';
sts++;
document.getElementById('r_fname').style.border='1px solid #FF3300';
}

str = g_tel;
if(str=='' || str==null || checkInternationalPhone(str)==false){
msg += 'Telephone, ';
sts++;
document.getElementById('r_tel').style.border='1px solid #FF3300';
}

str = g_email;
if(str=='' || str==null || checkEmail(str)==false){
msg += 'Email, ';
sts++;
document.getElementById('r_email').style.border='1px solid #FF3300';
}


str = g_country;
if(str=='' || str==null){
msg += 'Country, ';
sts++;
document.getElementById('r_country').style.border='1px solid #FF3300';
}


msg= msg.substring(0, msg.length-2);
if(sts==0)
{
document.getElementById('msg_contact_us_right').innerHTML=loading_msg;		
document.callback_frm.submit()
}
else
document.getElementById('msg_contact_us_right').innerHTML=msg;
}


// phone number validations
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 5;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim_phone(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){

if(strPhone==0)
return false;


t=0;
test_ph=strPhone.split('');
for(i=0; i<test_ph.length;i++)
{
if(test_ph[i]!=' ' && test_ph[i]!='+' && test_ph[i]!='-')
t=t+parseInt(test_ph[i]);
}
if(t<=0)
return false;


var bracket=3
strPhone=trim_phone(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



// phone num validation ends here
