/**
* @version $Id: functions.php 3112 2007-05-06 03:47:46Z harleywu $
* @package Listings SG
* @copyright Copyright (C) 2007 Modulo Pte Ltd. All rights reserved.
*/
function checkLogin () {
	var email = document.loginForm.email.value;
	var passwd = document.loginForm.passwd.value;
	if (email.length == 0 || passwd.length == 0) {
		alert("Signup for a free account now!");
		window.location = "http://www.listings.sg/profile/registers";
		return false;
	}
	return true;
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function validateSearch() {
	var searchString = document.searchForm.searchword.value;
	if ((searchString == "search...") || (searchString.length = 0))  {
		alert("Please enter search details.");
		return false;
	} else {
		if (searchString.length < 4) {
			alert("Search details must be longer than 3.");
			return false;
		} else {
			return true;
		}
	}
}
function checkLocationSearch () {
	var searchString = document.locationSearch.s.value;
	if ((searchString == "Search for location...") || (searchString.length = 0))  {
		alert("Please enter search details.");
		return false;
	} else {
		if (searchString.length < 4) {
			alert("Search details must be longer than 3.");
			return false;
		} else {
			return true;
		}
	}
}

function open_window(url) {
	cwin = window.open(url,"attach","width=350,height=400,toolbar=no,resizable=yes");
}
function checkListingPackageSelection () {
	var packageSelected = false;
	for (counter = 0; counter < document.addListingPackages.item_number.length; counter++) {
		if (document.addListingPackages.item_number[counter].checked) {
			packageSelected = true;
		}
	}
	if (!packageSelected) {
		if (document.addListingPackages.item_number.checked) {
			return true;
		} else {
			alert("Please select a listing package to continue.")
			return false;
		}
	} else {
		return true;
	}
}
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}
function ptoutput(theText) {
	document.write(theText);
}
function cbConnSubmReq() {
	cClick();
	document.connOverForm.submit();
}
function confirmSubmit() {
	if (confirm("Are you sure you want to remove this connection?"))
	return true ;
	else
	return false ;
}

