ajax_response	= '';		// Global value to store the respose from Ajax so that it can be used in other functions

function SS_Photos(listingid, photoid, foreign_table) {
	var width	= 700;
	var height	= 630;
	var page	= "/photos.php?type=" + foreign_table + "&propertyid=" + listingid;
	if(photoid) page = page + "&intPhotoID=" + photoid;
	var popup	= window.open(page, "_photos", "scrollbars=no,width=" + width + ",height=" + height + ",top=" + (screen.height/2 - height/2) + ",left=" + (screen.width/2 - width/2));
	if(popup) popup.focus();
}

function PhotoChangeOver(img_src, listingid, photoid, foreign_table) {
	var objImage = document.getElementById("id_image_large");
	var img = '<img src="' + img_src + '" style="cursor:pointer; width: 431px; height: 309px;" onClick="SS_Photos(\'' + listingid + '\', \'' + photoid + '\', \'' + foreign_table + '\')">';
	objImage.innerHTML = img;
}

/**
 * Submits a form via Ajax and runs a success function if required
 *
 * @return					void
 * @author					Nigel Wells
 * @version					1.0.07.05.21
 * @param form				Form object - NOTE: This must be the object and not just the name of the form
 * @param function_name		The function that will be called in lib_ajaxrequest.php
 * @param success_function	If set then this function will be called after the Ajax request has been successful
							- The response from Ajax will be stored in a global virable "ajax_response" which can then be accessed from function_name()
 * @param boolAlert			If true then it will alert the response returned from Ajax - Can be useful for debuging
 */

function submitAjaxForm(form, function_name, success_function, boolAlert) {
	var strResponse = '';
	var status = false;
	AjaxRequest.submit(
		form
		,{
		  'url':'includes/lib_ajaxrequest.php?ajax_function=' + function_name
		  ,'onSuccess':function(req){ 
			  var status = true;
			  strResponse = req.responseText; 
			  if(boolAlert === true) alert(strResponse);
			  if(success_function != '') {
				ajax_response = strResponse;
				  success_function += '()';
				  setTimeout(success_function, 1);
			  }
		  }
		  ,'onError':function(req){ form.submit(); }
		}
	);
}

function refreshSearchResults() {
	var form_search = document.getElementById('id_form_search');

	submitAjaxForm(form_search, 'refreshSearchResults', 'loadSearchResults', false);
}

function loadSearchResults() {
	document.getElementById('id_search_results').innerHTML = ajax_response;
}

function checkTxtAgent(form) {
	var strErrors = '';
	var intErrors = 0;

	if(form.sms_from.value == '' || form.sms_from.value == 'Name') {
		strErrors += "Name\n";
		intErrors++;
	}
	if(form.sms_email.value == '' || form.sms_email.value == 'Email') {
		strErrors += "Email\n";
		intErrors++;
	} else if(!isValidEmail(form.sms_email.value)) {
		strErrors += "Email is invalid\n";
		intErrors++;
	}
	if(form.sms_phone.value == '' || form.sms_phone.value == 'Phone') {
		strErrors += "Phone\n";
		intErrors++;
	}
	if(form.sms_message.value == '' || form.sms_message.value == 'Message') {
		strErrors += "Message\n";
		intErrors++;
	}

	if(intErrors > 0) {
		alert("There are " + intErrors + " fields incomplete:\n\n" + strErrors);
		return false;
	} else {
		return true;
	}
}

function checkEnquire(form) {
	var strErrors = '';
	var intErrors = 0;

	if(form.name.value == '' || form.name.value == 'Name') {
		strErrors += "Name\n";
		intErrors++;
	}
	if(form.email.value == '' || form.email.value == 'Email') {
		strErrors += "Email\n";
		intErrors++;
	} else if(!isValidEmail(form.email.value)) {
		strErrors += "Email is invalid\n";
		intErrors++;
	}
	if(form.phone.value == '' || form.phone.value == 'Phone') {
		strErrors += "Phone\n";
		intErrors++;
	}
	if(form.comments.value == '' || form.comments.value == 'Comments') {
		strErrors += "Comments\n";
		intErrors++;
	}

	if(intErrors > 0) {
		alert("There are " + intErrors + " fields incomplete:\n\n" + strErrors);
		return false;
	} else {
		return true;
	}
}

function checkFriend(form) {
	var strErrors = '';
	var intErrors = 0;

	if(form.name.value == '' || form.name.value == 'Your name') {
		strErrors += "Your name\n";
		intErrors++;
	}
	if(form.friend_name.value == '' || form.friend_name.value == 'Friends name') {
		strErrors += "Friends name\n";
		intErrors++;
	}
	if(form.email.value == '' || form.email.value == 'Email') {
		strErrors += "Email\n";
		intErrors++;
	} 	
	if(form.comments.value == '' || form.comments.value == 'Message') {
		strErrors += "Message\n";
		intErrors++;
	}

	if(intErrors > 0) {
		alert("There are " + intErrors + " fields incomplete:\n\n" + strErrors);
		return false;
	} else {
		return true;
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function addMyProperty(propertyid) {
	var div = document.getElementById("id_addMyProperty");

	div.innerHTML = 'Saving';
	AjaxRequest.get(
	  {
		'url':'/includes/lib_ajaxrequest.php?ajax_function=addMyProperty&propertyid=' + propertyid
		,'onSuccess':function(req){ 
			var strResponse = req.responseText; 
			div.innerHTML = strResponse;
			}
	  }
	);	
}

function removeMyProperty(propertyid) {
	var div = document.getElementById("id_removeMyProperty_" + propertyid);
	AjaxRequest.get(
	  {
		'url':'/includes/lib_ajaxrequest.php?ajax_function=removeMyProperty&propertyid=' + propertyid
		,'onSuccess':function(req){ 
			var strResponse = req.responseText; 
			div.innerHTML = '&nbsp;';
			}
	  }
	);	
}

function checkAllBoxs(form, objCheckall) {
	var loop = form.elements.length;

	if(objCheckall.checked == true) {
	    for(i = 0; i < loop; i++) {		  
		    if(form.elements[i].type == "checkbox") {
				form.elements[i].checked = true;
			}
		}
	} else {
	    for(i = 0; i < loop; i++) {		  
		    if(form.elements[i].type == "checkbox") {
				form.elements[i].checked = false;
			}
		}
	}
}

//function to check valid email address
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
    if (strEmail.search(validRegExp) == -1) 
   {
      return false;
    } 
    return true; 
}


/* Mailinglist Functions */

function mailinglist_subscribe_validate() {
	if (document.getElementById("mailinglist_name").value == '' || document.getElementById("mailinglist_name").value == 'Name') {
		alert('You must enter your name');
	} else if (document.getElementById("mailinglist_email").value == '' || document.getElementById("mailinglist_email").value == 'Email') {
		alert('You must enter your email address');
	} else if (document.getElementById("mailinglist_group").value == '') {
		alert('You must select a preferred agent');
	} else {
		return true;
	}
	return false;
}

function mailinglist_unsubscribe_validate() {
	if (document.getElementById("mailinglist_email").value == '' || document.getElementById("mailinglist_email").value == 'Email') {
		alert('You must enter your email address');
	} else {
		return true;
	}
	return false;
}