// JavaScript Document


/*
 * *************************************************************************
 * Name			: trim()
 * Description	: to remove unwanted spaces in the starting and ending of the text
 * Parameter	: text
 * Return value : String
 * Example		: str = trim(str) 
 * *************************************************************************
 */

function trim(sText) 
{
  while (sText.substring(0,1) == ' ') 
  {
    sText = sText.substring(1,sText.length);
  }
  while (sText.substring(sText.length-1,sText.length) == ' ') 
  {
    sText = sText.substring(0,sText.length-1);
  }
  return sText;
}


/*
 * *************************************************************************
 * Name			: isValidChar(inputText, ValidChars)
 * Description	: to check whether a string is valid
 * Parameter	: inputText  - text to be validated
 *                ValidChars - Valid characters list
 * Return value : true or false
 * Example		: isValidChar(str)
 * *************************************************************************
 */

function isValidChar(inputText, ValidChars)
{
	var Char;
	var i;

	// if length of the string is zero
	if (inputText.length <= 0)
		return false;

	// iterate each character in the string
	for(i=0;i<inputText.length;i++)
	{
		Char = inputText.charAt(i); 

		// if the character is found in the valid string
		if (ValidChars.indexOf(Char) == -1) 
		{
		  return false;
		}
	}
	return true;
}

/*
 * *************************************************************************
 * Name			: isNumeric(sText)
 * Description	: to check whether a string is numeric value
 * Parameter	: text
 * Return value : true or false
 * Example		: isNumeric(str)
 * *************************************************************************
 */

function isNumeric(sText) 
{
	var ValidChars = "0123456789";
	return isValidChar(sText, ValidChars);
}


/*
 * *************************************************************************
 * Name			: isNumericWithDecimal(sText)
 * Description	: to check whether a string is numeric value
 * Parameter	: text
 * Return value : true or false
 * Example		: isNumericWithDecimal(str)
 * *************************************************************************
 */

function isNumericWithDecimal(sText) 
{
	var ValidChars = "0123456789.";
	return isValidChar(sText, ValidChars);
}

/*
 * *************************************************************************
 * Name			: isNumericSpace(sText)
 * Description	: to check whether a string is numeric value
 * Parameter	: text
 * Return value : true or false
 * Example		: isNumericSpace(str)
 * *************************************************************************
 */

function isNumericSpace(sText) 
{
	var ValidChars = "0123456789 -";
	return isValidChar(sText, ValidChars);
}

/*
 * *************************************************************************
 * Name			: isAlphaNumeric(inputText)
 * Description	: to check whether a string is alphanumeric
 * Parameter	: text
 * Return value : true or false
 * Example		: isAlphaNumeric(str)
 * *************************************************************************
 */

function isAlphaNumeric(inputText)
{
	var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
	return isValidChar(inputText, ValidChars);
}

/*
 * *************************************************************************
 * Name			: isAlphabetic(inputText)
 * Description	: to check whether a string is alphanumeric
 * Parameter	: text
 * Return value : true or false
 * Example		: isAlphabetic(str)
 * *************************************************************************
 */

function isAlphabetic(inputText)
{
	var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	return isValidChar(inputText, ValidChars);
}

/*
 * *************************************************************************
 * Name			: isAlphaComDash(inputText)
 * Description	: to check whether a string is alphanumeric
 * Parameter	: text
 * Return value : true or false
 * Example		: isAlphabetic(str)
 * *************************************************************************
 */

function isAlphaComDash(inputText)
{
	var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ,-";
	return isValidChar(inputText, ValidChars);
}

function isValidPassword(inputText)
{
	var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_!@#$%^*_-~:;.,";
	return isValidChar(inputText, ValidChars);
}


/*
 * *************************************************************************
 * Name			: isNullValue()
 * Description	: to check null value in text box
 * Parameter	: ctrName  - control name (text box name)
 *				  msg      - message to be alerted
 *                divName    - name of the div where to show tbe error message
 *  Return value: true or false
 *  Example		: isNullValue("username","Enter username","div_username") 
 * *************************************************************************
 */