function checkChosen () {
	if (document.agencySelector.agencyname.options[0].selected) {
		alert("Please select an agency to continue");
		return false;
	} else {
		return true;
	}
}
function checkBloggerSelector () {
	if (document.agencyBloggerSelector.agent.options[0].selected) {
		alert("Please select an agent to continue");
		return false;
	} else {
		return true;
	}
}
function validateSelector () {
	var searchString = document.tranSearchSelector.s.value;
	if (searchString == "Enter Details here...") {
		alert("Please enter search details.");
		return false;
	} else {
		return true;
	}
}
function validateAgentSearch () {
	var searchString = document.agentSearch.s.value;
	var searchType = document.agentSearch.searchtype.value;
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (searchString == "Enter Details here...") {
		alert("Please enter search details.");
		return false;
	} else {
		if (searchString.length < 2) {
			alert("Search string too short, please input a longer term to search.");
			return false;
		} else {
			if (searchType == "mobilenumber") {
				if (searchString.length == 0) {
					alert("Please enter a contact number.");
					return false;
				}
				if (searchString.length <=7){
					alert("Please enter a valid contact number.");
					return false;
				}
				for (i = 0; i < searchString.length && blnResult == true; i++) {
					strChar = searchString.charAt(i);
					if (strValidChars.indexOf(strChar) == -1) {
						alert("Please check your contact number, it should only contain numbers.");
						return false;
					}
				}
			}
		}
	}


	return true;
}
function confirmDelete(text) {
	if (text) {
		var agree=confirm(text)
	} else {
		var agree=confirm("Are you sure you want to delete?");
	}
	if (agree) {
		return true;
	} else {
		return false;
	}
}
function validateAgentSelector ($value) {
	if ($value == '') {
		alert("Please select an agent to continue");
		return false;
	} else {
		document.agentSelector.submit();
	}
}
function validateAgencyListingsSearch () {
	var searchString = document.agencyListingSearch.s.value;
	if (searchString == "Enter Details here...") {
		alert("Please enter search details.");
		return false;
	} else {
		return true;
	}
}
function checkSearch () {
	if ((document.searchBlogForm.s.value == '') || (document.searchBlogForm.s.value == 'Please enter details')) {
		alert("Please enter search details");
		return false;
	} else {
		return true;
	}
}
function checkWallPost () {
	if (document.postWallComment.comment.value.length) {
		return true;
	} else {
		alert("You need to say something.");
		return false;
	}
}
// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() {
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// if running Internet Explorer
	if(window.ActiveXObject) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			xmlHttp = false;
		}
	} else {
		// if running Mozilla or other browsers
		try {
			xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlHttp = false;
		}
	}
	// return the created object or display an error message
	if (!xmlHttp) {
		alert("Error creating the XMLHttpRequest object.");
	} else {
		return xmlHttp;
	}
}
function profileAddress(userid) {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
		var baseAddr = null;
		if( document.getElementsByTagName ) {
			var elems = document.getElementsByTagName( 'base' );
			if( elems.length ) {
				baseAddr = elems[ 0 ].href;
			}
		}
		xmlHttp.open("GET", baseAddr + "profileAddress.php?user" + userid, true);
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				// status of 200 indicates the transaction completed successfully
				if (xmlHttp.status == 200) {
					xmlResponse = xmlHttp.responseText;
					print (xmlResponse);
				} else {
					alert("There was a problem accessing the server: " + xmlHttp.statusText);
				}
			}
		};
		xmlHttp.send(null);
	} else {
		setTimeout('profileAddress()', 5000);
	}
}
// make asynchronous HTTP request using the XMLHttpRequest object
function process() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
		var form = document.getElementById("class_search_form");
		var selectlist = document.getElementById("pclass");
		var pclass = selectlist.value;
		if (pclass == '') {
			alert ("Please select a property class to continue");
			return;
		}
		var baseAddr = null;
		if( document.getElementsByTagName ) {
			var elems = document.getElementsByTagName( 'base' );
			if( elems.length ) {
				baseAddr = elems[ 0 ].href;
			}
		}
		xmlHttp.open("GET", baseAddr + "rsearch.php?pclass[]=" + pclass, true);
		xmlHttp.onreadystatechange = function () {
			// move forward only if the transaction has completed
			if (xmlHttp.readyState == 4) {
				// status of 200 indicates the transaction completed successfully
				if (xmlHttp.status == 200) {
					xmlResponse = xmlHttp.responseText;
					document.getElementById("search_step2").innerHTML = xmlResponse;
				} else {
					alert("There was a problem accessing the server: " + xmlHttp.statusText);
				}
			}
		};
		xmlHttp.send(null);
	} else {
		setTimeout('process()', 5000);
	}
}
// executed automatically when a message is received from the server

function searchStep2() {
	var form = document.getElementById("class_search_form");
	var pclass = form.pclass.value;
	if (pclass == '') {
		alert ("Please select a property class to continue");
		return;
	}
	return form.submit();
}

function provideInstruction(videotype) {
	if (videotype == 0) {
		instructions = "Please select a video type";
	} else if (videotype == 1) {
		instructions = "<strong>Google Video Instructions</strong>:<br/>If the link on your browser displays http://video.google.com/videoplay?docid=-7456311858922843652<br/>then your video_id is <strong>-7456311858922843652</strong>, if there is a minus sign at the front, please include it.";
	} else if (videotype == 11) {
		instructions = "<strong>Youtube Video Instructions</strong>:<br/>If the link on your browser displays http://www.youtube.com/watch?v=BTBGylVE5MQ<br/>then your video_id is <strong>BTBGylVE5MQ</strong>.";
	} else if (videotype == 14) {
		instructions = "<strong>Metacafe Video Instructions</strong>:<br/>If the link on your browser displays http://www.metacafe.com/watch/50234/news_bloopers/<br/>then your video_id is <strong>50234/news_bloopers</strong>, please omit the trailing slash.";
	}
	document.getElementById("instructionSpace").innerHTML = instructions;
	document.getElementById("idSpace").innerHTML = '<table><tr><td><strong>Video ID</strong>:</td><td><input type="text" name="videoid" id="videoid" size=35"/> <input type="button" class="button" value="Load Video" onclick="processVideo()"/></td></tr></table>';

}
var cItemCount=0
var maxCompareItems=4
function checkMaxCompare (item) {
	if(item.checked) {
		cItemCount=cItemCount+1;
	} else {
		cItemCount=cItemCount-1;
	}
	if (cItemCount>maxCompareItems) {
		item.checked=false;
		cItemCount=cItemCount-1;
		alert("The limit for comparing listings is "+maxCompareItems+".\n If you would like to compare another item, please remove one or more items from your list.");
	}
}
function checkListingComparison () {
	if (cItemCount < 2 ) {
		alert("Please select at least 2 listings to compare");
		return false;
	} else {
		return true;
	}
}

