//This function is used to check whether the string contains all the integers only.
//It returns
//true : If string contains all numeric characters
//false: Otherwise
//Parameters: //1) String to be check
//


//Compare FromToDate
// Pass lvFromDate,lvToDate as string in "dd/MM/yyyy" format.
function isValidFromToDate(FromDate,ToDate)
{
	var lvFromdate = new Date(FromDate.substring(6,10),
                            FromDate.substring(3,5)-1,
                            FromDate.substring(0,2));
	var lvTodate = new Date(ToDate.substring(6,10),
                            ToDate.substring(3,5)-1,
                            ToDate.substring(0,2));
	if (lvFromdate > lvTodate)
		return false;
	else
		return true;
}
function CheckDigit(InwardNo)
		{	
			
			var str=new String(InwardNo)
			var index=str.lastIndexOf('/')
			var checkstr=str.substr(index+1) 
			var i
			var Inwstring=str.substring(0,index)
			var InwardArray
			var newstr=""
			var newString = ""; 
			var counter = str.length; 
			var count=counter;
			var V_TOTAL_SUM=0;
			var V_WEIGHT=1;
			var V_COUNTER=1;
			var V_DIGIT,V_PRODUCT;
			var cdchar=checkstr.charAt(1)
			var V_REMAINDER
			
			if(Inwstring.indexOf('/')==-1)
			{
				
				//if(Inwstring.indexOf('-')!=-1)
					InwardArray=Inwstring.split("-");
				//else
					
						
			}
			else
			{
			
				InwardArray=Inwstring.split("/");
			}
			
			for(i=0;i<InwardArray.length;i++)
				newstr=newstr+InwardArray[i]
			newString=cdchar+newstr;
			
			for(V_COUNTER=1;V_COUNTER<=newString.length ;V_COUNTER++)
			{
				V_DIGIT=newString.substr(V_COUNTER-1,1);
					
				V_PRODUCT=(V_DIGIT) * (V_COUNTER);
				
				V_TOTAL_SUM=V_TOTAL_SUM + V_PRODUCT;
			}
			
		
		V_REMAINDER=V_TOTAL_SUM%parseInt(checkstr.charAt(0));
		
		return V_REMAINDER
		}

function ValidatePhoneNoBlur(Texts)
{
	var i= new String();
	var str=new String();
	str=Texts.value;
	
	var cnt;
	for(cnt=0;cnt< str.length;cnt++)
	{	
		i=str.substring(cnt,1);	
		if(isNaN(i))
		{
			if (i=="-" || i=="+"   )
			{
				return;
			}
			alert("Please Enter Valid Format +99-99-99999999");
			Texts.value="";
			Texts.focus();
			return;
		}
	}
}


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 isString(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") 
    {
    }
		else
    {
		return false;
    } 
  }
  //all characters are numbers.
  return true;
}

function CheckString(Texts)
{
	var str=new String();
	str=Texts.value;
	
	if (isString(str))
	{
	}
	else
	{
		alert("Please Enter Valid Text Value");	
		Texts.value="";
		Texts.focus();
		return false;
	}
}

function CheckStringBlur(Texts)
{
	var str=new String();
	str=Texts.value;
	
	if (isString(str))
	{
	}
	else
	{
		alert("Please Enter Valid Text Value");	
		Texts.value="";
		Texts.focus();
		return false;
	}
}

//called onblur evevnt
function ValidateCurrencyBlur(Texts)
{	
	if(isNaN(Texts.value))
	{
		alert("Please Enter Valid Amount");
		Texts.value="";
		Texts.focus();
		return false;
	}
	else
	{
		if (parseFloat(Texts.value) < 0)
		{
			alert("Please Enter Valid Amount");
			Texts.value="";
			Texts.focus();
			return false;									
		}
	}			
}

