String.prototype.trim = function() {
  var str = this;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function ge(eid) {
	return document.getElementById(eid);  
}
window.ge = ge;

function capitalizeString(str) 
{
	var inputString = (this != window) ? this : str;
	inputString = inputString.trim();

	var tmpStr, tmpChar, preString, postString, strlen;
	tmpStr = inputString.toLowerCase();
	stringLen = tmpStr.length;
	if (stringLen > 0)
	{
	  for (i = 0; i < stringLen; i++)
	  {
	    if (i == 0)
		{
	      tmpChar = tmpStr.substring(0,1).toUpperCase();
	      postString = tmpStr.substring(1,stringLen);
	      tmpStr = tmpChar + postString;
	    }
	    else
		{
	      tmpChar = tmpStr.substring(i,i+1);
	      if (tmpChar == " " && i < (stringLen-1))
		  {
	      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
	      preString = tmpStr.substring(0,i+1);
	      postString = tmpStr.substring(i+2,stringLen);
	      tmpStr = preString + tmpChar + postString;
	      }
	    }
	  }
	}
	return tmpStr;
}
String.prototype.capitalize = capitalizeString;

function getElement(formName, elementName)
{
	var theElement = document.forms[formName].elements[elementName];
	if (theElement == null)
	{
		alert("Element ["+this.elementName+"] not found");
		return null;
	}
	
	return theElement;
}

function getValue(obj)
{
	var rtn = "" ;
	var objType = obj.type ;
	if ( objType == "text" || objType == "hidden" || objType == "textarea" ) {
		rtn = obj.value ;
	} else if ( objType == "checkbox" ) {
		if ( obj.checked == true)
			rtn = "Yes" ;
		else
			rtn = "No" ;
	} else if ( obj[0] != null && obj[0].type == "checkbox" ) {
		var length = obj.length ;
		deli = "";
		
		for (i = 0; i<length; i++)
		{
			if ( obj[i].checked == true )
			{
				rtn = rtn + deli + obj[i].value ;
				deli = "," ;
			}
		}
	} else if ( objType == "select-one" ) {
		var rtn = "" ;
		rtn = obj.options[obj.selectedIndex].value ;
	} else if ( objType == "select-multiple" ) {
		var rtn = "" ;
		var length = obj.options.length ;
		for (i = 0; i<length; i++)
		{
			if ( obj.options[i].selected == true )
				rtn = rtn + obj.options[i].value + "\r\n" ;
		}
	} else if ( objType == "radio") {
		if ( obj.checked == true)
			rtn = obj.value ;
		else
			rtn = "not selected" ;
	} else if ( obj[0].type == "radio" ) {
		var length = obj.length ;
		for (i = 0; i<length; i++)
		{
			if ( obj[i].checked == true )
				rtn = obj[i].value ;
		}
	} else   {
		
		alert(obj[0].type) ;
	}			
	return rtn ;
}

function patternMatch(theValue, thePattern)
{
	theValue = theValue.trim() ;
	var matchPattern = new RegExp(thePattern, "");
	var matchArray = theValue.match(matchPattern);
	if ( matchArray != null )
		return true ;
	else
		return false ;
}	

function hideButtons()
{
	buttonDiv = document.getElementById("buttons");
	//buttonDiv.style.display = "none";
	buttonDiv.style.visibility='hidden';
	return;
}

function showButtons()
{
	buttonDiv = document.getElementById("buttons");
	buttonDiv.style.visibility='visible';
	return;
}

function showDivForm()
{
	var divForm = document.getElementById("divForm");
	divForm.style.visibility='visible';
	return;
}


function getFileExtention(uri)
{
	var rtn = "";
	var dotIndex = uri.lastIndexOf(".");
	var slashIndex = uri.lastIndexOf("\\");
	if ( dotIndex > slashIndex || dotIndex >= 0)
	{
		rtn = uri.substr(dotIndex + 1);
	}
	return rtn;
}

function getFileName(uri)
{
	var rtn = uri;
	var slashIndex = uri.lastIndexOf("\\");
	if ( slashIndex  >= 0)
	{
		rtn = uri.substr(slashIndex + 1);
	}
			
	return rtn;
}

function isJpgFile(uri)
{
	var ext = getFileExtention(uri).toUpperCase() ;
	return ( ext == "JPG" || ext == "JPEG" );
}

function validateJpgFileUpload(uri)
{
	var rtn = true;
	if (uri != null && uri.length > 0)
	{
		rtn = isJpgFile(uri);
	}
	if (!rtn)
	{
		var str = "The file you are trying to upload: \n\n      " + getFileName(uri) + "\n\n is not a valid JPG file. \n\n";
		str += "Please only upload image file of JPG format, with the file size of less than 512K bytes.";
		alert(str);
	}
	return rtn;
}

function isLatin(str)
{
	var rtn = true;
	var ucode;
	for (i=0; i<str.length; i++)
	{
		ucode = str.charCodeAt(i);
		if (ucode > 255) {
			rtn = false;
			break;
		}
	}
	return rtn;
}

function withTobaccoDeprecated(str)
{
	var rtn = false;
	var tobacco = "带烟";
	if (str.indexOf(tobacco) >=  0) {
		rtn = true;
		alert("您的广告不可包含任何与烟草相关的信息.");
	}
	return rtn;
}

function withTobacco(str)
{
	var rtn = false;
	var idx = 0;
	var tobacco = new Array();
	tobacco[idx++] = "带烟";
	tobacco[idx++] = "帶烟";
	for (i=0; i<idx; i++)
	{
		if (str.indexOf(tobacco[i]) >=  0) {
			rtn = true;
			alert("您的广告不可包含任何与烟草相关的信息! 本网站严禁任何烟草信息, 违反者将导致被取消服务, 希望您理解并合作, 谢谢!");
		}
	}
	return rtn;
}

// Gets a browser specific XmlHttpRequest Object
function newXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}

