/****************************************************************************************
	Functionality : To trim the text and it is attached with the String object
	Author 	      : Prakash Prabhu
	Parameter     : None
	Return value  : None
*****************************************************************************************/
String.prototype.trim=fnTrim;
function fnTrim()
{
	for(intLeftCounter=0; intLeftCounter<this.length; intLeftCounter++)
		if(this.charAt(intLeftCounter)!=" ")break;
	for(intRightCounter=this.length-1; intRightCounter>=0; intRightCounter--)
		if(this.charAt(intRightCounter)!=" ")break;		
	if(intLeftCounter==this.length)return "";	
	return this.substring(intLeftCounter,intRightCounter+1);
}
/****************************************************************************************
	Functionality : To get the date in DD/MM/YYYY format
	Author 	      : Prakash Prabhu
	Parameter     : None
	Return value  : Daet in DD/MM/YYYY format
*****************************************************************************************/
String.prototype.getDateInDDMMFormat=_getDateInDDMMFormat;
function _getDateInDDMMFormat()
{	
	if(this.trim() == "" || this.indexOf("/") == -1 || (this.indexOf("/") == this.lastIndexOf("/")) )return this;
	arDate = this.split("/");
	return ((arDate.length != 3)?"":(arDate[1] + "/" + arDate[0] + "/" + arDate[2]) )
}
/****************************************************************************************
	Functionality : To get the date in MMDD//YYYY format
	Author 	      : Prakash Prabhu
	Modified by   : Prakash Prabhu
	Parameter     : None
	Return value  : Daet in MM/DD/YYYY format
*****************************************************************************************/
String.prototype.getDateInMMDDFormat=_getDateInMMDDFormat;
function _getDateInMMDDFormat()
{	
	if(this.trim() == "" || this.indexOf("/") == -1 || (this.indexOf("/") == this.lastIndexOf("/")) )return this;
	arDate = this.split("/");
	return ((arDate.length != 3)?"":(arDate[0] + "/" + arDate[1] + "/" + arDate[2]) )
}
/****************************************************************************************
	Functionality : Extracts the file name from the given path.
	Author 	      : Prakash Prabhu
	Parameter     : strPath - Path
	Return value  : None
*****************************************************************************************/
function fnGetFileName(strPath)
{	
	if(strPath.trim() == "") return "";
	if(strPath.indexOf("\\") == -1) return "";
	arFileName = strPath.split("\\");
	return  arFileName[arFileName.length-1];		
}
/****************************************************************************************
	Functionality : Returns the ids of the selected records
	Author 	      : Prakash Prabhu
	Parameter     : None
	Return value  : comma seperated ids
*****************************************************************************************/
function fnGetSelectedCategories()
{
	if(!document.forms(0).chkDelete)return;
	if(!document.forms(0).chkDelete.length)
		return (!document.forms(0).chkDelete.disabled && document.forms(0).chkDelete.checked)?document.forms(0).chkDelete.value.trim():"";
	strSelectedCategories="";
	for(intCounter=0;intCounter<document.forms(0).chkDelete.length;intCounter++)
		if(document.forms(0).chkDelete[intCounter].checked)
			strSelectedCategories += document.forms(0).chkDelete[intCounter].value.trim()+",";
	if(strSelectedCategories.trim()!="")
		return strSelectedCategories.substring(0,strSelectedCategories.length-1);
	else
		return "";
}
/****************************************************************************************
	Functionality : Checks/unchecks all the checkboxes.
	Author 	      : Prakash Prabhu
	Parameter     : None
	Return value  : None
*****************************************************************************************/
function fnCheckAllCheckBoxes(objElement)
{
	if(!document.forms(0).chkDelete)return;
	if(!document.forms(0).chkDelete.length)
	{
		if(!document.forms(0).chkDelete.disabled)
			document.forms(0).chkDelete.checked=objElement.checked;
		return;
	}
	for(intCounter=0;intCounter<document.forms(0).chkDelete.length;intCounter++)
		if(!document.forms(0).chkDelete[intCounter].disabled)
			document.forms(0).chkDelete[intCounter].checked=objElement.checked;
}
/****************************************************************************************
	Functionality : Validate and calls the loadrecords function
	Author 	      : Prakash Prabhu
	Parameter     : object of the pagination text box.
	Return value  : None
*****************************************************************************************/
function fnPaginationGo(intTotalPages,intItemsPerPage)
{
	if(document.frmPagination.txtPagination.value == "")
	{
		alert("Please enter the Page Number");
		document.frmPagination.txtPagination.focus();
		return false;
	}
	if(!fnIsValidNumber(document.frmPagination.txtPagination.value))
	{
		alert("Please enter a valid Page Number");
		document.frmPagination.txtPagination.focus();
		return false;
	}
	if(parseInt(document.frmPagination.txtPagination.value) > parseInt(intTotalPages))
	{
		alert("Please enter a Page Number between 1 and " + intTotalPages);
		document.frmPagination.txtPagination.focus();
		return false;
	}
	fnLoadRecords((parseInt(document.frmPagination.txtPagination.value) -1) * parseInt(intItemsPerPage));	
}

