/**********************************************************
For using all these functions follow instructions below.
1.Put this below the head of the document.
<script language=JAVASCRIPT SRC="allfunc.js"></script>
2.use return FunctionName(paramters)
***********************************************************/

function tarea_check(usrSpc,obj)
{
/************************************************
DESCRIPTION: Validates that a textarea doesnt having 
more than the value user specifies

PARAMETERS:
usrSpc - is the user specified value
obj - is the object (i.e the textarea)

RETURNS:
   True if valid, otherwise false.
*************************************************/

	len_tarea=obj.value.length;
	if (len_tarea>usrSpc)
	{
	alert("Total Characters  Exceeded above "+usrSpc);
	obj.focus();
	return false;
	}

}


function isblank(s) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.

PARAMETERS:
value - the String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/

//alert(s);
     for (var i = 0; i < s.length; i++) {
          var c = s.charAt(i);
          if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		 }
		 return true;
}


function validateNotEmpty( obj ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.

PARAMETERS:
obj - object of which the String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = obj.value;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   else
   {
   alert("Required Fields Need To Be Filled. Should not be Empty");
   obj.focus();
   return false;
   }
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
 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;
}

//http://www.siteexperts.com/tips/functions/ts21/page3.asp

function validateEmail(strValue,obj) {

/* PARAMETERS:
   strValue - String to be tested for validity
   obj - object passed i.e the text box

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
************************************************
*/
var objRegExp  = /(^[a-z0-9]([a-z0-9_\.]*)@([a-z0-9_\.]*)([.][a-z0-9]{3})$)|(^[a-z0-9]([a-z0-9_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;

  //check for valid email
  //if true return true
  if (objRegExp.test(strValue))
  {
  return true;
  }
  else
  {
  alert("Please Enter Valid Email");
  obj.focus();
  return false;
  }
}


function validateUSDate( obj ) {
/************************************************
DESCRIPTION: 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

PARAMETERS:
      obj- object for which the date is to be checked 

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	strValue=obj.value;
  //check to see if in correct format
  if(!objRegExp.test(strValue))
  {
	alert("Please Enter Valid Date");
	obj.focus();
    return false; 
    //doesn't match pattern, bad date
  }
  else{
    var strSeparator = strValue.substring(2,3) //find date separator
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //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}
    var intDay = parseInt(arrayDate[1]);

    //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 intYear = parseInt(arrayDate[2]);
    var intMonth = parseInt(arrayDate[0]);
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
    else
		{
		alert("Date Not Found.Please Check for The Date");
		obj.focus();
		return false;
		}
  }
  return false; //any other values, bad date
}

function validateInteger( obj ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid integer number.

PARAMETERS:
   obj - object for which the String to be tested for validity

RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  = /(^-?\d\d*$)/;
	strValue=obj.value;
  //check for integer characters
	if (objRegExp.test(strValue))
	{
	return true;
	}
	else
	{
	alert("Value Is not An Integer");
	obj.focus();
	return false;
	}
}


/*********************--------------          ------------*************************/
/*  Validation for careers page *///////
function check_js()
{
	
	if(document.frmEbmNadi.name.value == "")
	{
	 alert("Please enter the Name");
	 document.frmEbmNadi.name.focus();
	 return false;
	}
	
   for (var i = 0; i < document.frmEbmNadi.name.value.length; i++) 
         {
            var ch = document.frmEbmNadi.name.value.substring(i, i + 1);
			if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)&& ch ==""))
            {
             alert("\nThe Name field only accepts letters.\n\nPlease re-enter the name.");
             document.frmEbmNadi.name.select();
             document.frmEbmNadi.name.focus();
             return false;
           }
        } 
	
	if(document.frmEbmNadi.email.value=="")
	 {
	 alert("Enter your e-mail");
	 document.frmEbmNadi.email.select();
	 document.frmEbmNadi.email.focus();
	 return false;
	 }
	 
	  var email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	  if (document.frmEbmNadi.email.value.search(email)==-1)
	  {
		  alert("Invalid E-mail Address! Please re-enter.");
		  document.frmEbmNadi.email.select();
		  document.frmEbmNadi.email.focus();
		  return false;
	  }	 
	
/*	if(document.frmEbmNadi.position.value=="0")
	{
 		alert("Select the position");
 		document.frmEbmNadi.position.focus();
	 	return false;
	} */
	
	if ((document.frmEbmNadi.message.value == null) || (document.frmEbmNadi.message.value == "") || isblank(document.frmEbmNadi.message.value)) 
	 {
		alert("Please enter the message");
		document.frmEbmNadi.message.select();
		document.frmEbmNadi.message.focus();
		return false;
	 }
	
	if(document.frmEbmNadi.file1.value != "")
	{
	 Str = document.frmEbmNadi.file1.value
	 if (Str.substring(Str.length-3) != "doc")
	 {
		 alert("Please select the correct file");
		 document.frmEbmNadi.file1.focus();
	     return false;
	 }
	}
	 return true;
}

/* Validation for order place ment page */
function check()
{

	if ((document.frmEbm.address.value == null) || (document.frmEbm.address.value == "") || isblank(document.frmEbm.address.value)) 
	 {
		alert("Please enter the text");
		document.frmEbm.address.select();
		document.frmEbm.address.focus();
		return false;
	 }
	 
	if(document.frmEbm.email.value=="")
	 {
	 alert("Enter your e-mail");
	 document.frmEbm.email.select();
	 document.frmEbm.email.focus();
	 return false;
	 }
	 
	  var email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	  if (document.frmEbm.email.value.search(email)==-1)
	  {
		  alert("Invalid E-mail Address! Please re-enter.");
		  document.frmEbm.email.select();
		  document.frmEbm.email.focus();
		  return false;
	  }	 
	 

	if(document.frmEbm.po_no.value=="")
	 {
	 alert("Please enter purchase order number");
	 document.frmEbm.po_no.select();
	 document.frmEbm.po_no.focus();
	 return false;
	 }

	if(isNaN(document.frmEbm.po_no.value))
	{
	 alert("Purchase Order Reference must be number");
	 document.frmEbm.po_no.focus();
	 return false;
	}
	
	if(document.frmEbm.day.value=="0")
	{
	 alert("Select the Day");
	 document.frmEbm.day.focus();
	 return false;
	}

	if(document.frmEbm.month.value == "0")
	{
	 alert("Select the month");
	 document.frmEbm.month.focus();
	 return false;
	}
	
	if(document.frmEbm.year.value == "0")
	{
	 alert("Select the year");
	 document.frmEbm.year.focus();
	 return false;
	}
	
	if ((document.frmEbm.daddress.value == null) || (document.frmEbm.daddress.value == "") || isblank(document.frmEbm.daddress.value)) 
	 {
		alert("Please enter the Delivery Address");
		document.frmEbm.daddress.focus();
		return false;
	 }
	
	if(document.frmEbm.despatch.value=="")
	{
	 alert("Enter mode of Despatch");
	 document.frmEbm.despatch.focus();
	 return false;
	}
	
   for (var i = 0; i < document.frmEbm.despatch.value.length; i++) 
         {
            var ch = document.frmEbm.despatch.value.substring(i, i + 1);
			if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)&& ch !=""))
            {
             alert("\nThe Despatch field only accepts letters.\n\nPlease re-enter the Despatch.");
             document.frmEbm.despatch.select();
             document.frmEbm.despatch.focus();
             return false;
           }
        } 
		
	 
	if(document.frmEbm.payment.value == "")
	{
	 alert("Enter payment Terms");
	 document.frmEbm.payment.focus();
	 return false;
	}

  return true; 
}