function ValidateNumberBlur(Texts)
{
	var str=new String();
	str=(Texts.value);
	//if(!isInteger(str))
	if(isNaN(Texts.value))
	{
		alert("Please Enter Valid Number");
		Texts.value="";
		Texts.focus();
		return false;
	}				
	return true;
}


//Date validations
//
//
//Date delimiter character
var dtCh= "/";
//Minimum year in the date that is allowed
var minYear=1800;
//Maximum year in the date that is allowed
var maxYear=2500;

//This function given the year, returns the no. of days in February month for that year.
//February has 29 days in any year evenly divisible by four except for centurial years that are not also divisible by 400.
//Parameters:
//1) Year
function daysInFebruary(year)
{
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

//This function given the month builds the month array from January till that month with no. of days in that month.
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;
		//february is taken as 29,it should be calculated in the isDate function
		//by calling the function DaysInFebruary  
		if(i==2) 
		  this[i] = 29;
   } 
   return this;
}


//This function strips the specific characters from the string and returns the resulting string.
//Parameters:
//1) String to be checked
//2) Characters in the string to be stripped
function stripCharsInBag(s, bag)
{
	var i;
  var returnString = "";
  for(i=0;i<s.length;i++)
  {   
    var c=s.charAt(i);
    if(bag.indexOf(c) == -1) 
      returnString += c;
  }
  return returnString;
}


//This is the common function called from the ValidateData function of the various pages to validate whether the field contains the valid date data. It displays an alert if it is not valid, sets the focus to that control and returns the false value to indicate the failure of validation.
//Note: 1) The blank value will be treated as not date and the validation will fail.
//If the field is nullable and date, donot check for date condition if it is not entered.
//2) This function will always alert the error.
//Parameters:
//Control supporting the value property
function CheckDateBlur(item)
{
  //blank exit 
  if(item.value=="")
  {
	return true;
  }		
  //if the control contains the valid date?
  if(!isDate(item.value))
  {
    //set the focus to the control
	  item.focus();
	  item.value="";
	  //return the failure code
	  return false;
	}
	//return the success code
	return true;
}
//This procedure is called from the CheckDate function to check if the given variable contains the valid date data. It expects the date to be passed in mm/dd/yyyy format. If the variable is not date, it alerts the appropriate error to the user and returns false else returns true value.
//There is no in-built function available in javascript for validating the date object.
//Parameters:
//1) String containing the object
function isDate(dtStr)
{
  //build the days array
	var daysInMonth=DaysArray(12);
	//find the first date delimiter
	var pos1=dtStr.indexOf(dtCh);
	//find the second date delimiter
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	//extract the day portion
	var strDay=dtStr.substring(0,pos1);
	//extract the month portion 
	var strMonth=dtStr.substring(pos1+1,pos2);
	//extract the year portion
	var strYear=dtStr.substring(pos2+1);
	
	strYr=strYear;
	//if the day contains the leading zero,trim it
	if(strDay.charAt(0)=="0" && strDay.length>1) 
	  strDay=strDay.substring(1);
	//if the month contains the leading zero,trim it  
	if(strMonth.charAt(0)=="0" && strMonth.length>1) 
	  strMonth=strMonth.substring(1);
	
	//if the year contains the leading zero,trim it  
  //Note : leading zeros are trimmed from day,month and year portion
  //because when converted to numeric using parseInt function,the variable
  //is treated as octal if it contains leading zeros
	for(var i=1; i<= 3;i++) 
	{
		if(strYr.charAt(0)=="0" && strYr.length>1) 
		  strYr=strYr.substring(1);
	}
	
	//convert the month portion to numeric
	month=parseInt(strMonth);
	//convert the date portion to numeric
	day=parseInt(strDay);
	//convert the year portion to numeric
	year=parseInt(strYr);
	
	//alert("day : "+day+" month "+month+" year "+year);
	//if the delimiter1 or delimiter2 is missing
	//alert the error to the front end
	if(pos1==-1 || pos2==-1)
	{
		alert("The date format should be : dd/mm/yyyy");
		return false;
	}

	//day validations :
	//must be in the range 1 to 31
	//if february,must be 28 or 29 depending upon the year
	//other than february,must be in the range of day stored in array corresponding to the month
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("Please enter a valid day");
		return false;
	}

  //the month should be in the range 1 to 12	
	if(strMonth.length<1 || month<1 || month>12)
	{
		alert("Please enter a valid month");
		return false;
	}
	
	//year validation :
	//length should be of 4 digit (2 digits not allowed)
	//it should be in the range of minYear and maxYear defined at the top
	if(strYear.length!= 4 || year==0 || year<minYear || year>maxYear)
	{
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	
	//the delimiter character should not be repeated after the position2 (2 delimiter only)
	//other than delimiter chars, all the remaining characters should be integer
	if(dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert("Please enter a valid date");
		return false;
	}
	//success
  return true;
}

