/* **************************************************************************
BEGIN Validate functions
***************************************************************************** */

/***************************************************************************

Function              IsUrl()

Description           To verify for a valid homepage

****************************************************************************/
function IsUrl(theField)
{

    var theInput = theField.value;
    var theLength = theInput.length;
    var count;
    var countval=0;
    for(var i=0;i<theLength;i++)
    {
      var theChar = theInput.substring(i,i+1);
      if(theChar == ".")
      {
        var countval = countval+1 ;
      }
      else if ((theChar >="0" && theChar <="9") ||
            (theChar >="A" && theChar <="Z") ||
            (theChar >="a" && theChar <="z") || (theChar == "-") ||
            (theChar == "_") || (theChar == " ") || (theChar == "/") ||
            (theChar == ":") || (theChar == ".") || (theChar == "&") ||
            (theChar == "?") || (theChar == "=") || (theChar == "~") )
      {
      }
      else
      {
       return false;
      }
      count = countval;
    }

    if(count<2)
    {
      return false;
    }

    var str = theInput.substring(0,7);
    if(str == "http://")
    {
    }
    else
    {
      return false;
    }
    return true;
}

/* ***************************************************************************

Function Name          fnIsValidUrl()
Description            Checks if the value of the input parameter (field) is
                       a valid url (starts with http://). Returns false on error
                       else returns true.

**************************************************************************** */
function fnIsValidUrl(lvatheField)
{

  var lvatheInput = lvatheField.value;
  var lvatheLength = lvatheInput.length;
  var lvacount;
  var countval=0;
  for(var i=0;i<lvatheLength;i++)
  {
        var lvatheChar = lvatheInput.substring(i,i+1);
    if(lvatheChar == ".")
    {
        var lvacountval = lvacountval+1 ;

    }
    else
    if ((lvatheChar >="0" && lvatheChar <="9") || (lvatheChar >="A" && lvatheChar <="Z") || (lvatheChar >="a" && lvatheChar <="z") || (lvatheChar == "-") || (lvatheChar == "_") || (lvatheChar == " ") ||(lvatheChar == "/") ||(lvatheChar == ":") || (lvatheChar == ".")  || (lvatheChar == "&") || (lvatheChar == "?") || (lvatheChar == "="))
    {
    }
    else
    {

      return false;
    }
    lvacount = lvacountval;
  }


  if(lvacount<2)
  {

    return false;
  }

  var lvastr = lvatheInput.substring(0,7);

  if(lvastr == "http://")
  {

  }
  else
  {

    return false;
  }

  return true;
}


/**
  * Validate that the given string contains only digits
  */
function validateDigits(str)
{
  var digits = "0123456789";
	var temp;
	for(var i =0;i<str.length;i++)
	{
		temp = str.substring(i, i+1);
		if (digits.indexOf(temp) == -1)
		{
			return false;
		}
	}
	return true;
}

/** Function kept for backwards compatibility, use validateEmail instead */
function fnvalidate_email(theInput)
{
  return validateEmail(theInput);
}

function validate_email(theInput)
{
  return validateEmail(theInput);
}
/* *********************** Function for validating email **************** */
function validateEmail(theObj)
{
  var lvaemailLength = theObj.value.length;
  if (lvaemailLength == 0)
  {
     return true;
  }

  if (lvaemailLength < 8)
  {
     fnReportError(600);
     theObj.focus();
     return false;
  }

  var lvaemailText = theObj.value;
  if ((lvaemailText.indexOf("@") == -1) || (lvaemailText.indexOf(".") == -1) )
  {
     fnReportError(601);
     theObj.focus();
     return false;
  }

  if( (lvaemailText.substring(lvaemailText.indexOf("@")+1,lvaemailLength)).indexOf("@") != -1)
  {
     fnReportError(602);
     theObj.focus();
     return false;
  }

  // 2005.02, Vass.fix: email now only needs one char before the @ sign
  //if (lvaemailText.indexOf("@") < 2)
  if (lvaemailText.indexOf("@") < 1)
  {
     fnReportError(603);
     theObj.focus();
     return false;
  }

  if ((lvaemailText.lastIndexOf(".") - lvaemailText.indexOf("@")) < 3)
  {
     fnReportError(604);
     theObj.focus();
     return false;
  }

  if ((lvaemailLength - lvaemailText.lastIndexOf(".")) < 3)
  {
     fnReportError(605);
     theObj.focus();
     return false;
  }

    // Kenneth 2006-01-27
    if (lvaemailText.indexOf(" ") > -1) {
		alert("validateEmail contains space 3303");
		fnReportError(3303); // No spaces allowed in email
		theObj.focus();
		return false;
    }

    // Kenneth 2006-01-27
    if (lvaemailText.indexOf(":") > -1) {
		alert("validateEmail contains : 3302");
		fnReportError(3302); // No colon allowed in email
		theObj.focus();
		return false;
    }

  if (lvaemailText.indexOf("\\") > -1)
  {
     fnReportError(606);
     theObj.focus();
     return false;
  }
  if (lvaemailText.indexOf("/") > -1)
  {
     fnReportError(607);
     theObj.focus();
     return false;
  }
  if (lvaemailText.indexOf("'") > -1)
  {
    fnReportError(608);
    theObj.focus();
    return false;
  }

  if (lvaemailText.indexOf("!") > -1)
  {
     fnReportError(609);
     theObj.focus();
     return false;
  }

  if (lvaemailText.indexOf("zaz.com.br") > -1)
    {
     fnReportError(340);
      theObj.focus();
      return false;
    }

   if ( (lvaemailText.indexOf(",") > -1) || (lvaemailText.indexOf(";") > -1) )
    {
     fnReportError(342);
      theObj.focus();
      return false;
    }

    if (lvaemailText.indexOf("#") > -1)
     {
        fnReportError(423);
        theObj.focus();
        return false;
     }
     if (lvaemailText.indexOf("%") > -1)
     {
        fnReportError(424);
        theObj.focus();
        return false;
     }
     if (lvaemailText.indexOf("&") > -1)
     {
        fnReportError(425);
        theObj.focus();
        return false;
     }
     if (lvaemailText.indexOf("^") > -1)
     {
        fnReportError(426);
        theObj.focus();
        return false;
     }
  return true;
}