/* Validation for Enquiry form */
function enquiry()
{
	
if ((document.frmEbmNadi.address.value == null) || (document.frmEbmNadi.address.value == "") || isblank(document.frmEbmNadi.address.value)) 
	 {
		alert("Please enter the text");
		document.frmEbmNadi.address.select();
		document.frmEbmNadi.address.focus();
		return false;
	 }
 
	if(isNaN(document.frmEbmNadi.phone.value))
	{
	 alert("Please enter the Phone number");
	 document.frmEbmNadi.phone.select();
	 document.frmEbmNadi.phone.focus();
	 return false;
	}
	
	if(isNaN(document.frmEbmNadi.fax.value))
	{
	 alert("Please enter the Fax number");
	 document.frmEbmNadi.fax.select();
	 document.frmEbmNadi.fax.focus();
	 return false;
	}
	
	if(document.frmEbmNadi.email.value=="")
	 {
	 alert("Enter your e-mail");
	 document.frmEbmNadi.email.select();
	 document.frmEbmNadi.email.focus();
	 return false;
	 }
	 
	  var email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	  if (document.frmEbmNadi.email.value.search(email)==-1)
	  {
		  alert("Invalid E-mail Address! Please re-enter.");
		  document.frmEbmNadi.email.select();
		  document.frmEbmNadi.email.focus();
		  return false;
	  }	 
	
	if(document.frmEbmNadi.volume.value=="")
	 {
	 alert("Please enter Volume");
	 document.frmEbmNadi.volume.select();
	 document.frmEbmNadi.volume.focus();
	 return false;
	 }
	 
	document.getElementById("check").style.display="none";
	document.getElementById("enquiry").style.display="";
return true;
}

function isValidEmail(pemail, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (pemail==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (pemail.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(pemail)) {  // check to make sure all characters are valid
        return false;
    }
    if (pemail.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (pemail.lastIndexOf(".") <= pemail.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (pemail.indexOf("@") == pemail.length) {  // @ must not be the last character
        return false;
    } else if (pemail.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (pemail.indexOf(".") == pemail.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(pemail) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < pemail.length; i++) {
    var letter = pemail.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

