
//function for validating date
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

//This function validates Date
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

//Function to show alert message to user
//This is called implicitly
function ShowAlert(Element,Msg,UniqueID,FormName){
	if(FormName==""){
		document.forms[0].elements[Element].focus();
	}else{
		document.forms[FormName].elements[Element].focus();
	}
	alert(Msg);
	return false;
}

//This function shows the error message using the layer, applicable where u used dispaly method
function ShowError(divID){
 var divObj = document.getElementById ? document.getElementById(divID) : document.all ? document.all[divID] : document.layers[divID];
   var divStyle = (document.layers) ? divObj : divObj.style;
   var vis = divStyle.visibility;
   if (document.layers)
      divStyle.visibility = 'show';
   else           
   divStyle.visibility = 'visible' ;
   return false;
}

function ShowLayerAlert(Element,Msg,LayerID,timeout){
self.mb.alert(Msg);
self.mb.center();
self.mb.setOpacity(100);
if(timeout>0 && timeout!=""){
Akaar_close(timeout);
document.forms[1].elements[Element].focus();
return false;
}
document.forms[1].elements[Element].focus();
return false;
}

function LayerDisplay(Element,Msg,LayerID){
return ShowError(LayerID);
document.forms[1].elements[Element].focus();
return false;
}

//chaged by mihir on 18 Apr, 2008.
//added argument FormName. If Form is passed it is used to get the control, otherwise first form is used.
function ValidateBlank(Element,Extra,FormName){
	var val;
	if(FormName==""){
		val = document.forms[0].elements[Element].value;
	}else{
		val = document.forms[FormName].elements[Element].value;
	}
	
	if(val == ""){
		return false;
	} else {
		return true;
	}
}

//added by mihir on 19 January, 2009
//This fundtion is used to validate state drop-down based on country selection
//id of country drop-down is passedin Extra parameter
function ValidateState(Element,Extra,FormName){
	var val;
	if(FormName==""){
		val = document.forms[0].elements[Element].value;
		country = document.forms[0].elements[Extra].value;
	}else{
		val = document.forms[FormName].elements[Element].value;
		country = document.forms[FormName].elements[Extra].value;
	}
	if((country=="US") && (val == "")){
		return false;
	} else {
		return true;
	}
}


//added by mihir on 19 January, 2009
//This fundtion is used to validate other state drop-down based on country selection
//id of country drop-down is passedin Extra parameter
function ValidateOtherState(Element,Extra,FormName){
	var val;
	if(FormName==""){
		val = document.forms[0].elements[Element].value;
		country = document.forms[0].elements[Extra].value;
	}else{
		val = document.forms[FormName].elements[Element].value;
		country = document.forms[FormName].elements[Extra].value;
	}

	if((country!="US") && (val == "")){
		return false;
	} else {
		return true;
	}
}

//This function can be use to validate or compare password from two input, name of the two input box is passed as second parameter of this function joined by "_"
//This function also shows how u can get extra parameter in your javascript function
//chaged by mihir on 18 Apr, 2008.
//added argument FormName. If Form is passed it is used to get the control, otherwise first form is used.
function ValidatePassword(Element,Extra,FormName){
	var splitme=Extra.split("#");
	var fld1=splitme[0];
	var fld2=splitme[1];
	
	if(FormName==""){
		var pass1=document.forms[0].elements[fld1].value;
		var pass2=document.forms[0].elements[fld2].value;
	}else{
		var pass1=document.forms[FormName].elements[fld1].value;
		var pass2=document.forms[FormName].elements[fld2].value;
	}
	
	if(pass2==null || pass2==null)
	{
		return false;
	}
	if(pass2!=pass1)
	{
		return false;
	}
	return true;
}

//This function validates zip code for US i.e allows only five digit in zip
function ValidateUSZip(Element){
if ((document.forms[1].elements[Element].value=="") ||(document.forms[1].elements[Element].length!=5)) {
return false;
}
return true;

}

//This function validates Date
function ValidateDate(Element){
if (isDate(document.forms[1].elements[Element].value)==false){
		return false;
		}
return true;
}