var cListingPageItemCount=0
var maxListingPageCompareItems=3
function checkMaxCompareListingPage (item) {
	if(item.checked) {
		cListingPageItemCount=cListingPageItemCount+1;
	} else {
		cListingPageItemCount=cListingPageItemCount-1;
	}
	if (cListingPageItemCount>maxListingPageCompareItems) {
		item.checked=false;
		cListingPageItemCount=cListingPageItemCount-1;
		alert("The limit for comparing listings is "+maxListingPageCompareItems+".\n If you would like to compare another item, please remove one or more items from your list.");
	}
}
function expand(thistag,tag2,tag3) {
	styleObj = document.getElementById(thistag).style;
	if (styleObj.display=='none') {
		styleObj.display = '';
	} else {
		styleObj.display = 'none';
	}
	styleObj2 = document.getElementById(tag2).style;
	if (styleObj2.display=='none') {
		styleObj2.display = '';
	} else {
		styleObj2.display = 'none';
	}
	styleObj3 = document.getElementById(tag3).style;
	if (styleObj3.display=='none') {
		styleObj3.display = '';
	} else {
		styleObj3.display = 'none';
	}
}
function validEmailAddress(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	if (str.indexOf(" ")!=-1){
		return false
	}
	var trailingTLD = str.substring(ldot+1,lstr);
	if (trailingTLD.indexOf(dot)) {
		if (trailingTLD.indexOf(dot) + 1 ==  trailingTLD.length) {
			return false;
		}
		if (( trailingTLD.indexOf(dot) + 2) == trailingTLD.length) {
			// shortest tld is 2
			return false;
		}
	} else {
		if (trailingTLD.length < 2) {
			// shortest tld is 2
			return false;
		}
	}
	return true
}
function validateAgentQuickSearch() {
	var searchString = document.agentsearch.s.value;
	if (searchString.length == 0) {
		alert("Please enter search details.");
		return false;
	}
	if (searchString == "Enter Details here...") {
		alert("Please enter search details.");
		return false;
	} else {
		if (searchString.length < 2) {
			alert("Search string too short, please input a longer term to search.");
			return false;
		} else {
			return true;
		}
	}
}
function hideFlash(){
	var historicalPricesContainer = document.getElementById("historicalPricesContainer");
	if (historicalPricesContainer) {
		historicalPricesContainer.style.visibility = "hidden";
	}
	var flashObjects = document.getElementsByTagName("object");
	if (flashObjects.length) {
		for (i=0; i<flashObjects.length; i++) {
			flashObjects[i].style.visibility = "hidden";
		}
	}
	var flashEmbeds = document.getElementsByTagName("embeds");
	if (flashEmbeds.length) {
		for (i=0; i<flashEmbeds.length; i++) {
			flashEmbeds[i].style.visibility = "hidden";
		}
	}
}
function showFlash(){
	var historicalPricesContainer = document.getElementById("historicalPricesContainer");
	if (historicalPricesContainer) {
		historicalPricesContainer.style.visibility = "visible";
	}
	var flashObjects = document.getElementsByTagName("object");
	if (flashObjects.length) {
		for (i=0; i< flashObjects.length; i++) {
			flashObjects[i].style.visibility = "visible";
		}
	}
	var flashEmbeds = document.getElementsByTagName("embeds");
	if (flashEmbeds.length) {
		for (i=0; i<flashEmbeds.length; i++) {
			flashEmbeds[i].style.visibility = "visible";
		}
	}
}
function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}
function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}