//Script
function CheckBlank(tbox,fieldName){
	var tval = tbox.value;
	if(Trim(tval)=="")
	 {

	tbox.focus();
	  //alert("Please enter [ " + fieldName + " ] Its mandatory.");
	  alert( message("1") + " " + fieldName + ". " + message("2"))
	  return false;
	  }
	return true;
}

function CheckBlankNewMSG(tbox,fieldName){
	var tval = tbox.value;
	if(Trim(tval)=="")
	 {

	 tbox.focus();
	  //alert("Please enter [ " + fieldName + " ] Its mandatory.");
	  alert(fieldName)
	  return false;
	  }
	return true;
}

function CheckBlankNoMSG(fieldValue){
		if(Trim(fieldValue)==""){
			return false;
		}
	return true;
}


function CheckQuotes (tbox, fieldName,type){
    var fieldValue= tbox.value;
	if(fieldValue!=''){
		str = new String(fieldValue)
		len = str.length
		for (i=0;i<=len;i++) {
			val = str.charAt(i);
			if (val == "'"  && (type=="s" || type=="S"))
			 {	//alert("Apostrophe is not Allowed.");
				alert(message("4"));
				tbox.focus();
				return false;
			} else if(val == '"' && (type=="d" || type=="D")) {
				//alert("Double Quote is not Allowed.");
				alert(message("5"));
				tbox.focus();
				return false;
			}
		}
	}
	return true;
}

// Function to control number of characters typed into Textarea.
// Input Parameters: textfield - TextArea Name
//                   maxlimit  - Maximum number of characters.
function textControl(textfield,maxlimit)
{
  if (textfield.value.length > maxlimit) 
  {
     textfield.value = textfield.value.substring(0, maxlimit);
  }
}

function CheckAlphaNumeric(fieldName, fieldValue){
	if(fieldValue!=''){
		str = new String(fieldValue)
		len = str.length
		for (i=0;i<=len;i++){
				val = str.charCodeAt(i)
				if ((val > 0) && (val <= 31)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val > 32) && (val <= 47)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 58) && (val <= 64)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 91) && (val <= 94) && (val != 96)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 123) && (val <= 127)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6"))
					return false;
				}
			}
	}
	return true;
}

// Use only letters (a_z), Numbers (0-9), spcial character (_) and No Spaces 
function CheckAlphaNumeric_Version1(fieldName, fieldValue){
	if(fieldValue!=''){
		str = new String(fieldValue)
		len = str.length
		for (i=0;i<=len;i++){
				val = str.charCodeAt(i)
				if ((val > 0) && (val <= 31)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val > 32) && (val <= 47)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 58) && (val <= 64)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 91) && (val <= 94) && (val != 96)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 123) && (val <= 127)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6"))
					return false;
				}
				if ((val >= 65) && (val <= 91)){
					alert("Caps letters are not allowed ")
					//alert(fieldName + ' : ' + message("6") )
					return false;
				}
			}
	}
	return true;
}

// Use only letters (a-z, A-Z), Numbers (0-9) and No Spaces 
function CheckAlphaNumeric_Version2(fieldName, fieldValue){
	if(fieldValue!=''){
		str = new String(fieldValue)
		len = str.length
		for (i=0;i<=len;i++){
				val = str.charCodeAt(i)
				if ((val > 0) && (val <= 31)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val > 32) && (val <= 47)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 58) && (val <= 64)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 91) && (val <= 94) && (val != 96)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6") )
					return false;
				}
				if ((val >= 123) && (val <= 127)){
					//alert(fieldName + ': Only Alphanumeric Character Permitted.');
					alert(fieldName + ' : ' + message("6"))
					return false;
				}
				if (val == 95){
					alert(fieldName + ': Should not contain Underscore.');
					//alert(fieldName + ' : ' + message("6") )
					return false;
				}
			}
	}
	return true;
}

function CheckSpace(fieldName,fieldValue){
	x = fieldValue.length;
	for(i=0;i < x;i++){
		if(fieldValue.substring(i,i+1)==" "){
			//alert(fieldName + " cannot have spaces");
			alert(fieldName + " " + message("3"))
			return false;
		}
	}
	return true;
}