//this function validates number, allows only number in given value
function ValidateNumber(Element,Extra,FormName){
	if(!ValidateBlank(Element,Extra,FormName)){
	return false;
	}

	chk1=".!@#$%^*()-+=|\~`{}[]: <>?/,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	chk3="0123456789";
	
	var val;
	if(FormName==""){
		val = document.forms[0].elements[Element].value;
	}else{
		val = document.forms[FormName].elements[Element].value;
	}

	for(k=0;k!=val.length;k++)
	{
		ch1= val.charAt(k);
		ch2= val.charAt(0);
		rtn1=chk1.indexOf(ch1);
		rtn3=chk3.indexOf(ch2);
		
		if(rtn3 < 0)
		{
			return false;
			break;
	 	}
		else if(rtn1!=-1)
		{
			return false;
			break;	
		}
	  }
		return true;
}

//this function validates number, allows only number in given value
function ValidateFloatNumber(Element,Extra,FormName){
	if(!ValidateBlank(Element,Extra,FormName)){
	return false;
	}

	chk1="!@#$%^*()-+=|\~`{}[]: <>?/,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	chk3="0123456789";
	
	var val;
	if(FormName==""){
		val = document.forms[0].elements[Element].value;
	}else{
		val = document.forms[FormName].elements[Element].value;
	}

	for(k=0;k!=val.length;k++)
	{
		ch1= val.charAt(k);
		ch2= val.charAt(0);
		rtn1=chk1.indexOf(ch1);
		rtn3=chk3.indexOf(ch2);
		
		if(rtn3 < 0)
		{
			return false;
			break;
	 	}
		else if(rtn1!=-1)
		{
			return false;
			break;	
		}
	  }
		return true;
}
//This function validaets alpha numeric, allows only alphpa numberic characters 
function ValidateAlphaNumeric(Element){
	if(!ValidateBlank(Element)){
	return false;
	}
	 chk1 = "!@#$%^*()-+=|\~`{}[]: <>?/,";
	chk3="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for(k=0;k!=document.forms[1].elements[Element].value.length;k++)
	{
	ch1= document.forms[1].elements[Element].value.charAt(k);
	ch2= document.forms[1].elements[Element].value.charAt(0);
	rtn1=chk1.indexOf(ch1);
	rtn3=chk3.indexOf(ch2);
		if(rtn3 < 0)
		{
			return false;
			break;
	 	}
		else if(rtn1!=-1)
		{
			return false;
			break;	
		}
	  }
return true;

}

//Created by mihir on 1 May, 2008.
//This function is used to check whether a checkbox is checked or not.
function ValidateBlankCheckBox(Element,Extra,FormName){
	var val;
	
	if(FormName==""){
		val = document.forms[0].elements[Element].checked;
	}else{
		val = document.forms[FormName].elements[Element].checked;
	}
	
	return val;
}


//Created by mihir on 1 May, 2008.
//This function is used to validate attachment type.
function ValiateAttachment(Element,Extra,FormName){
	var attchval;
	
	if(FormName==""){
		attchval = document.forms[0].elements[Element].value;
	}else{
		attchval = document.forms[FormName].elements[Element].value;
	}

	//if field is optional and blank, do not validate.
	if(Extra=="optional" && attchval==""){
		return true;
	}else{
		if(attchval!=""){	//if field is not blank
			var temp = attchval.split(".");
			var file_type = temp[temp.length-1].toLowerCase();

			if(file_type!="jpg" && file_type!="jpeg" && file_type!="gif" && file_type!="png" && file_type!="doc" && file_type!="xls" && file_type!="csv" && file_type!="pdf" && file_type!="txt"){
				return false;	//invalid file type
			}else{
				return true;	//valid file type
			}	//end else
		}else{
			return false;	//Error, field is not optional, can not be blank.
		}	//end else
	}	//end else
}

// This function is used to check image files whether it is gif/jpg
function ValiateImageType(Element,Extra,FormName){
	var imgName;
	
	if(FormName==""){
		imgName = document.forms[0].elements[Element].value;
		frmEle = document.forms[0].elements[Element];
	}else{
		imgName = document.forms[FormName].elements[Element].value;
		frmEle = document.forms[FormName].elements[Element];
	}

	if((Extra=="optional") && (imgName.length == 0)){
		return true;
	}else{
		if (imgName.length < 3) {
			//alert ("Please select valid category images.");
			frmEle.focus();
			return false;			
		}	
		
		if(imgName.indexOf('.gif', 0) == -1 && imgName.length > 3){
			if (imgName.indexOf ('.GIF', 0) == -1 && imgName.length > 3){
				if (imgName.indexOf ('.jpg', 0) == -1 && imgName.length > 3){
					if (imgName.indexOf ('.JPG', 0) == -1 && imgName.length > 3){
						//alert ("Please upload only GIF or JPG file");
						//frmEle.focus();
						return false;
					}else
						return true;
				}else
					return true;
			}else
				return true;
		}else
			return true;
	}//else 
		//return true;
}


