
/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function addOptionList(optSrcId, OptTargetId) {
	
	//alert(optSrcId);
	var src = document.getElementById(optSrcId);
	var target = document.getElementById(OptTargetId);
	
	for( i = src.options.length -1; i >= 0; i-- ) {
		//alert(i);
		if( src[i].selected ) {
			//alert("sel");
			addOption(target, src[i].text, src[i].value);
			removeOption( src, i);
		}
	}
	//sortlist(src);
	//sortlist(target);
}


function addOption(obj, text, value) {
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	obj.options.add(optn)
}


function removeOption(obj, i) {
	obj.remove(i);
}


function sortlist(objId) {
	
	var lb = objId;
	
	//var lb = document.getElementById('mylist');
	arrTexts = new Array();

	for(i=0; i<lb.length; i++)  {
	  arrTexts[i] = lb.options[i].text;
	}

	arrTexts.sort();

	for(i=0; i<lb.length; i++)  {
	  lb.options[i].text = arrTexts[i];
	  lb.options[i].value = arrTexts[i];
	}
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


/* E-mail validation */
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
}


function ValidEmail(obj){

	var emailID = obj;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	} else if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	} else {
		return true;
	}
}


/* Word count function for textarea */
function wordCount(field,counter,maxlimit,linecounter) {
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	// trim the extra text
	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	} else { 
		document.getElementById(counter).innerHTML=charcnt+" of "+maxlimit+" character(s)";
	}
}

function ValidPhone(obj, format) {
	
	if (format == undefined) format = 'xxx-xxx-xxxx';
	var isPhone = true;
	var objName = obj.name.replace(/_/g, " ");

	if (isEmpty(obj, 'Please enter value for ' + objName)) {
		return false;
	} 
	
	switch (format) {
		case 'xxx-xxx-xxxx': 
			if (obj.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1) isPhone = false; break;

		case 'xxx': 
			if (obj.value.search(/\d{3}/)==-1) isPhone = false; break;

		case 'xxx-xxxx': 
			if (obj.value.search(/\d{3}\-\d{4}/)==-1) isPhone = false; break;
	}
	
	if (!isPhone) {
      alert("The value for " + objName + " you entered is not valid.\r\nPlease enter a valid format i.e. " + format);
	  //obj.value = ''; 
	  obj.focus(); // please don't make focus to field, to avoid infinite loop
      return false;
   } else {
	  return true;
   }
}

function ValidPassword(obj) {
	if (isEmpty(obj, 'Please enter password')) {
		obj.focus();
		return false;
	} else if (obj.value.length < 6) {
		alert('Please enter minimum 6 characters');
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function getFocus(obj) {
	obj.focus();
}