function fetchHtml(url, objectId) {
	var xhReq = newXmlHttpRequestObject();
	xhReq.open("get", url, true);
	xhReq.onreadystatechange = function() {
		// wait until the response is completed
		if (xhReq.readyState != 4)  {
		 return;
		}
		var serverResponse = xhReq.responseText;
		
		
		var newdiv = document.createElement("div");
		newdiv.innerHTML = serverResponse;
		var container = document.getElementById(objectId);
		container.appendChild(newdiv);
		
		
//		var targetObject = document.getElementById(objectId);
//		targetObject.innerHTML = serverResponse;
		if (window.afterFetchHtml)
			afterFetchHtml();
	};
	xhReq.send(null);
}

// concat checked checkbox/radio items to text field
function checkboxToText(cbFName, txtFId )
{
	// Parameter: field name of checkbox; field ID of text field
	var strCbSelected = "";
	var deli = "";

	var txtF = document.getElementById(txtFId);
	var cbItems = document.getElementsByName(cbFName);
	for (var i=0; i < cbItems.length; i++)
	{
		if (cbItems[i].checked)
		{
			strCbSelected = strCbSelected + deli + cbItems[i].value;
			deli = "| ";
		}
	}
	if ( strCbSelected != "" )
		txtF.value = strCbSelected ;
	return;
} 

// check checkbox/radio items based on value of text field
function textToCheckbox(txtFId, cbFName)
{
	// Parameter: field ID of text field; field name of checkbox
	
	var deli = "|";
	var txtF = document.getElementById(txtFId);
	var cbItems = document.getElementsByName(cbFName);

	var strText = txtF.value;
	var items = strText.split(deli);
	
	for (var i=0; i < cbItems.length; i++)
	{
		cbItems[i].checked = false;
		var cbItem = cbItems[i].value;
		for (var j=0; j<items.length; j++)
		{
			if ( cbItem.trim() === items[j].trim() )
			{
				cbItems[i].checked = true;
				break;
			}
		}
	}
	return;
} 

function toggle(evt, divId)
{
	contentDiv = document.getElementById(divId);
	curStat = contentDiv.style.display;
	if (curStat == "block")
		contentDiv.style.display = "none";
	else
	{
		contentDiv.style.display = "block";
	}
	return false;
}

function markAsRead(sno) {
	var objStatus = "status" + sno;
	var statusStr = document.getElementById(objStatus).innerHTML;
	if (statusStr == "New") {
		var url = "/forum/pms/memPm_setStatusRead.php?sno=" + sno;
		var objId = "ajaxOutput";
		fetchHtml(url, objId);
		var objA = "time" + sno;
		document.getElementById(objA).style.fontWeight = "normal";
		document.getElementById(objStatus).innerHTML = "Read";
	}
	return true;
}

function deleteSingle(evt, sno) {
	document.getElementById("pmH" + sno).style.display = "none";
	document.getElementById("pm" + sno).style.display = "none";

	var url = "/forum/pms/memPm_deleteSingle.php?sno=" + sno;
	var objId = "ajaxOutput";
	fetchHtml(url, objId);
	return false;
}

function checkUncheckAll(theElement) 
{
   var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length; z++)
	 {
   		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
   		{
	 			theForm[z].checked = theElement.checked;
	 		}
   }
}

function del_confirm(theElement)
{
	 var theForm = theElement.form;
	 var z = 0;
	 var ifChecked = 0;
	 for(z=0; z<theForm.length; z++)
	 {
   		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
   		{
	 			if(theForm[z].checked)
	 			{
	 				ifChecked=1;
	 				break;
	 			}
	 		}
   }
  if(!ifChecked)
  {
  	alert("Please select at least one message that you want to delete.");
  	return false;
  }      
  
var r=confirm("Are you sure you want to delete those selected message(s) permanently?");
if (r==true)
  {
  return true;
  }
else
  {
  return false;
  }
}