/** Function to display the hint and larger image in layer (C) Tigra http://www.softcomplex.com/
The code has been taken from the http://www.softcomplex.com/products/tigra_hints/
Tigra Hints is free JavaScript widget that displays pop-up box with notes (also known as tooltips or hints) when mouse appears over any HTML element on the page. Any HTML is welcome inside the box: lists, tables, images etc. This component makes the site more informative and easy to use while saving valuable space.
*/
function THints (o_cfg, items) {
	this.top = o_cfg.top ? o_cfg.top : 0;
	this.left = o_cfg.left ? o_cfg.left : 0;
	this.n_dl_show = o_cfg.show_delay;
	this.n_dl_hide = o_cfg.hide_delay;
	this.b_wise = o_cfg.wise;
	this.b_follow = o_cfg.follow;
	this.x = 0;
	this.y = 0;
	this.divs = [];
	this.show  = TTipShow;
	this.showD = TTipShowD;
	this.hide = TTipHide;
	this.move = TTipMove;
	if (document.layers) return;
	var b_IE = navigator.userAgent.indexOf('MSIE') > -1,
	s_tag = ['<div id="TTip%name%" style="visibility:hidden;position:absolute;top:0px;left:0px;',   b_IE ? 'width:1px;height:1px;' : '', o_cfg['z-index'] != null ? 'z-index:' + o_cfg['z-index'] : '', '"><table cellpadding="0" cellspacing="0" border="0"><tr><td class="', o_cfg.css, '" nowrap>%text%</td></tr></table></div>'].join('');

	this.getElem = 
		function (id) { return document.all ? document.all[id] : document.getElementById(id); };
	this.showElem = 
		function (id, hide) { this.divs[id].o_css.visibility = hide ? 'hidden' : 'visible'; };
	this.getWinSz = window.innerHeight != null 
		? function (b_hight) { return b_hight ? innerHeight : innerWidth; }
		: function (b_hight) { return document.body[b_hight ? 'clientHeight' : 'clientWidth']; };	
	this.getWinSc = window.innerHeight != null 
		? function (b_hight) { return b_hight ? pageYOffset : pageXOffset; }
		: function (b_hight) { return document.body[b_hight ? 'scrollTop' : 'scrollLeft']; };	
	if (window.opera) {
		this.getSize = function (id, b_hight) { 
			return this.divs[id].o_css[b_hight ? 'pixelHeight' : 'pixelWidth']
		};
		document.onmousemove = function () {
			myHint.x = event.clientX;
			myHint.y = event.clientY;
			if (myHint.b_follow && myHint.visible) myHint.move(myHint.visible)
			return true;
		};
	}
	else {
		this.getSize = function (id, b_hight) { 
			return this.divs[id].o_obj[b_hight ? 'offsetHeight' : 'offsetWidth'] 
		};
		document.onmousemove = b_IE
		? function () {
			myHint.x = event.clientX + document.body.scrollLeft;
			myHint.y = event.clientY + document.body.scrollTop;
			if (myHint.b_follow && myHint.visible) myHint.move(myHint.visible)
			return true;
		} 
		: function (e) {
			myHint.x = e.pageX;
			myHint.y = e.pageY;
			if (myHint.b_follow && myHint.visible) myHint.move(myHint.visible)
			return true;
		};
	}
	for (i in items) {
		document.write (s_tag.replace(/%text%/, items[i]).replace(/%name%/, i));
		this.divs[i] = { 'o_obj' : this.getElem('TTip' + i) };
		this.divs[i].o_css = this.divs[i].o_obj.style;
	}
}

function TTipShow (id) {
	if (document.layers) return;
	this.hide();
	if (this.divs[id]) {
		if (this.n_dl_show) this.divs[id].timer = setTimeout("myHint.showD(" + id + ")", this.n_dl_show);
		else this.showD(id);
		this.visible = id;
	}
}

function TTipShowD (id) {
	this.move(id);
	this.showElem(id);
	if (this.n_dl_hide) this.timer = setTimeout("myHint.hide()", this.n_dl_hide);
}