//Trim Function.
//Added by Ahmed on 11 July 2005.
function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);

		if(TRIM_VALUE=="")
		{
			return "";
		}
		else
		{
			return TRIM_VALUE;
		}
}

//RTrim Function.
//Added by Ahmed on 11 July 2005.
function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";


		if(v_length < 0)
		{
			return"";
		}
		var iTemp = v_length -1;


		while(iTemp > -1)
		{
			if(VALUE.charAt(iTemp) == w_space){}


				else{
					strTemp = VALUE.substring(0,iTemp +1);
					break;
				}
				iTemp = iTemp-1;
		}
		return strTemp;
}

//LTrim Function.
//Added by Ahmed on 11 July 2005.
function LTrim(VALUE)
{
    var w_space = String.fromCharCode(32);

	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;


	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space){}


			else{
				strTemp = VALUE.substring(iTemp,v_length);
				break;
			}
			iTemp = iTemp + 1;
	}
	return strTemp;
}

//this fuction sets the popup window in the center of the 
//screen
function wopen(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, resizable=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}


function isAlphaNumeric(s)
{
  var i;
  for(i=0;i< s.value.length;i++)
  {   
    //check that current character is number.
    var c = s.value.charAt(i);
    if((c >= "0" && c <= "9") || (c >= "a" && c <= "z") || (c >= "A" && c <= "Z")) 
    {
    }
		else
    {
     	//alert();
     	//alert(s.value);
     	document.getElementById(s.id).value = "";
     	alert('Only AlphaNumeric Characters Allowed');
     	s.focus();
     	return false;
    } 
  }
  //all characters are numbers.
  return true;
}