/*
This is how you invoke it - CheckMinSelectGrid(CheckBox1,from,to,4, 2); 

PS: The name of the Checkboxes have to be same...(It'll form an array)				
*/
function CheckMinSelectGrid(ctrlName, heading, colFrom, colTo, rowVal, minSelect) {
	var ctr=0;
	var begCtr = 0;
	for(begCtr=parseInt(colFrom); begCtr<=parseInt(colTo); begCtr++) {
		if(document.frmTheForm.elements[ctrlName+begCtr+rowVal].Checked==true) {
			ctr++;
		}
		if(ctr>=parseInt(minSelect)) return true;
	}
	//alert('Minimum '+minSelect+ ' Checkbox has to be selected under the head "'+heading+'"');
	return false;
}

/*
This is how you invoke it - CheckMinSelectGrid(CheckBox1,from,to,4, 2); 

PS: The name of the Checkboxes have to be same...(It'll form an array)				
*/
function CheckMinSelectGridNoMSG(ctrlName, heading, colFrom, colTo, rowVal, minSelect) {
	var ctr=0;
	var begCtr = 0;
	alert(ctrlName+colFrom+rowVal);
	for(begCtr=parseInt(colFrom); begCtr<=parseInt(colTo); begCtr++) {
		if(document.frmTheForm.elements[ctrlName+begCtr+rowVal].Checked==true) {
			ctr++;
		}
		if(ctr>=parseInt(minSelect)) return true;
	}
	return false;
}

// This function checks whether any of the list items have been selected or not..
//list ::  the full name of list as string -> "document.form.list"
//fieldName:: the name of the field.. to be used in alert.

function CheckComboSel(list, fieldName){
  var lval = list.selectedIndex;
  if (lval==0)
   {list.focus();
    //alert("Please enter [ " + fieldName + " ] Its mandatory.");
    alert( message("23") + " " + fieldName + ". " + message("2") )
    return false;
    }
  return true;  
}
function CheckComboSelNewMSG(list, fieldName){
  var lval = list.selectedIndex;
  if (lval==0)
   {list.focus();
    //alert("Please enter [ " + fieldName + " ] Its mandatory.");
    alert(fieldName)
    return false;
    }
  return true;  
}

function CheckComboSelNoMSG(list){
	var lval = list.selectedIndex;
	if (lval==0)
	{
	    return false;
    }
	return true;  
}

/*
This is how you invoke it - CheckMinSelect(CheckBox1, 4, 2); 

PS: The name of the Checkboxes have to be same...(It'll form an array)				
*/
function CheckMinSelect(ctrlName, heading, maxCount, minSelect) {
	var ctr=0;
	for(ictr=0; ictr <maxCount; ictr++) {
		if(document.forms[0].elements[ctrlName+ictr].checked) {
			ctr++;
		}
		
		if(ctr>=minSelect) return true;
	}
	//alert('You must select minimum one (1) of the choices available');
	alert(message("7"));
	return false;
}


function CheckSelected(ctrlname,min,max){
	var flag 
	var counter
	flag = false
	for(var counter=min;counter<=max;counter++){		
		if (document.forms[0].elements[ctrlname+counter].checked){
			flag = true;						
			break;
			}		
		}	
		if (flag == false){		
		//alert("Please select atleast one record");
		alert(message("7"));
		return false;
		}
		else{
		return true;
		}	
	}

/*
This is how you invoke it - CheckMaxSelect(CheckBox1, 4, 2); 

PS: The name of the Checkboxes have to be same...(It'll form an array)				
*/
function CheckMaxSelect(ctrlName, heading, maxCount, maxSelect) {
	var ctr=0;
	
	for(ictr=0; ictr <maxCount; ictr++) {
		
		if(document.forms[0].elements[ctrlName+ictr].Checked==true) {
			ctr++;
		}
		
		if(ctr>maxSelect) 
		{
			//alert('Maximum '+maxSelect+ ' Checkbox can be selected under the head "'+heading+'"');
			//return false;
		}
	}
	return true;
}


function CheckNumeric(tbox, fieldName) 
{
    var userValue=Trim(tbox.value); 
	var first = -1;
	var last = -1;
	if(userValue != "") 
	 {	str = new String(userValue)
		len = str.length
	    for (ictr=0;ictr<=len;ictr++)
		 {	val = str.charCodeAt(ictr);
			if (((val < 48) || (val > 57)))
			{	//Can consist of numbers only
				//alert(fieldName + " " + message("0"));
				//tbox.focus();
				return false;
		     }
		  }
	   }
	return true;
}