function validatePN(orgno)
{
  var fOk = false;
//  var re  = /^(\d{6})(?:[+]|[-])?(\d{4})$/gi;
  var re  = /^(\d{10})$/gi;
  var m   = re.exec(orgno);

  if(m)
  {
//    var s = m[1]+m[2];
    var s = m[1];
    var sum=0, add , nsu;

    for( var i=0; i < 9; i++ )
    {
        add = parseInt(s.charAt(i),10) * (2-(i%2));
        sum += parseInt(add/10,10) + (add % 10);
    }

    nsu = ( 0 < sum % 10 ) ? (parseInt(sum/10,10)+1)*10 : sum;

    if( (nsu - sum) == parseInt(s.charAt(9),10) ) fOk=true;
  }

  return fOk;
}

/**************************************************************************************************
Function      isEmpty()

Description   Checks if the field passed contains some interesting value,
              returns true if it does, false otherwise
**************************************************************************************************/
function isEmpty(theField) {
  var theInput = theField.value;
  if (theInput == "") {
    return false;
  }//end of if
  return true;
}//end of function isEmpty()

/**************************************************************************************************
Function      isAlphabetic()
Description   To Check if the field passed contains only alphabets, Returns True or False
***************************************************************************************************/
function isAlphabetic(theField){

  //this code was modified by Rishi on 15Feb2001 to suit the Swedish character set
  // 2005.01, Vass.update: we should now also allow german chars like ü
  // 20061101 Rikard: Fixed this so it actually works. :)
  var valid = 'ABCDEFGHIJKLMNÑOÓÒPQRSTUÜVWXYZÅÄÆÖabcdefghijklmnñoóòpqrstuüvwxyzåäæö&- ';
  //alert(theField.value+' '+theField.value.length);
  for(var i = 0; i < theField.value.length; i++){
    var c = theField.value.charAt(i);
    //alert(c+': '+valid.indexOf(c));
    if(valid.indexOf(c) == -1 ) {
      return false;
    }//end of if
  }//end of for
  return true;
} //end of function isAlphabetic()

/* ***************************************************************************
Function Name          fnIsAlpha()
Description            Checks if the value of the input parameter (field) is
                       alphabetic. Returns false on error else returns true.
**************************************************************************** */
function fnIsAlpha(theField)
{
  return isAlphabetic(theField);
	var theInput = theField.value;
  	if (theInput == "")
    		return true;
  	var theLength = theInput.length;
  	var retVal = true;
  	for (var i=0;i<theLength;i++)
  	{
    		var theChar = theInput.substring(i,i+1);
    		if ((theChar >="A" && theChar <="Z") || (theChar >="a" && theChar <="z")  || (theChar >="0" && theChar <="9") || (theChar == "*") || (theChar == "_"))
      			retVal =  true;
    		else
      			retVal = false;
   }
  	return retVal;
}

/* ***************************************************************************
Function Name          fnIsAlphaNum()
Description            Checks if the value of the input parameter (field) is
                       alpha-numeric or a hyphen. Returns false on error else returns true.
**************************************************************************** */
function fnIsAlphaNum(lvatheField)
{
  var lvatheInput = lvatheField.value;
  if (lvatheInput == "")
    return true;
  var lvatheLength = lvatheInput.length;
  var lvaretVal = true;
  for (var lvai=0;lvai<theLength;lvai++)
  {
    var lvatheChar = lvatheInput.substring(lvai,lvai+1);
    if (lvatheChar >="0" && lvatheChar <="9" || (lvatheChar >="A" && lvatheChar <="Z") || (lvatheChar >="a" && lvatheChar <="z"))
      lvaretVal =  true;
 else
     {
      lvaretVal = false;
      return lvaretVal;
     }
  }
  return lvaretVal;
}

