
/*
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();
}

function gotopage(obj) {
	var path = obj.options[obj.selectedIndex].value;
	location.href = path;
}


function checkAllCheckboxes(frm) {
	if (frm.c1.checked) {
    	for(i=0; i<frm.length; i++)
		 frm.elements[i].checked=true;
	} else {
      for(i=0; i<frm.length; i++)
		 frm.elements[i].checked=false;
	}
}


function confirmDelete(frm) {
	if(confirm('Are you sure to delete?')) {
		frm.submit();
	} else {
		return false;
	}
}

/* JS for inline message */

/* To make look good */

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 15;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}

/* END JS for inline message */