function isNullValue(ctrName, msg, divName) 
{
	var objCtrl;
	var objValue;
	var objDiv;

	// reference to the control
	objCtrl = document.getElementById(ctrName);
	objDiv = document.getElementById(divName);
	
	// if div is present
	if (objDiv != null) {
		objDiv.innerHTML = "";
	}

	// if control is not found
	if (objCtrl != null)
	{
		objValue = objCtrl.value;

		// check the value
		if (trim(objValue) == "")
		{
			//display message in div
			if (objDiv != null) {
				objDiv.innerHTML = msg;
			}
			return true;
		}
		else
			return false;
	}
	else
		return true;
}


 /*
 * *************************************************************************
 * Name			: setDivValue(objName, argValue)
 * Description	: to set the value for div tag
 * Parameter	: objName - element name 
 *                argValue - value to be dispalyed
 * Return value : 
 * Example		: setDivValue("txtName", "text")
 * *************************************************************************
 */
 function setDivValue(objName, argValue)
 {
 	var objElement = document.getElementById(objName);
 	if (objElement != null)
 	{
 		objElement.innerHTML = argValue;
 	}
 }


/*
 * *************************************************************************
 * Name			: emailCheck(str)
 * Description	: to check whether a string is valid email
 * Parameter	: text
 * Return value : true or false
 * Example		: emailCheck(str) 
 * *************************************************************************
 */

// valid chars : a-z, A-Z, 0-9, _, .
function emailCheck(ctrName, strMsg, focus) 
{
	var at="@";
	var dot=".";

	var str;
	var objValue;
	var strMessage;
	var flgValid = true;

	objCtrl = document.getElementById(ctrName);

	// if control is not found
	if (objCtrl != null)
		str = objCtrl.value;
	else
	{
		flgValid = false;
		str = "";
	}

	// checking valid character in email address
	flgValid = isValidEmailChars(str);
	

	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);


	// alert message
	if (trim(strMsg) != "")
		strMessage = strMsg;
	else
		strMessage = "Invalid E-mail ID";

	// validate email
	if (str.indexOf(at)==-1)
	   flgValid = false;

	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   flgValid = false;

	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		flgValid = false;
	
	else if (str.indexOf(at,(lat+1))!=-1)
		flgValid = false;
	
	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		flgValid = false;
	
	else if (str.indexOf(dot,(lat+2))==-1)
		flgValid = false;
	
	else if (str.substring(str.length-1,str.length)==".")
		flgValid = false;
	
	else if (str.indexOf(" ")!=-1)
		flgValid = false;
	
	// if validation fails
	if (flgValid == false)
	{
		alert(strMessage);
		if (focus == true)
		{
			objCtrl.focus();
		}
	}
	else {
		flgValid = true;					
	}
	
	return flgValid;	

}

/*
 *  * *************************************************************************
 *  Function Name: fnLstSelect
 *  Description  : this function select an item in a list/Menu
 *  parameters   : ctlname - control name of the list box
 * 				   value - value or text of the list box to be selected
 *
 *  Example      : fnLstSelect("categoryId","1")
 * * *************************************************************************
*/

function fnLstSelect(ctlname,value)
{
	var selObj = document.getElementById(ctlname);
	var len;

	if (selObj != null)
	{
		// get the count of list items
		len = selObj.length;
		
		// iterate for each value in list
		for(i=0;i<len;i++)
		{
			// if the value matches
			if(selObj.options[i].value == value || selObj.options[i].text == value)
			{
				selObj.selectedIndex = i;
			}
		}
	}
}

/*
 * *************************************************************************
 * Name			: openBrWinCenter()
 * Description	: to show a popup window in centre of screen
 * Parameter	: theURL  - url of page 
 *				  winName - window name
 *                features - features for the popwidow other than height and width
 *				  w_width  - width of the screen
 *                w_height - height of the screen
 *  Return value: 
 *  Example		: openBrWinCenter("a.htm","popwin","height=300,width=300",300,300) 
 * *************************************************************************
 */

function openBrWinCenter(theURL,winName,features, w_width, w_height) 
{ 
	// calculate left and top values for window
	var left = parseInt((screen.width/2) - (w_width/2));
	var top = parseInt((screen.height/2) - (w_height/2));
	
	// if url is not empty
	if (theURL != "")
	{
		window.open(theURL,winName,"left=" + left + ",top=" + top + "," + features);
	}
}

/*
 * *************************************************************************
 * Name			: isNull()
 * Description	: to check null value in text box
 * Parameter	: ctrName  - control name (text box name)
 *				  msg      - message to be alerted
 *                focus    - to focus the control after alerting, give true or false
 *  Return value: true or false
 *  Example		: isNull("username","Enter username",true) 
 * *************************************************************************
 */