function CheckNumericDec(tbox, fieldName) 
{
    var userValue=Trim(tbox.value); 
	var first = -1;
	var last = -1;
	var decimals;
	decimals=0;
	var numValue
	numValue = parseInt(userValue)
	if(userValue==""){
		alert( message("1") + " " + fieldName + "." + message("2"))		
		tbox.focus();
		return false;		
	}			
	if(userValue != "") 
	 {	str = new String(userValue)
		len = str.length
		for (ictr=0;ictr<=len;ictr++)
		 {	val = str.charCodeAt(ictr);
			if (((val < 48) || (val > 57)))
			{	
				if (val != 46)
				{
					//Can consist of numbers only
					alert(fieldName + " " + message("0"));
					tbox.focus();
					return false;
				}
				else
				{
				 decimals++;
				}
		     }
		  }
		if(len==1 && decimals==1)
		{
			//Can not contain just a decimal
			alert(fieldName + " " + message("0"));
			tbox.focus();
			return false;
		}
	   }
	   
	   if (decimals>1) 
	   {	//only one decimal allowed
		    alert(fieldName + " " + message("0"));
			tbox.focus();
			return false;
	   }
//	   if(numValue==0){
//			alert(fieldName + " " + message("20"))		
//			tbox.focus();
//			return false;
//		}	
//		if(numValue>99999.99){
//			alert(fieldName + " " + message("21"))		
//			tbox.focus();
//			return false;
//		}
	return true;
}

function CheckNumericNoMSG(fieldValue) 
{
    var userValue=Trim(fieldValue); 
	var first = -1;
	var last = -1;
	if(userValue != "") 
	 {	str = new String(userValue)
		len = str.length
	    for (ictr=0;ictr<=len;ictr++)
		 {	val = str.charCodeAt(ictr);
			if (((val < 48) || (val > 57)))
			{ return false;
		     }
		  }
	   }
	return true;
}

//Trimming the space
function Trim(fieldValue) {
	fieldValue += "";
	if(fieldValue!="") {
		var pnLen = fieldValue.length;
		var pnStart=0, pnStop=0;
		var ictr;

		// Traverse through the String to find first Nonspace character.
		for(ictr=0;ictr<pnLen;ictr++) { 
			// If Non Space is found then exit out of the loop.
			
			if(fieldValue.substr(ictr,1) != " " && fieldValue.substr(ictr,1) != '\n')
				break;
		}
		pnStart = ictr;

		// Traverse through the String to find last Nonspace character.
		for(ictr=pnLen-1;ictr > pnStart;ictr--) {
			// If Non Space is found then exit out of the loop.
			if(fieldValue.substr(ictr,1) != " " && fieldValue.substr(ictr,1) != '\n')
				break;
		}
		pnStop = ictr + 1;
		return(fieldValue.substring(pnStart,pnStop));
	} else {
		return "";
	}
}

	/*
	Parameters :
		1. cboObject : Object Reference
		2. strValue : The &#65533; and &#65533; delimited string
		3. sDefaultValue : Can be All or Select [value will be ""]
		4. sSelectedValue	:	Will display the value in the combo box as selected
	*/


	function PopCombo(cboObject, strValue, sDefaultValue, sSelValue) {
	
	
		var sSelectedValue;
		sSelectedValue = Trim(sSelValue);
		var strRecords;
		var arrRecords;
		var arrFields;
		var iCtr;
		var iRow = 0;
		strRecords = strValue;
		//strRecords = strRecords.substring(1, strRecords.length - 1);
		arrRecords = strRecords.split("&#65533;");
		if(sDefaultValue != "") {
			cboObject.options[0] = new Option();
			cboObject.options[0].value='';
			cboObject.options[0].text = sDefaultValue;
			cboObject.options[0].selected=true;
			iRow = 1;
		}
		
		for (iCtr=0; iCtr<arrRecords.length; iCtr++) {
			arrFields = arrRecords[iCtr].split("&#65533;");
			
			if(arrFields[0]==sSelectedValue) {
				cboObject.options[iRow] = new Option();
				cboObject.options[iRow].value=arrFields[0];
				cboObject.options[iRow].text=arrFields[1];
				cboObject.options[iRow].selected=true;
			} else {
				cboObject.options[iRow] = new Option();
				cboObject.options[iRow].value=arrFields[0];
				cboObject.options[iRow].text=arrFields[1];
				
			}
			iRow++;
		}
	}