function TTipMove (id) {
	var n_x = this.x + this.left, n_y = this.y + this.top;
	if (this.b_wise) {
		var n_w = this.getSize(id), n_h = this.getSize(id, true),
		n_win_w = this.getWinSz(), n_win_h = this.getWinSz(true),
		n_win_l = this.getWinSc(), n_win_t = this.getWinSc(true);
		if (n_x + n_w > n_win_w + n_win_l) n_x = n_win_w + n_win_l - n_w;
		if (n_x < n_win_l) n_x = n_win_l;
		if (n_y + n_h > n_win_h + n_win_t) n_y = n_win_h + n_win_t - n_h;
		if (n_y < n_win_t) n_y = n_win_t;
	}
	this.divs[id].o_css.left = n_x;
	this.divs[id].o_css.top = n_y;
}

function TTipHide () {
	if (this.timer) clearTimeout(this.timer);
	if (this.visible != null) {
		if (this.divs[this.visible].timer) clearTimeout(this.divs[this.visible].timer);
		setTimeout("myHint.showElem(" + this.visible + ", true)", 10);
		this.visible = null;
	}
}



function wrap (s_, b_ques) {
	return "<table cellpadding='0' cellspacing='0' border='0' style='-moz-opacity:90%;filter:progid:DXImageTransform.Microsoft.dropShadow(Color=#777777,offX=4,offY=4)'><tr><td rowspan='2'><img src='img/1"+(b_ques?"q":"")+".gif'></td><td><img src='/img/pixel.gif' width='1' height='15'></td></tr><tr><td background='img/2.gif' height='28' nowrap>"+s_+"</td><td><img src='img/4.gif'></td></tr></table>"
}

function wrap_img (s_file, s_title) {
	return "<table cellpadding=5 bgcolor=white style='border:1px solid #777777'><tr><td><img src='"+s_file+"'></td></tr><tr><td align=center>"+s_title+"</td></tr></table>"
}
var HINTS_CFG = {
	'top'        : 5, // a vertical offset of a hint from mouse pointer
	'left'       : 5, // a horizontal offset of a hint from mouse pointer
	'css'        : 'hintsClass', // a style class name for all hints, TD object
	'show_delay' : 500, // a delay between object mouseover and hint appearing
	'hide_delay' : 2000, // a delay between hint appearing and hint hiding
	'wise'       : true,
	'follow'     : false,
	'z-index'    : 0 // a z-index for all hint layers
}

/*==================================================*
 $Id: AkarValidation.js,v 1.8 2009/01/19 11:44:05 mihir Exp $
 Copyright 2003 Patrick Fitzgerald
 http://www.barelyfitz.com/webdesign/articles/filterlist/

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *==================================================*/