function isNull(ctrName, msg, focus) 
{
	var objCtrl;
	var objValue;

	// reference to the control
	objCtrl = document.getElementById(ctrName);
	
	// if control is not found
	if (objCtrl != null)
	{
		objValue = objCtrl.value;

		// check the value
		if (trim(objValue) == "")
		{
			if (trim(msg) != "") alert(msg);
			if (focus == true)   objCtrl.focus();
			return true;
		}
		else
			return false;
	}
	else
		return true;
}


/*
 * *************************************************************************
 * Name			: fnInteger()
 * Description	: this function return only integer values
 * Parameter	: event
 *
 *  Return value: true or false
 *  Example		: onkeypress="return fnInteger(event)"
 * *************************************************************************
 */

function fnInteger(objEvent)//this return integer value
{
	  var iKeyCode;
	  iKeyCode = objEvent.keyCode;

	  // if it is digit
	  if(iKeyCode>=48 && iKeyCode<=57) return true;

	  return false;
}

/*
 * *************************************************************************
 * Name			: fnFloat()
 * Description	: this function return only float values
 * Parameter	: event
 *
 *  Return value: true or false
 *  Example		: onkeypress="return fnFloat(event)"
 * *************************************************************************
 */

function fnFloat(objEvent)
{
	  var iKeyCode;
	  iKeyCode = objEvent.keyCode;

	  // if it is digit
	  if(iKeyCode>=48 && iKeyCode<=57) return true;
	  if(iKeyCode==46) return true;

	  return false;
}

/*
 * *************************************************************************
 * Name			: isAlphaNumeric(inputText)
 * Description	: to check whether a string is alphanumeric
 * Parameter	: text
 * Return value : true or false
 * Example		: isAlphaNumeric(str)
 * *************************************************************************
 */

function isValidEmailChars(inputText)
{
	var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._@";
	return isValidChar(inputText, ValidChars);
}
 

/*
 * *************************************************************************
 * Name			: emailCheckDiv(str)
 * Description	: to check whether a string is valid email
 * Parameter	: text
 * Return value : true or false
 * Example		: emailCheckDiv(str) 
 * *************************************************************************
 */

// valid chars : a-z, A-Z, 0-9, _, .
function emailCheckDiv(ctrName, strMsg, divName) 
{
	var at="@";
	var dot=".";

	var str;
	var objValue;
	var objDiv;
	var flgValid = true;
	var strMessage;

	objCtrl = document.getElementById(ctrName);
	objDiv = document.getElementById(divName);

	// if control is not found
	if (objCtrl != null)
		str = objCtrl.value;
	else
		return false;

	// checking valid character in email address
	flgValid = isValidEmailChars(str) 


	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	// alert message
	if (trim(strMsg) != "")
		strMessage = strMsg;
	else
		strMessage = "Invalid E-mail ID";

	// validate email
	if (str.indexOf(at)==-1)
	   flgValid = false;

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   flgValid = false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		flgValid = false;
	
	if (str.indexOf(at,(lat+1))!=-1)
		flgValid = false;
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		flgValid = false;
	
	if (str.indexOf(dot,(lat+2))==-1)
		flgValid = false;
	
	if (str.substring(str.length-1,str.length)==".")
		flgValid = false;
	
	if (str.indexOf(" ")!=-1)
		flgValid = false;
	
	// if validation fails
	if (flgValid == false)
	{
		if (objDiv != null)
			objDiv.innerHTML = strMessage;		
	} 
	else {
		if (objDiv != null)
			objDiv.innerHTML = "";	
		return true;		
	}	
	 
	return flgValid;			
}


 /*
 * *************************************************************************
 * Name			: setTextValue(objName, strValue) 
 * Description	: to set the value for div tag
 * Parameter	: objName - element name 
 *                argValue - value to be dispalyed
 * Return value : 
 * Example		: setTextValue("txtName","")
 * *************************************************************************
 */
 
 function setTextValue(objName, strValue) 
 {
 	var objElement = document.getElementById(objName);
 	
 	if (objElement != null)
 		objElement.value = strValue;

 }

  
 /*
 * *************************************************************************
 * Name			: getTextValue(objName)
 * Description	: to set the value for div tag
 * Parameter	: objName - element name 
 *                argValue - value to be dispalyed
 * Return value : 
 * Example		: getTextValue("txtName")
 * *************************************************************************
 */
 
 function getTextValue(objName) 
 {
 	var objElement = document.getElementById(objName);
 	
 	if (objElement != null)
 		return objElement.value;
 	else
 		return "";
 }