function fnLoadRecords(intRecordNumber)
{
	document.mainForm.hidStartingRecord.value = intRecordNumber;
	document.mainForm.submit();
}
/****************************************************************************************
	Functionality : Validate the given data.
	Author 	      : Prakash Prabhu
	Parameter     : strValue - value to validate
	Return value  : true/false
*****************************************************************************************/
function fnIsValidNumber(strValue)
{
     if((isNaN(parseInt(strValue)) || (parseInt(strValue).toString().length != strValue.length)))
		return false;
	else
		return (parseInt(strValue) <= 0)?false:true;
}
/****************************************************************************************
	Functionality : Submit the given url and returns the value.
	Author 	      : Prakash Prabhu
	Parameter     : Query string to submit
	Return value  : Value returned by the commonvalidations.asp
*****************************************************************************************/
function fnGetXMLValue(strQueryString)
{
   strUrl = "CommonValidations.asp?" + strQueryString;
   objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
   objXMLHTTP.open("GET",strUrl,false)   
   objXMLHTTP.send();
			
   if(objXMLHTTP.responseXML && objXMLHTTP.responseXML.xml && objXMLHTTP.responseXML.documentElement)
   		return objXMLHTTP.responseXML.documentElement.text;
   else
   		return "Error";
}
/****************************************************************************************
	Functionality : Validate the given email id
	Author 	      : Prakash Prabhu
	Parameter     : None
	Return value  : True/false
*****************************************************************************************/
function fnIsValidEmailAddress(strEmailId)
{
     return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmailId))
}
/****************************************************************************************
	Functionality : Displays the copyright message of the SAKATA website.
	Author 	      : Prakash Prabhu
	Parameter     : None
	Return value  : None;
*****************************************************************************************/ 
function fnGetCopyRight()
{
	if(window.top && top.opener && top.opener.strYear)
		strCopyRightYear = top.opener.strYear;
	else
		strCopyRightYear = (new Date()).getFullYear();
		
	document.write("© " + strCopyRightYear + " SAKATA SEED AMERICA, INC All rights reserved.");
}



/************************************************
	Functionality : Validates that a string a United States zip code in 5 digit format or zip+4 format. 99999 or 99999-9999
	Author 	      : Prakash Prabhu
	Parameter     : strValue - String to be tested for validity
	Return value  : True/false
*************************************************/
function validateUSZip( strValue ) {
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
 
  //check for valid US Zipcode
  return objRegExp.test(strValue);
}


/************************************************
	Functionality : Validates that a string contains valid
					US phone pattern. 
					Ex. (999) 999-9999 or (999)999-9999
	Author 	      : Prakash Prabhu
	Parameter     : strValue - String to be tested for validity
	Return value  : True/false

*************************************************/
function validateUSPhone( strValue ) {
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
 
  //check for valid us phone with or without space between 
  //area code
  return objRegExp.test(strValue); 
}

/******************************************************************************
	Functionality : Validates that a string contains only valid numbers.
	Author 	      : Prakash Prabhu
	Parameter     : strValue - String to be tested for validity
	Return value  : True/false
******************************************************************************/
function  validateNumeric( strValue ) {  
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
  //check for numeric characters 
  return objRegExp.test(strValue);
}


/************************************************
	Functionality : Validates that a string contains only valid integer number.
	Author 	      : Prakash Prabhu
	Parameter     : strValue - String to be tested for validity
	Return value  : True/false
******************************************************************************/
function validateInteger( strValue ) {  
  var objRegExp  = /(^-?\d\d*$)/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}


