﻿
var whitespace = " \t\n\r";

/****************************************************************/

function validate_text(control,FieldName,text)
{
  var field=document.getElementById(control);
  if(field.value==text)
  {
   alert("Error: You need to select " + FieldName + "!");
   field.focus();
   return false;
  }
}


/****************************************************************/

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(control, FieldName)
{
    var field=document.getElementById(control);
	var strField = new String(field.value);
	// Is s empty?
    if (isEmpty(strField))
    {
        alert("Error: You need to enter information for " + FieldName + "!");
		field.focus();
		field.select();
		return false;
    }
	if (isWhitespace(strField)) 
	{
		alert("Error: You can't enter whitespace characters only for " + FieldName + "!");
		field.focus();
		field.select();
		return false;
	}
	return true;
}

/****************************************************************/

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


/****************************************************************/

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)
{   
    var i;

    // Is s empty?
//    if (isEmpty(s)) return true;
    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0;i<s.length;i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);
	if(whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

/****************************************************************/

// Returns true if the string passed in is a valid money
//  (no alpha characters except a decimal place), 
//   else it displays an error message

function ForceMoney(control,FieldName,DecimalPlace)
{
    var field=document.getElementById(control);
	var strField= new String(field.value);
	
	var i = 0;
    var count=0;
	for (i = 0; i < strField.length; i++)
	{
		if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && (strField.charAt(i) != '.')) 
		{		
			alert("Error: " + FieldName + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols!");
			field.focus();
			field.select();
			return false;
		}
		if(strField.charAt(i) == '.')
		{
		    count=count+1;
		}
	}	
	
	if(count > 1)
	{
	    alert("Error: " + FieldName + " must have only 1 decimal!");
        field.focus();
        return false;
	}
		
    var decimal = strField.indexOf(".");
    if(decimal != -1)
    {
      if (decimal+DecimalPlace != strField.length-1)
      {
        alert("Error: " + FieldName + " must have only 2 decimal places!");
        field.focus();
        return false;
      }
    }

	return true;
}


/****************************************************************/

function isValidEmail(control) 
{
    var field=document.getElementById(control);
	var email= new String(field.value);
//alert(email.length);
//alert(email.indexOf("@"));
	if (isWhitespace(email)) return true;

    if (! allValidChars(email)) 
    {  // check to make sure all characters are valid
        alert("Error: check to make sure all characters are valid!");
        field.focus();
        field.select(); 
        return false;
    }
    if (email.indexOf("@") < 1) 
    { //  must contain @, and it must not be the first character
        alert("Error: must contain @, and it must not be the first character!");
        field.focus();
        field.select(); 
        return false;
    } 
    else if (email.lastIndexOf(".") <= email.indexOf("@")) 
    {  // last dot must be after the @
        alert("Error: last dot must be after the @!");
        field.focus();
        field.select(); 
        return false;
    } 
    else if (email.indexOf("@") == email.length-1) 
    {  // @ must not be the last character
        alert("Error: @ must not be the last character!");
        field.focus();
        field.select(); 
        return false;
    } 
    else if (email.indexOf("..") >=0) 
    { // two periods in a row is not valid
        alert("Error: two periods in a row is not valid!");
        field.focus();
        field.select(); 
	    return false;
    } 
    else if (email.indexOf(".") == email.length-1) 
    {  // . must not be the last character
        alert("Error: . must not be the last character!");
        field.focus();
        field.select(); 
	    return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}


/****************************************************************/

// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message

function ForceNumber(control, FieldName)
{
    var field=document.getElementById(control);
	var strField = new String(field.value);
	
	if (isWhitespace(strField)) return true;

	var i = 0;
    
	for (i = 0; i < strField.length; i++)
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9') 
		{
		
			alert("Error: " + FieldName + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols!");
			field.focus();
			field.select();
			return false;
		}

	return true;
}

/****************************************************************/


/****************************************************************/

// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message

function ForceNaturalNumber(control, FieldName)
{
    var field=document.getElementById(control);
	var strField = new String(field.value);
	
	if (isWhitespace(strField)) return true;

	if (strField =='0')
		{
		
			alert("Error: " + FieldName + " must be greater than 0")
			field.focus();
			field.select();
			return false;
		}

	return true;
}

/****************************************************************/




/****************************************************************/

// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message

function ForceAlphabets(control, FieldName)
{
    var field=document.getElementById(control);
	var strField = new String(field.value);
	var validchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	if (isWhitespace(strField)) return true;

	var i = 0;
    var statValidChars=0;
	for (i = 0; i < strField.length; i++)
	{
	    statValidChars=0;
	    for(var j = 0; j < validchars.length; j++)
	    {
	        
		    if (strField.charAt(i) == validchars.charAt(j)) 
		    {
	        	    statValidChars=1;	
		    }
		    
		}
		if(statValidChars!=1)
		{
		    alert("Error: " + FieldName + " must be a valid alphabetic entry.  Please do not use commas or dollar signs or any numeric symbols!");
			field.focus();
			field.select();
			return false;
		}
    }
    
            
	return true;
}

/****************************************************************/


//Strong Password 

 function checkPassword(control1,control2)
  {
    var pwd1=document.getElementById(control1);
    var pwd2=document.getElementById(control2);
    
    
      if(pwd1.value.length < 6) {
        alert("Error: Password must contain atleast six characters!");
        pwd1.focus();
        return false;
  }
     
//      re = /[0-9]/;
//      if(!re.test(pwd1.value)) {
//        alert("Error: password must contain at least one number (0-9)!");
//        pwd1.focus();
//        return false;
//      }
//      re = /[a-z]/;
//      if(!re.test(pwd1.value)) {
//        alert("Error: password must contain at least one lowercase letter (a-z)!");
//        pwd1.focus();
//        return false;
//      }
//      re = /[A-Z]/;
//      if(!re.test(pwd1.value)) {
//        alert("Error: password must contain at least one uppercase letter (A-Z)!");
//        pwd1.focus();
//        return false;
//      }
    if(pwd1.value != "" && pwd1.value == pwd2.value) 
    {
    }
    else
    {
      alert("Error: Please check that you've entered and confirmed your password!");
      pwd2.focus();
      return false;
    }

    return true;
  }
  
  /*****************************Compare Dates***************************************/
  function compareDates(control1)
  {
     var dt=new Date(document.getElementById(control1).value);
     var today = new Date();
//     var dd = today.getDate();
//     var mm = today.getMonth()+1;
//     var yyyy = today.getFullYear();
//     today=mm+'/'+dd+'/'+yyyy;
     if(dt<=today)
     {
       alert("Error: Date should be greater than current date!");
       document.getElementById(control1).focus();
       return false;
     }
  }
  
  /*************************************************************************************/
//check maxlength

function check_length(control,maxLength) {
     var field=document.getElementById(control);      
	 var strlength = parseInt(field.value.length);
	 var strMaxlength = parseInt(maxLength);
  
     if (strlength >= strMaxlength)
     {        
         window.alert("Error: You can't enter more than " + strMaxlength + " characters");         
         return false;          
     }
     else
     {         
         return true; 
     }
}

//******************************************************************************************

function checkQty(frm,alertText)
{
  // loop through all elements
  for (i = 0; i < frm.length; i++)
  {
    // Look for our qty only
    if (frm.elements[i].name.indexOf("txtQty") !=  - 1)
    {
      // If any are 0 then confirm alert, otherwise nothing happens
      if (frm.elements[i].value==0)
      {
      frm.elements[i].focus();
      frm.elements[i].select();
      alert(alertText);
      return false;
      }
    }    
   }
}