/* ***************************************************************************
Function Name          fnIsNum()
Description            Checks if the value of the input parameter (field) is
                       numeric. Returns false on error else returns true.
**************************************************************************** */
function fnIsNum(theField)
{
  	var theInput = theField.value;
  	if (theInput == "")
    		return true;
  	var theLength = theInput.length;
 	  var retVal = true;
  	for (var i=0;i<theLength;i++)
  	{
    		var theChar = theInput.substring(i,i+1);
    		if (theChar <"0" || theChar >"9")
      			retVal =  false;
   }
  	return retVal;
}

/* ***************************************************************************
Function Name          fnIsNumOrWildcard()
Description            Checks if the value of the input parameter (field) is
                       numeric or contains a wilcard, * or ?.
                       Returns false on error else returns true.
**************************************************************************** */
function fnIsNumOrWildcard(lvatheField)
{
  var lvatheInput = lvatheField.value;
  if (lvatheInput == "")
    return true;
  var lvatheLength = lvatheInput.length;
  var lvaretVal = true;
  for (var lvai=0;lvai < lvatheLength;lvai++)
  {
    var lvatheChar = lvatheInput.substring(lvai,lvai+1);
    if (
        ((lvatheChar <"0" || lvatheChar >"9")
        && (lvatheChar != '*')
        && (lvatheChar != '?'))
        )
      lvaretVal =  false;
  }
  return lvaretVal;
}


/* *****************************************************************************************
Function   fnIsNumEmpty()

Description   FUNCTION FOR CHECKING FOR NUMERIC AND EMPTY FIELDS
************************************************************************************************  */
function fnIsNumEmpty(theField)
{
  	var theInput = theField.value;
  	if (theInput == "")
    		return true;
  	var theLength = theInput.length;
  	var retVal = true;
  	for (var i=0;i<theLength;i++)
  	{
    		var theChar = theInput.substring(i,i+1);
    		if (theChar <"0" || theChar >"9")
      			retVal =  false;
   }
  	return retVal;
}

/* **************************************************************************
END Validate functions
***************************************************************************** */

/*************************************************************************************
Function              stripInitialWhiteSpaces()
-was fnstripInitialWhiteSpaces()
-Strongly recommended to use trim() function instead

Description           To strip Initial White Spaces, Returns the string without spaces
**************************************************************************************/
function stripInitialWhiteSpaces(lsttheInput)
{
 for (var i=0;i<lsttheInput.length;i++)
 {
  if (lsttheInput.substring(i,i+1) != " ")
   break;
 }
 return(lsttheInput.substring(i,lsttheInput.length));
}

/** Function kept for backwards compatibility, use trim instead */
function fnStripInitialWhiteSpaces(strInput)
{
 return stripInitialWhiteSpaces(strInput);
}
/**********************************************************************************
Function stripFinalWhiteSpaces()
-was fnstripFinalWhiteSpaces()
-Strongly recommended to use trim() function instead

Description           To strip Final White Spaces, Returns the string without spaces
***********************************************************************************/
function stripFinalWhiteSpaces(lsttheInput)
{
 for (var i=lsttheInput.length;i>0;i--)
 {
  if (lsttheInput.substring(i-1,i) != " ")
   break;
 }
 return(lsttheInput.substring(0,i));
}

/** Function kept for backwards compatibility, use trim instead */
function fnStripFinalWhiteSpaces(strInput)
{
 return stripFinalWhiteSpaces(strInput);
}

/**********************************************************************************
Function      trim()

Description   Removes initial and final White Spaces, Returns the string without spaces
***********************************************************************************/
function trim(aString)
{
  var tmp = stripInitialWhiteSpaces(aString);
  tmp = stripFinalWhiteSpaces(tmp);
  return tmp;
}

/** Prints the current page */
function printPage()
{
	// Should give "Microsoft Internet Explorer" back if IE
//	alert(navigator.appName);

	if (navigator.appName.indexOf('Microsoft') != -1)
	{
		self.print();
//		VBPrint();
	}
	else
	{
		self.print();
	}
}

/** Prints the current page alt.function */
function printPageVB()
{
	// Should give "Microsoft Internet Explorer" back if IE
//	alert(navigator.appName);

	if (navigator.appName.indexOf('Microsoft') != -1)
	{
		self.print();
//		VBPrint();
	}
}

/** Closes the current window */
function closeThisWin(){
	self.close();
}

/** Extracts the value of fieldName from form */
function fnExtractValue(form, fieldName)
{
	var field = findField(form, fieldName);
	if (field != null)
	{
		return field.value;
	}
	return null;
}

/** Find the field with fieldName in form */
function fnFindField(form, fieldName)
{
	var numberElements = form.elements.length;
	var elementIndex;
	for (elementIndex = 0; elementIndex < numberElements; elementIndex++)
	{
		var elementName = form.elements[elementIndex].name;
		var index = elementName.lastIndexOf(fieldName);
		if (index>0)
		{
			var cutFieldName = elementName.substring(index);

			if (cutFieldName == fieldName)
			{
				return form.elements[elementIndex];
			}
		}
	}
	return null;
}