function filterlist(selectobj) {

  //==================================================
  // PARAMETERS
  //==================================================

  // HTML SELECT object
  // For example, set this to document.myform.myselect
  this.selectobj = selectobj;

  // Flags for regexp matching.
  // "i" = ignore case; "" = do not ignore case
  // You can use the set_ignore_case() method to set this
  this.flags = 'i';

  // Which parts of the select list do you want to match?
  this.match_text = true;
  this.match_value = false;

  // You can set the hook variable to a function that
  // is called whenever the select list is filtered.
  // For example:
  // myfilterlist.hook = function() { }

  // Flag for debug alerts
  // Set to true if you are having problems.
  this.show_debug = false;

  //==================================================
  // METHODS
  //==================================================

  //--------------------------------------------------
  this.init = function() {
    // This method initilizes the object.
    // This method is called automatically when you create the object.
    // You should call this again if you alter the selectobj parameter.

    if (!this.selectobj) return this.debug('selectobj not defined');
    if (!this.selectobj.options) return this.debug('selectobj.options not defined');

    // Make a copy of the select list options array
    this.optionscopy = new Array();
    if (this.selectobj && this.selectobj.options) {
      for (var i=0; i < this.selectobj.options.length; i++) {

        // Create a new Option
        this.optionscopy[i] = new Option();

        // Set the text for the Option
        this.optionscopy[i].text = selectobj.options[i].text;

        // Set the value for the Option.
        // If the value wasn't set in the original select list,
        // then use the text.
        if (selectobj.options[i].value) {
          this.optionscopy[i].value = selectobj.options[i].value;
        } else {
          this.optionscopy[i].value = selectobj.options[i].text;
        }

      }
    }
  }

  //--------------------------------------------------
  this.reset = function() {
    // This method resets the select list to the original state.
    // It also unselects all of the options.

    this.set('');
  }


  //--------------------------------------------------
  this.set = function(pattern) {
    // This method removes all of the options from the select list,
    // then adds only the options that match the pattern regexp.
    // It also unselects all of the options.

    var loop=0, index=0, regexp, e;

    if (!this.selectobj) return this.debug('selectobj not defined');
    if (!this.selectobj.options) return this.debug('selectobj.options not defined');

    // Clear the select list so nothing is displayed
    this.selectobj.options.length = 0;

    // Set up the regular expression.
    // If there is an error in the regexp,
    // then return without selecting any items.
    try {

      // Initialize the regexp
      regexp = new RegExp(pattern, this.flags);

    } catch(e) {

      // There was an error creating the regexp.

      // If the user specified a function hook,
      // call it now, then return
      if (typeof this.hook == 'function') {
        this.hook();
      }

      return;
    }

    // Loop through the entire select list and
    // add the matching items to the select list
    for (loop=0; loop < this.optionscopy.length; loop++) {

      // This is the option that we're currently testing
      var option = this.optionscopy[loop];

      // Check if we have a match
      if ((this.match_text && regexp.test(option.text)) ||
          (this.match_value && regexp.test(option.value))) {

        // We have a match, so add this option to the select list
        // and increment the index

        this.selectobj.options[index++] =
          new Option(option.text, option.value, false);

      }
    }

    // If the user specified a function hook,
    // call it now
    if (typeof this.hook == 'function') {
      this.hook();
    }

  }


  //--------------------------------------------------
  this.set_ignore_case = function(value) {
    // This method sets the regexp flags.
    // If value is true, sets the flags to "i".
    // If value is false, sets the flags to "".

    if (value) {
      this.flags = 'i';
    } else {
      this.flags = '';
    }
  }


  //--------------------------------------------------
  this.debug = function(msg) {
    if (this.show_debug) {
      alert('FilterList: ' + msg);
    }
  }


  //==================================================
  // Initialize the object
  //==================================================
  this.init();

}


//changed by mihir on 18 Apr, 2008
//added Extra and FormName arguments
//If form name is passed it is used, otherwise first form is used.
function ValidateEmail(Element, Extra, FormName){ 
	if(FormName==""){
		var emailstring = document.forms[0].elements[Element].value;
	}else{
		var emailstring = document.forms[FormName].elements[Element].value;
	}
	//Extra parameter changes by mihir on 1 may, 2008. Changes done to allow optional email field in form. It will allow blank value in email field if "optional" is passed in Extra parameter.
	if(Extra=="optional" && emailstring==""){
		//email address is optional field. If it is blank, do not check anything.
		return true;
	}else{
		var ampIndex = emailstring.indexOf("@");
		var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
		var dotIndex = afterAmp.indexOf(".");
					// determine dot position in entire string (not just after amp portion)
		dotIndex = dotIndex + ampIndex + 1;
					// afterAmp will be portion of string from ampersand to dot
		afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
					// afterDot will be portion of string from dot to end of string
		var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
		var beforeAmp = emailstring.substring(0,(ampIndex));
		var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
					// index of -1 means "not found"
		if ((emailstring.indexOf("@") != "-1") &&
			(emailstring.length > 5) &&
			(afterAmp.length > 0) &&
			(beforeAmp.length > 1) &&
			(afterDot.length > 1) &&
			(email_regex.test(emailstring)) ) {		
			return true;	//email address is valid		  
		} else {
			document.forms[0].elements[Element].focus();
			return false;	//email address is invalid
		}
	}//end of else
	
}

function ValidateBlankHTMLEditor(Element,Extra, FormName){
	val=getEditorContent(Element);
	if(Trim(val)==""){
		return false;
	}
	return true;	
}

function getEditorContent(elemid){
	tinyMCE.triggerSave();
	return document.getElementById(elemid).value;
}

function LTrim(str){
	if (str==null){return null;}
	for(var i=0;str.charAt(i)==" ";i++);
	return str.substring(i,str.length);
}

function RTrim(str){
	if (str==null){return null;}
	for(var i=str.length-1;str.charAt(i)==" ";i--);
	return str.substring(0,i+1);
}

function Trim(str){return LTrim(RTrim(str));}
