/* Function to go back */
function go_back()
{
	window.history.back(-1);	
}
/* Function to submit page on currency select */
function sel_maincurrency(frm,val)
{	
	frm.action = window.location.href;
	frm.submit();
}
/* Function to submit page on recentproperty clear all */
function sel_recentpropclearll(frm)
{	
	if (confirm('Are you sure you want to clear the list'))
	{
		frm.action = window.location.href;
		frm.submit();
	}
}
/* Function to jumb using main category*/
function sel_maincategory(val)
{	
	window.location = val;
}

/* Function to show the state based on selected country */
function showsearchstate(obj,cid)
{
	arrval = eval('countryval'+cid);
	arrkey = eval('countrykey'+cid);
	
	for(i=obj.options.length-1;i>0;i--)
	{
		 obj.remove(i);
	}
	for(i=0;i<arrkey.length;i++)
	{
		var lgth = obj.options.length;
		obj.options[lgth]= new Option(arrval[i],arrkey[i]);
	}
}

function showcustomerstate(obj,cid)
{
	arrval = eval('countryval'+cid);
	arrkey = eval('countrykey'+cid);
	
	for(i=obj.options.length-1;i>0;i--)
	{
		 obj.remove(i);
	}
	for(i=0;i<arrkey.length;i++)
	{
		var lgth = obj.options.length;
		obj.options[lgth]= new Option(arrval[i],arrkey[i]);
	}
	var lgth = obj.options.length;
	obj.options[lgth]= new Option('- Other -',0);
}

function validate_login(frm)
{
	fieldRequired = Array('txt_mainuser','txt_mainpass');
	fieldDescription = Array('Username','Password');
	fieldEmail = Array();
	fieldConfirm = Array();
	fieldConfirmDesc  = Array();
	fieldNumeric = Array();
	if(Validate_Form_Objects(frm,fieldRequired,fieldDescription,fieldEmail,fieldConfirm,fieldConfirmDesc,fieldNumeric))
	{
		show_processingdiv();	
		return true;
	}
	else
		return false;
}
function validate_sendpass(frm)
{
	fieldRequired = Array('txt_mainuser');
	fieldDescription = Array('Username');
	fieldEmail = Array();
	fieldConfirm = Array();
	fieldConfirmDesc  = Array();
	fieldNumeric = Array();
	if(Validate_Form_Objects(frm,fieldRequired,fieldDescription,fieldEmail,fieldConfirm,fieldConfirmDesc,fieldNumeric))
	{
		show_processingdiv();	
		return true;
	}
	else
		return false;
}