/************************************************
	Functionality : Validates that a string is not all blank (whitespace) characters.
	Author 	      : Prakash Prabhu
	Parameter     : strValue - String to be tested for validity
	Return value  : True/false
*************************************************/
function validateNotEmpty( strValue ) {
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }  
   return false;
}

/************************************************
	Functionality : Removes leading and trailing spaces.
	Author 	      : Prakash Prabhu
	Parameter     : strValue - Source string from which spaces will be removed
	Return value  : Source string with whitespaces removed.
*************************************************/
function trimAll( strValue ) { 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}


/************************************************
	Functionality : Trims trailing whitespace chars.
	Author 	      : Prakash Prabhu
	Parameter     : strValue - String to be trimmed.
	Return value  : Source string with right whitespaces removed.
*************************************************/
function rightTrim( strValue ) {
var objRegExp = /^([\w\W]*)(\b\s*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

/************************************************
	Functionality : Trims leading whitespace chars.
	Author 	      : Prakash Prabhu
	Parameter     : strValue - String to be trimmed.
	Return value  : Source string with left whitespaces removed.
*************************************************/
function leftTrim( strValue ) {
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

/************************************************
	Functionality : Checks if from date is greater than to date
	Author 	      : Prakash Prabhu
	Parameter     : from - from date textbox name
				  : to - to date textbox name
				  : fromLabel - the from date which needs to be displayed on error
				  : toLabel - the to date which needs to be displayed on error
	Return value  : alerts in case of error and returns false else returns true
*************************************************/
function doDateCheck(from, to, fromLabel, toLabel) {
	if (!validateNotEmpty(from.value)){
		alert ("Please enter the " + fromLabel + ".");
		from.focus();
		return false;
		}
	if (!validateNotEmpty(to.value)){
		alert ("Please enter the " + toLabel + ".");
		to.focus();
		return false;
		}		
	if (Date.parse(from.value) > Date.parse(to.value)) {
		alert(fromLabel+" must occur before the "+toLabel);
		return false;
	}
	else
	{
		return true;
	}
}
/************************************************
	Functionality : Checks if from date is greater than to date
	Author 	      : Prakash Prabhu
	Parameter     : strValue - The name of the RTF element.
	Return value  : True if the mandatory of the RTF textarea is filled otherwise False.
*************************************************/
function trimRTF(strValue){
	editor_filterOutput(strValue);
//	regExp=/<p>|<\/p>|&nbsp;/gi;
	regExp=/<[^>]*>|&nbsp;/gi;
	if(!validateNotEmpty((document.mainForm[strValue].value).replace(regExp, "")) || !validateNotEmpty((document.all[strValue].value).replace(regExp, "")))return false;
	return true;
}


/************************************************
	Functionality : Validates that a string contains only 
					valid dates with 2 digit month, 2 digit day, 
					4 digit year. Date separator can be ., -, or /.
					Uses combination of regular expressions and 
					string parsing to validate date.
					Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
	Author 	      : Prakash Prabhu
	Parameter     : strValue - String to be tested for validity
	Return value  : True if valid, otherwise false.
	
*************************************************/

function validateUSDate( strValue ) {
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = parseInt(arrayDate[1],10); 
	var intYear = parseInt(arrayDate[2],10);
    var intMonth = parseInt(arrayDate[0],10);

	//Check for valid year --it should be greater than 1900
	if (intYear < 1900){
		return false;
	}	
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return false;
	}
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
		
    //check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
  }
  return false; //any other values, bad date
}
function fnValidateSearch()
{
	if(document.mainForm.txtSearch.value == "")
	{
		alert("Please enter the text to search");
		document.mainForm.txtSearch.focus();
		return false;
	}
	document.mainForm.hidStartingRecord.value = "0";
	document.mainForm.hidSearch.value = document.mainForm.txtSearch.value;
	return true;
}
function fnSearch(strSearchType)
{
	if(fnValidateSearch())
	{
		if(strSearchType && strSearchType != "")
			document.mainForm.hidSearchType.value = strSearchType;
		document.mainForm.action="SearchCatalog.asp"
		document.mainForm.submit();
	}
}
function fnKeyPress(e,strSearchType)
{    
	var charCode = e.keyCode
	if (charCode == 13)
	{
		event.returnValue=0;
		event.cancelBubble=false;
		fnSearch(strSearchType);
		return;
	}		

}