/*

//
//
//end Date validation
//end Date validation
//end Date validation


//This is a common function called from various pages to handle the keypress event of the various controls to restrict the key input. 
//E.g. disabled, numeric, unsignednumeric, integer, unsignedinteger, and date.
//Note: the respective property of the control must be set before calling this function.
//Parameters: 1) Event object
var bNetscape;
//to get the current browser
bNetscape=isBrowserNetscape();
function validateKeyPress(e,typ)
{
  //to store the key being pressed
  var key;
  //to store the control triggering the event
  var target;
  
  //obtain the key and target depending upon the browser type : netscape or IE
	if(bNetscape)
	{
		key = e.which;
		target=e.target;	
	}
	// IE
	else 
	{
  	key = e.keyCode; 
		target=e.srcElement;
	}
	//test
	//test
	
	//return true;
	  if(!((key>=48 && key<=57) || (String.fromCharCode(key)==dtCh)))
		{
		e.srcElement.value = e.srcElement.value + key;
		key=0 ;	
	    return false;	
	    }
	//test
	//test
	
	
	//debug : print the key and target
	//alert("key "+key);
	//alert("target "+target);
	
	//if the disabled property is set,suppress the key
	if(target.disabled==true)
	{
	  //alert("disabled");
	  return(false);
	}
	
	//double validation  
	
	
	if(target.double==true) 
	{
	 
	  key=UsrKeyPressDouble(target.value,key);
	  if(key==0)
	    return(false);
	}
	//unsigned double validtion
	else if(target.unsigneddouble==true) 
	{
	  key=UsrKeyPressUnsignedDouble(target.value,key);
	  if(key==0)
	    return(false);
	}
	//long validation
	else if(target.long==true) 
	{
	  key=UsrKeyPressLong(target.value,key);
	  if(key==0)
	    return(false);
	}
	//unsigned long validation
	else if(target.unsignedlong==true) 
	{
	  key=UsrKeyPressUnsignedLong(target.value,key);
	  if(key==0)
	    return(false);
	}
	//date validation
	//else if(target.date==true) 
	else if(typ==1) 
	{
	  if(!((key>=48 && key<=57) || (String.fromCharCode(key)==dtCh)))
	    return(false);
	}
	return(true);
}

//This function is called from the usrKeyPress event procedure of the control to check whether the key being pressed contains the long value (with sign). It returns 0 in case of failure, keyAscii otherwise.
//Parameters:
//1) Value of control before change
//2) Key pressed
function UsrKeyPressLong(strValue,KeyAscii)
{
  //allow the special characters <32 and ==127
  if(!(KeyAscii<32 || KeyAscii==127))
  {
    //must be integers only
    if(!(KeyAscii>= 48 && KeyAscii<= 57))
    {
      //allow the minus sign if it is not already entererd (i.e. only one minus sign)
      if(String.fromCharCode(KeyAscii)=="-")
      {
        if(strValue.indexOf("-")>=0)
          return(0);
      }      
      else
        return(0);
    }
  }
  //alert(KeyAscii);
  return(KeyAscii);
}

//This function is called from the usrKeyPress event procedure of the control to check whether the key being pressed contains the long value(unsigned). It returns 0 in case of failure, keyAscii otherwise.
//Parameters:
//1) Value of control before change
//2) Key being pressed
function UsrKeyPressUnsignedLong(strValue,KeyAscii)
{
  //allow the control keys
  if(!(KeyAscii<32 || KeyAscii==127))
  {
    //must be integers only
    if(!(KeyAscii>= 48 && KeyAscii<= 57))
      return(0);
  }
  //alert(KeyAscii);
  return(KeyAscii);
}

//This function is called from the usrKeyPress event procedure of the control to check whether the key being pressed contains the double value(with sign). It returns 0 in case of failure, keyAscii otherwise.
//Parameters:
//1) Value of control before change
//2) Key being pressed
function UsrKeyPressDouble(strValue,KeyAscii)
{
  //allow control keys
  if(!(KeyAscii<32 || KeyAscii==127))
  {
    //must be integers only 
    if(!(KeyAscii>= 48 && KeyAscii<= 57))
    {
      //allow one decimal
      if(String.fromCharCode(KeyAscii)==".")
      {
        if(strValue.indexOf(".")>=0)
          return(0);
      }
      //allow one minus sign
      else if(String.fromCharCode(KeyAscii)=="-")
      {
        if(strValue.indexOf("-")>=0)
          return(0);
      }      
      else
        return(0);
    }
  }
  //alert(KeyAscii);
  return(KeyAscii);
}

//This function is called from the usrKeyPress event procedure of the control to check whether the key being pressed contains the double value(unsigned). It returns 0 in case of failure, keyAscii otherwise.
//Parameters:
//1) Value of control before change
//2) Key being pressed
function UsrKeyPressUnsignedDouble(strValue,KeyAscii)
{
  //allow control keys
  if(!(KeyAscii<32 || KeyAscii==127))
  {
    //must be integers only 
    if(!(KeyAscii>= 48 && KeyAscii<= 57))
    {
      //allow only one decimal character
      if(String.fromCharCode(KeyAscii)==".")
      {
        if(strValue.indexOf(".")>=0)
          return(0);
      }
      else
        return(0);
    }
  }
  //alert(KeyAscii);
  return(KeyAscii);
}



*/