/*
* Shows current character count for textarea filed and limits total number of 
* characters that can be entered into textarea field. 
*
*	limitFiled - source textarea filed that we are monitoring
*	limitCounterId  - the id of the <div> that will contain message "You have X characters left"
*	maxChars - maximum number of characters allowed
*
*/
function limitText( limitField, limitCounterId, maxChars ) 
{		
	var counterDiv = document.getElementById(limitCounterId);//.innerHTML = innerHtml;
	if (limitField.value.length > maxChars)
	{
		limitField.value = limitField.value.substring(0, maxChars);
		counterDiv.innerHTML = "0";
	}
	else 
	{
		counterDiv.innerHTML = ( maxChars - limitField.value.length ) ;
	}	
}


/*
* Following two functions show or hide a <div> area 
*	Parameter: divId - id of the target <div> area
*	Used for: - javascript that limits/counts number of characters per input field
*			  - javascript that automatically populates list of suggested cities after each keystroke
*				in city input filed on listingMandatorySection.jsp
*/
function showDiv( divId )
{
	if (document.layers) document.layers[divId].visibility="show";
	else document.getElementById(divId).style.visibility="visible";
}

function hideDiv( divId )
{
	if (document.layers) document.layers[divId].visibility="hide";
	else document.getElementById(divId).style.visibility="hidden";
}

function clearDiv( divId )
{
	if (document.layers)
	{ 
		document.layers[divId].innerHTML="";
	}
	else
	{
		document.getElementById(divId).innerHTML="";
	}	 

}



// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function URLEncode(plaintext )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;

};

	

function TrimString(sInString) {
	sInString = sInString.replace( /^\s+/g, "" );// strip leading
	return sInString.replace( /\s+$/g, "" );// strip trailing

//	var ret = sInString.replace( /\s+/g, "" );// strip all whitespace

}

function validateDecimal(element)
{
	var test = element.value.replace( /[^0123456789\.]+/g, "");

	// eliminate second appearance of . (decimal point)
	var i = test.indexOf(".");
	var j = test.lastIndexOf(".");

	if ( i > -1 && j > i )
	{
		if ( j == test.length - 1)
		{
			test = test.substring(0, j);
		}
		else
		{			
			test = test.substring(0,j) + test.substring(j+1,test.length);
		}
	}
		
	if ( element.value != test)
	{
		element.value=test;
	}
}

function validateInteger(element)
{
	var test = element.value.replace( /[^0123456789]+/g, "");
	element.value=test;
}


function toUpperCase(element)
{
	var test = element.value.toUpperCase(); 
		
	if ( element.value != test)
	{
		element.value=test;
	}
}


//
// Used by onload event handler for <body> tag on listing form page1
//  - populates visible city field from hidden field
//
function setCity()
{
	var cityHiddenField=document.getElementById("cityHidden");
	var cityVisibleField=document.getElementById("city");
	if ( cityHiddenField )
	{
		cityVisibleField.value = cityHiddenField.value;
	}
//	clearDiv("autocomplete");
}

function submitForm(param)
{

	if ( document.forms[0].js_rooms_select )
	{
		saveRooms();
	}

	document.forms[0].elements["button"].value=param;
	document.forms[0].submit();
}