/* Function to validate the newsletter form*/
function validate_newsform(frm)
{
	var alerts ='';
	var atleastone = false;
	src_loc = document.getElementById('cbo_newsgroup[]');
	for (i=0;i<src_loc.length;i++)
	{
		if (src_loc.options[i].selected)
			atleastone = true;
	}
	if (atleastone==false)
	{
		alerts = ' -- Newsletter Group ';
	}
	
	if (frm.txt_newsname.value=='')
	{
		alerts += '\n -- Missing Name ';
	}
	if (!isValidEmail(frm.txt_newsemail.value))
	{
		alerts += '\n -- Invalid Email ';
	}
	if(alerts!='')
	{
		alert('Error! \n'+alerts);
		return false;
	}	
	show_processingdiv();
	return true;
}
function validate_regcustomer(frm)
{
	if(fn_dynamic) {
		if(!fn_dynamic())
			return false;
	}
	fieldRequired = Array('txt_custfname','txt_custsurname','txt_custaddress','txt_custpostcode','txt_custemail','txt_custpassword','txt_custconfirm');
	fieldDescription = Array('First name','Surname','Address','Post code','Email id','Password','Confirm Password');
	fieldEmail = Array();
	fieldConfirm = Array('txt_custpassword','txt_custconfirm');
	fieldConfirmDesc  = Array('Password','Confirm Password');
	fieldNumeric = Array();
	if(Validate_Form_Objects(frm,fieldRequired,fieldDescription,fieldEmail,fieldConfirm,fieldConfirmDesc,fieldNumeric))
	{
		if(frm.cbo_custcountry.value==0)
		{
			alert('Please select country');
			frm.cbo_custcountry.focus();
			return false;
		}
		if(frm.cbo_custstate.value==-1)
		{
			alert('Please select state');
			frm.cbo_custstate.focus();
			return false;
		}
		if (!isValidEmail(frm.txt_custemail.value))
		{
			alert('Invalid Email id');
			frm.txt_custemail.focus();
			return false;
		}
		show_processingdiv();
		return true;
	}
	else
	{
		return false;
	}
}
function validate_investors(frm)
{
	if(fn_dynamic) {
		if(!fn_dynamic())
			return false;
	}
	fieldRequired = Array('txt_investfname','txt_investlastname','txt_invest_hno','txt_invest_street','txt_invest_city','txt_investpostcode','txt_investphone','txt_investemail');
	fieldDescription = Array('First name','Lastname','House Number','Street','City','Post Code','Phone No','Email');
	fieldEmail = Array();
	fieldConfirm = Array();
	fieldConfirmDesc  = Array();
	fieldNumeric = Array();
	if(Validate_Form_Objects(frm,fieldRequired,fieldDescription,fieldEmail,fieldConfirm,fieldConfirmDesc,fieldNumeric))
	{
		if(frm.cbo_investcountry.value==0)
		{
			alert('Please select country');
			frm.cbo_investcountry.focus();
			return false;
		}
		if(frm.cbo_investstate.value==-1)
		{
			alert('Please select state');
			frm.cbo_investstate.focus();
			return false;
		}
		if (!isValidEmail(frm.txt_investemail.value))
		{
			alert('Invalid Email id');
			frm.txt_investemail.focus();
			return false;
		}
		show_processingdiv();
		return true;
	}
	else
	{
		return false;
	}
}
function validate_editprofile(frm)
{
	fieldRequired = Array('txt_custfname','txt_custsurname','txt_custaddress','txt_custpostcode');
	fieldDescription = Array('First name','Surname','Address','Post code');
	fieldEmail = Array();
	if(frm.txt_custpassword.value!='' || frm.txt_custconfirm.value !='')
	{
		fieldConfirm = Array('txt_custpassword','txt_custconfirm');
		fieldConfirmDesc  = Array('Password','Confirm Password');
		var cnt = fieldRequired.length;
		fieldRequired[cnt] 		= 'txt_custpassword';
		fieldDescription[cnt] 	= 'Password';
		cnt++;
		fieldRequired[cnt] 		= 'txt_custconfirm';
		fieldDescription[cnt] 	= 'Password Confirmation';
	}
	else
	{
		fieldConfirm = Array();
		fieldConfirmDesc  = Array();
	}
	fieldNumeric = Array();
	if(Validate_Form_Objects(frm,fieldRequired,fieldDescription,fieldEmail,fieldConfirm,fieldConfirmDesc,fieldNumeric))
	{
		if(frm.cbo_custcountry.value==0)
		{
			alert('Please select country');
			frm.cbo_custcountry.focus();
			return false;
		}
		if(frm.cbo_custstate.value==-1)
		{
			alert('Please select state');
			frm.cbo_custstate.focus();
			return false;
		}
		show_processingdiv();
		return true;
	}
	else
	{
		return false;
	}
}
function emailafriend_validate(frm)
{
	fieldRequired = Array('txt_friendname','txt_friendemail','txt_yourname','txt_youremail');
	fieldDescription = Array('Friend\'s name','Friend\'s Email','Your Name','Your Email Address');
	fieldEmail = Array('txt_friendemail','txt_youremail');
	fieldConfirm = Array();
	fieldConfirmDesc  = Array();
	fieldNumeric = Array();
	if(Validate_Form_Objects(frm,fieldRequired,fieldDescription,fieldEmail,fieldConfirm,fieldConfirmDesc,fieldNumeric))
	{
		show_processingdiv();	
		return true;
	}
	else
		return false;
	
}
function validate_enquirysend(frm)
{
	if(fn_dynamic) {
		if(!fn_dynamic())
			return false;
	}
	fieldRequired = Array('txt_enqfname','txt_enqsurname','txt_enqaddress','txt_enqpostcode','txt_enqphone','txt_enqemail');
	fieldDescription = Array('Firstname','Surname','Address','Postcode','Phone','Email');
	fieldEmail = Array('txt_enqemail');
	fieldConfirm = Array();
	fieldConfirmDesc  = Array();
	fieldNumeric = Array();
	if(Validate_Form_Objects(frm,fieldRequired,fieldDescription,fieldEmail,fieldConfirm,fieldConfirmDesc,fieldNumeric))
	{
		if(frm.cbo_enqcountry.value==0)
		{
			alert('Please select country');
			return false;
			
		}
		if(frm.cbo_enqstate.value==-1)
		{
			alert('Please select state. \nIf your state is not listed then select "Other"');
			return false;
			
		}
		show_processingdiv();
		return true;
	}
	else
		return false;
}
function validate_customer_propertysubmit(frm,src_loc)
{
	var atleastone = false;
	fieldRequired = Array('txt_subpropertynumber','txt_subtitle','txt_subshort');
	fieldDescription = Array('Property Number','Title','Short Description');
	fieldEmail = Array();
	fieldConfirm = Array();
	fieldConfirmDesc  = Array();
	fieldNumeric = Array();
	var ii =0;
	var reqcnt = fieldRequired.length;
	for(i=0;i<frm.elements.length;i++)
	{
		if(frm.elements[i].name.substr(0,12)=='txt_subprice' || frm.elements[i].name.substr(0,11) == 'txt_sqprice' || frm.elements[i].name.substr(0,12) == 'txt_endprice')
		{
			fieldNumeric[ii] = frm.elements[i].name;
			ii++;
			fieldRequired[reqcnt] = frm.elements[i].name;
			fieldDescription[reqcnt] = 'Enter Price';
			reqcnt++;
		}
	}	
	/*fieldNumeric[ii] = 'txt_subarea';
	if(frm.txt_subbedrooms.value!='')
	{
		ii++;
		fieldNumeric[ii] = 'txt_subbedrooms';
	}
	if(frm.txt_subbathrooms.value!='')
	{
		ii++;
		fieldNumeric[ii] = 'txt_subbathrooms';
	}
	if(frm.txt_subyear.value!='')
	{
		ii++;
		fieldNumeric[ii] = 'txt_subyear';
	}*/
	if(Validate_Form_Objects(frm,fieldRequired,fieldDescription,fieldEmail,fieldConfirm,fieldConfirmDesc,fieldNumeric))
	{
		if(frm.cbo_subcountry.value==0)
		{
			alert("Select Country");
			return false;
		}
		if(frm.cbo_substate.value==-1)
		{
			alert("Select State");
			return false;
		}
		for (i=0;i<src_loc.length;i++)
		{
			if (src_loc.options[i].selected)
				atleastone = true;
		}
		if (atleastone==false)
		{
			alert('Select the category for the property');
			return false;
		}
		show_processingdiv();
		return true;
	}
	else
	{
		return false;
	}
}
function open_realtime(propid,hostname,themename,siteid,curr)
{
	var loc ='http://'+hostname+'/themes/' + themename + '/html/property_checkavailability.php?pid=' + propid + '&siteid='+ siteid+'&curr='+curr;
	win_name = window.open(loc, 'realtime_availability', 'top=0, left=0, menubar=0, resizable=yes, scrollbars=yes, toolbar=0,width=995,height=550');
	win_name.focus();
}
function open_resale(hostname,themename)
{
	var loc ='http://'+hostname+'/themes/' + themename + '/html/resale_property_list.php';	
	win_name = window.open(loc, 'realsale_property', 'top=0, left=0, menubar=0, resizable=yes, scrollbars=yes, toolbar=0,width=995,height=550');
	win_name.focus();
}
function open_play(playid)
{
	win_name = window.open('../play.php?id='+playid, 'Play', 'top=0, left=0, menubar=0, resizable=yes, scrollbars=yes, toolbar=0,width=250,height=275');
	win_name.focus();
}
/* Function to delete item from enquiry cart*/
function delete_enquirycart()
{
	if(confirm('Are you sure you want to remove selected enquiry from the list?'))
		return true;
	else
		return false;
}
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
/* Function to change the bg on the right of top flash */
function changeBgColor(newBgColor) {
document.getElementById('toptable_td').className = newBgColor;
}
/* Function to show the processing diablog*/
function show_processingdiv()
{
	document.getElementById('processing_div').style.left 		= (screen.width-390)/2;
	document.getElementById('processing_div').style.top 		= (screen.height-100)/2;
	document.getElementById('processing_div').style.display		= '';
	window.scroll (0,0);
}
function showImagePopup(fname,hostname,themename)
{
	window.open( "http://"+hostname+"/themes/"+themename+"/html/popup_img.php?"+fname, "","resizable=1,HEIGHT=200,WIDTH=200,SCROLLBARS=yes");
}
function showfloorplans(obj,hostname,themename)
{	
	if(obj.value!=0)
	{
		xx  =	window.open( "http://"+hostname+"/themes/"+themename+"/html/property_floorplan_popup.php?flrid="+obj.value, "floor_plan","resizable=1,HEIGHT=400,WIDTH=500,SCROLLBARS=yes");		
		xx.focus();
	}
}
function link_submit(tabid,detimgid,url,cat_anchor,showthumbnail)
{
	if (tabid=='')
		tabid = 0;
	if (detimgid=='')
		detimgid = 0;
	if (url!='')
	{
		if (tabid!='' && cat_anchor)
			url = url+'#'+tabid;
		document.frm_forcesubmit.action 			= url;
	}	
		
	document.frm_forcesubmit.det_image_id.value 	= detimgid;
	document.frm_forcesubmit.tab_id.value 			= tabid;
	document.frm_forcesubmit.show_thumbnail.value 	= showthumbnail;
	document.frm_forcesubmit.submit();
}