/*
	Parameters :
		1. cboObject : Object Reference
		2. strValue : The &#65533; and &#65533; delimited string
		3. sDefaultValue : Can be All or Select [value will be ""]
		4. sSelectedValue	:	Will display the value in the combo box as selected
		5. sParentValue : Value on whose basis combo will be populated
	*/

	function PopComboValue(cboObject, strValue, sDefaultValue, sSelectedValue,sParentValue) {
		var strRecords;
		var arrRecords;
		var arrFields;
		var iCtr;
		var iRow = 0;
		cboObject.options.length = 0;
		strRecords = strValue;
		//alert(strValue)
		arrRecords = strRecords.split("&#65533;");
		if(sDefaultValue != "") {
			cboObject.options[0] = new Option();
			cboObject.options[0].value='';
			cboObject.options[0].text = sDefaultValue;
			cboObject.options[0].selected=true;
			iRow = 1;
		}
		
		for (iCtr=0; iCtr<arrRecords.length; iCtr++) {
			arrFields = arrRecords[iCtr].split("&#65533;");
			if (Left(arrFields[0], 5) == sParentValue) { 
				if(arrFields[0]==sSelectedValue) {
					cboObject.options[iRow] = new Option();
					cboObject.options[iRow].value=arrFields[0];
					cboObject.options[iRow].text=arrFields[1];
					cboObject.options[iRow].selected=true;
				} else {
					cboObject.options[iRow] = new Option();
					cboObject.options[iRow].value=arrFields[0];
					cboObject.options[iRow].text=arrFields[1];
				}
				iRow++;
			}
		}
	}
	

	/*
	Parameters :
		1. cboObject : Object Reference
		2. strValue : The &#65533; and &#65533; delimited string
		3. sDefaultValue : Can be All or Select [value will be ""]
		4. sSelectedValue	:	Will display the value in the combo box as selected
	*/

	function PopParentCombo(cboObject, strValue, sDefaultValue, sSelValue) {
		var sSelectedValue;
		sSelectedValue = Trim(sSelValue);
		var strRecords;
		var arrRecords;
		var arrFields;
		var iCtr;
		var iRow = 0;
		cboObject.options.length = 0;
		strRecords = strValue;
		arrRecords = strRecords.split("&#65533;");
		if(sDefaultValue != "") {
			cboObject.options[0] = new Option();
			cboObject.options[0].value='';
			cboObject.options[0].text = sDefaultValue;
			cboObject.options[0].selected=true;
			iRow = 1;
		}
		
		for (iCtr=0; iCtr<arrRecords.length; iCtr++) {
		
			arrFields = arrRecords[iCtr].split("&#65533;");
			if (Trim(parseInt(arrFields[2]))==0) {
				if(arrFields[0]==sSelectedValue) {
					cboObject.options[iRow] = new Option();
					cboObject.options[iRow].value=arrFields[0];
					cboObject.options[iRow].text=arrFields[1];
					cboObject.options[iRow].selected=true;
				} else {
					cboObject.options[iRow] = new Option();
					cboObject.options[iRow].value=arrFields[0];
					cboObject.options[iRow].text=arrFields[1];
				}
				iRow++;
			}
		}
	}

/*	PopChildCombo:
	Parameters :
		1. cmbObject : Object Reference
		2. strValue : The &#65533; and &#65533; delimited string [ChildID&#65533;ChildName&#65533;ParentID&#65533;]
		3. sParentID : The ParentID for which Child has to be populated
		4. sDefaultValue : Can be All or Select [value will be ""]
		5. sSelectedValue	:	Will display the value in the combo box as selected
	*/

	function PopChildCombo(cmbObject, strValue, sParentID, sDefaultValue, sSelectedValue) {
		var strRecords;
		var arrRecords;
		var arrFields;
		var iCtr;
		var iSubRow = 0;
		cmbObject.options.length = 0;
		strRecords = strValue;
		arrRecords = strRecords.split("&#65533;");
		if(sDefaultValue != "") {
			cmbObject.options[0] = new Option();
			cmbObject.options[0].value='';
			cmbObject.options[0].text = sDefaultValue;
			cmbObject.options[0].selected=true;
			iSubRow = 1;
		}
		for (iCtr=0; iCtr<arrRecords.length; iCtr++) {
			arrFields = arrRecords[iCtr].split("&#65533;");
			if(sParentID == Trim(arrFields[2])){
				if(arrFields[0]==sSelectedValue) {
					cmbObject.options[iSubRow] = new Option();
					cmbObject.options[iSubRow].value=arrFields[0];
					cmbObject.options[iSubRow].text=arrFields[1];
					cmbObject.options[iSubRow].selected=true;
				} else {
					cmbObject.options[iSubRow] = new Option();
					cmbObject.options[iSubRow].value=arrFields[0];
					cmbObject.options[iSubRow].text=arrFields[1];
				}
				iSubRow++;
			}
		}
	}




