/***********************************    This function is used to trim the user entered values   **************************/
function trim(sString) 
{
     while (sString.substring(0,1) == ' ')
          {
          sString = sString.substring(1, sString.length);
          }
     while (sString.substring(sString.length-1, sString.length) == ' ')
          {
          sString = sString.substring(0,sString.length-1);
          }
   return sString;
}

function ltrim ( s ){
	return s.replace( /^\s*/, "" )
}

function rtrim ( s ){
	return s.replace( /\s*$/, "" );
}


function fulltrim ( s ){
	return rtrim(ltrim(s));
}

/*********************************** This function is used to validate Characters ************************************/

function jsValidate_Characters(field) {    
	return field.value.test(/^[a-zA-Z ]$/);
}

/***************************************This function is used to validate Numbers *************************************/
function jsvalidate_number(field) {
	return field.value.test(/^[0-9]-.\/$/);
}  
/***************************************This function is used to validate Numbers in PhoneNumber *************************************/
function jsvalidate_PhoneNumber(field) {
    var valid = "0123456789"
    var ok = "yes";
      var temp;
	  /*var validNumber1;
	  var validNumber2;*/
      for (var i=0; i<field.length; i++)
        {
                temp = "" + field.substring(i, i+1);
			      	
                        if ( valid.indexOf(temp) == "-1" )
                        {
                                return false;
                        }

         }
  return true;
}  
/******************************** This function is used to validate Special Characters *******************************/
function jsvalidate_specialchar(field) {
    var valid = "/#$%^&*()-|!+,<>:;'`~"
    var ok = "yes";
      var temp;
	  /*var validNumber1;
	  var validNumber2;*/
      for (var i=0; i<field.length; i++)
        {
                temp = "" + field.substring(i, i+1);
			      	//alert("valid.indexOf(temp) ::"+valid.indexOf(temp));
                        if (valid.indexOf(temp) == "0" || valid.indexOf(temp) == "1" ||valid.indexOf(temp) == "2" ||valid.indexOf(temp) == "3" ||valid.indexOf(temp) == "4" || valid.indexOf(temp) == "5" || valid.indexOf(temp) == "6" || valid.indexOf(temp) == "7" || valid.indexOf(temp) == "8" || valid.indexOf(temp) == "9" || valid.indexOf(temp) == "10" || valid.indexOf(temp) == "11" || valid.indexOf(temp) == "12" || valid.indexOf(temp) == "13" || valid.indexOf(temp) == "14" || valid.indexOf(temp) == "15" || valid.indexOf(temp) == "16" || valid.indexOf(temp) == "17" || valid.indexOf(temp) == "18" || valid.indexOf(temp) == "19" || valid.indexOf(temp) == "20" || valid.indexOf(temp) == "21")
                        {
                                return false;
                        }

         }
  return true;

}  
/********************************  This function is used to validate the Email *******************************/

function emailcheck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){		    
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){		    
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){		    
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){		   
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){		    
		    return false;
		 }

 		 return true	;				
	}
/******************************************* This is to Validate Phone number ****************************************************/
		/**
		 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
		 */

		// Declaring required variables
		var digits = "0123456789";
		// non-digit characters which are allowed in phone numbers
		var phoneNumberDelimiters = "()- ";
		// characters which are allowed in international phone numbers
		// (a leading + is OK)
		var validWorldPhoneChars = phoneNumberDelimiters+"+";
		// Minimum no of digits in an international phone no.
		var minDigitsInIPhoneNumber = 10;
/*
		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 stripCharsInBag(s, bag)
		{   var i;
			var returnString = "";
			// Search through string's characters one by one.
			// If character is not in bag, append to returnString.
			for (i = 0; i < s.length; i++)
			{   
				// Check that current character isn't whitespace.
				var c = s.charAt(i);
				if (bag.indexOf(c) == -1) 
				{	
					//alert("Inside stripCharsInBag IF");
					returnString += c;
				}
			}
			return returnString;
		}

		function checkInternationalPhone(strPhone){
		s=stripCharsInBag(strPhone,validWorldPhoneChars);
		return (jsvalidate_PhoneNumber(s) && s.length == minDigitsInIPhoneNumber);
		}

		function ValidateForm(Phone){
			//var Phone=document.frmSample.txtPhone
			
			/*if ((Phone.value==null)||(Phone.value=="")){
				//alert("Please Enter your Phone Number")
				MissingFields = MissingFields+"\n\n Please Enter your Phone Number.";
				//Phone.focus()
				return false
			}*/
			if (checkInternationalPhone(Phone)==false){
				//alert("Please Enter a Valid Phone Number")
				//MissingFields = MissingFields+"\n\n Please Enter a Valid Phone Number.";
				//Phone=""
				//Phone.focus()
				return false
			}
			return true
		 }
/******************************************* This is used to validate form in ContactUs page *************************************/

function jsValidateContactUsForm(formName)
{
	 

    var MissingFields = "";
     var Phonechk = 'NO';  //This variable is used in checking the phone field
	 var VarZipCode = formName.txtZip.value;
 if((trim(formName.txtName.value)).length <= 0)
         {

             MissingFields = "\n'Name'  field cannot be left blank";         
           
         }
  
  //Validate for Charecters
  if(jsValidate_Characters(document.getElementById('txtName')))
         {           
           
         }
         else
         {

             MissingFields = MissingFields+"\n\n'Name'  field can hold only character values";

         }
         
         if((trim(formName.txtAddress.value)).length <= 0)
         {

             MissingFields = MissingFields+"\n\n'Address'  field cannot be left blank";         
            
         }
         if((trim(formName.txtCity.value)).length <= 0)
         {

             MissingFields = MissingFields+"\n\n'City'  field cannot be left blank";         
           
         }
         if((trim(formName.txtZip.value)).length <= 0)
         {

			 MissingFields = MissingFields+"\n\n'Zip'  field cannot be left blank";         
          
         }		 
	     //Validate for Characters
         if(jsvalidate_number(VarZipCode))
         {           
            
         }
         else
         {

             MissingFields = MissingFields+"\n\n'Zip'  field can hold only numbers (0 to 9) and characters (- . /).";

         }
         if((trim(formName.txtPhone.value)).length <= 0)
         {
             
             MissingFields = MissingFields+"\n\n'Phone'  field cannot be left blank";
             Phonechk = 'YES';
         
         }
          //Validate for Integers 
          if(Phonechk == 'NO')
          {
                       if(ValidateForm(trim(document.getElementById('txtPhone').value) ) )
                           {           
                             //alert("Inside IF ");
                           }
                           else
                          {
								 //alert("Inside Else ");
                           //    MissingFields = MissingFields+"\n\n'Phone'  Field can hold only Numbers (0 to 9) and Character (-).";
						   MissingFields = MissingFields+"\n\n Please enter a valid phone number. example: 123-123-1234 or (123) 123-1234";
                  
                           }
         }
      
         if((trim(formName.txtEmail.value)).length <= 0)
         {
             MissingFields = MissingFields+"\n\nPlease enter your  'Email ID'";  
             alert(MissingFields);         
             return false;            
         }
        //Validation for Email field
        
        var emailID=formName.txtEmail;
		var emailValue = trim(emailID.value);
		emailID.value = emailValue;
		//alert("emailValue ::"+emailValue);
		//  Validate for special characters
		if(jsvalidate_specialchar(emailValue)){

		}
		else
		{
		   MissingFields = MissingFields+"\n\n'Email' field can contain only '@' and '.' special characters";
		   alert(MissingFields);
		   return false;
		}
        if (emailcheck(emailValue)==false){
                        MissingFields = MissingFields+"\n\nInvalid  'E-mail ID'"; 
                        alert(MissingFields);
                        return false;
                      }
       else
        {
             if(MissingFields != "")
             {
                 alert(MissingFields);
                 return false;              
             }
             else
             {
               return true;
             }
        
        }
 
 
        
}

/****************************** This is to validate form in Request a Bid Page  ****************************************/

function jsValidateRequestABidForm(FORMNAME)
{
   
    var MissingFieldsInRAB = "";
    var DivisionId    = FORMNAME.hdDivisionId.value;
	var VarZipCode1 = FORMNAME.txtZip.value;
	 var Phonechk = 'NO';  //This variable is used in checking the phone field
	
	if(DivisionId != "")
   {
        var hiddenInputarlEntireStageProductListById = FORMNAME.hdarlEntireStageProductListById.value;
        var objSelectTag;
		var counter  = 0
		//var UsedforChecking = 5 ; // this variable is used for checking. Later we can remove this variable.
        for ( var intIndex=1;intIndex <= hiddenInputarlEntireStageProductListById ; intIndex++)
                {
                       var myindex = 'myindex'+intIndex;
                       var SelectTagId = 'slStageProducts'+intIndex;     
                       objSelectTag = document.getElementById(SelectTagId);
                     
                      if(objSelectTag.selectedIndex==0)
                      {
                            counter = counter+1;
                      }
				}    

                       
      } 
  else
    {
		var hiddenInputarlEntireStageProductListByWithOutIdSize = FORMNAME.hdarlEntireStageProductListByWithOutIdSize.value;
        var objectSelectTag;
		var counterVariable  = 0;
		//var forChecking = 5 ; // this variable is used for checking. Later we can remove this variable.
        for ( var intIndex=1;intIndex <= hiddenInputarlEntireStageProductListByWithOutIdSize ; intIndex++)
                {
                       var myindex = 'myindex'+intIndex;
                       var SelectedTagId = 'slStageProducts'+intIndex;     
                       objectSelectTag = document.getElementById(SelectedTagId);
                     
                      if(objectSelectTag.selectedIndex==0)
                      {
                            counterVariable = counterVariable+1;
                      }
				}    

   }
   

 if((trim(FORMNAME.txtName.value)).length <= 0)
         {
          
             MissingFieldsInRAB = "\n'Name'  field cannot be left blank";
            
         }
  
  //Validate for Charecters
  if(jsValidate_Characters(document.getElementById('txtName')))
         {           
           
         }
         else
         {

             MissingFieldsInRAB = MissingFieldsInRAB+"\n\n'Name'  field can hold only character values";              

         }
         
         if((trim(FORMNAME.txtAddress.value)).length <= 0)
         {

             MissingFieldsInRAB = MissingFieldsInRAB+"\n\n'Address'  field cannot be left blank";
            
         }
         if((trim(FORMNAME.txtCity.value)).length <= 0)
         {

             MissingFieldsInRAB = MissingFieldsInRAB+"\n\n'City'  field cannot be left blank";
         
          
         }
		 if((trim(VarZipCode1)).length <= 0)
         {

             MissingFieldsInRAB = MissingFieldsInRAB+"\n\n'Zip'  field cannot be left blank";         
            
         }
		 //Validate for Characters
         if(jsvalidate_number(VarZipCode1))
         {           
            
         }
         else
         {

             MissingFieldsInRAB = MissingFieldsInRAB+"\n\n'Zip' field can hold only numbers (0 to 9) and characters (- . /).";

         }
         if((trim(FORMNAME.txtPhone.value)).length <= 0)
         {

             MissingFieldsInRAB = MissingFieldsInRAB+"\n\n'Phone'  field cannot be left blank";
			 Phonechk = 'YES';
         }
          //Validate for Integers  
	if(Phonechk == 'NO')
          {
			 if(ValidateForm(trim(document.getElementById('txtPhone').value)))
				 {           
				   
				 }
				 else
				 {

				 //    MissingFieldsInRAB = MissingFieldsInRAB+"\n\n'Phone'  Field can hold only Numbers (0 to 9) and Character (-). for eg:(111-323-1234)";
					   MissingFieldsInRAB = MissingFieldsInRAB+"\n\n Please enter a valid phone number. example: 123-123-1234 or (123) 123-1234.";
				 }
		  } 
         if((trim(FORMNAME.txtEmail.value)).length <= 0)
         {

             MissingFieldsInRAB = MissingFieldsInRAB+"\n\nPlease enter your  'Email ID'"; 
             alert(MissingFieldsInRAB); 
             return false;            
         }
         
        //Validation for Email field
        
        var emailID=FORMNAME.txtEmail;
		var emailValue = trim(emailID.value);
		emailID.value = emailValue;
		//alert("emailValue ::"+emailValue);
		//  Validate for special characters
		if(jsvalidate_specialchar(emailValue)){

		}
		else
		{
		   MissingFieldsInRAB = MissingFieldsInRAB+"\n\n'Email' field can contain only '@' and '.' special characters";
		   alert(MissingFieldsInRAB);
		   return false;
		}


        if (emailcheck(trim(emailID.value))==false){
                        MissingFieldsInRAB = MissingFieldsInRAB+"\n\nInvalid  'E-mail ID'";
                        alert(MissingFieldsInRAB);

                        return false;
                      }
        //Validation for Drop down list
        if(DivisionId == "")
        {
              if (counterVariable == hiddenInputarlEntireStageProductListByWithOutIdSize )
              {
                    MissingFieldsInRAB = MissingFieldsInRAB+"\n\nYou must make a selection atleast from one drop-down menu.";
                    alert(MissingFieldsInRAB);  
                    return false;
              }
              else
              {
                   if(MissingFieldsInRAB != "")
                   {
                       alert(MissingFieldsInRAB);
                       return false;              
                   }
                   else
                   {
                     return true;
                   }
              
              }
		}
       
			   else
			   {

					if(counter == hiddenInputarlEntireStageProductListById)
						{
					   
							 MissingFieldsInRAB = MissingFieldsInRAB+"\n\nYou must select a product from the drop-down menu.";
							 alert(MissingFieldsInRAB);
							 return false;
						}
						
						else
						{
							if(MissingFieldsInRAB != "")
							   {
								   alert(MissingFieldsInRAB);
								   return false;              
							   }
							   else
							   {
								 return true;
							   }
						}
				 }
}


/****************************This is to validate form in JobApplication page *************************************/

function fnValidate(objForm)
  {
      //var objForm         = document.<%=strFormName%>;
      var MissingFields = "";
      var Phonechk = 'NO';  //This variable is used in checking the phone field
      
      var objTxtName      = objForm.txtName;
      var objTxtTelehone  = objForm.txtTelehpone;
      var objTxtAddress   = objForm.txtAddress;
      var objFileField    = objForm.flResume;
      
      var jsTxtName       = trim(objTxtName.value);
      var jsTxtTelehone   = trim(objTxtTelehone.value);
      var jsTxtAddress    = trim(objTxtAddress.value);
      var jsFileField     = trim(objFileField.value);
      
      if(jsTxtName.length <= 0)
      {
          MissingFields = "\n'Name'  Field cannot be left Blank";
//          return;
      }
       if(jsValidate_Characters(objTxtName))
         {           
           
         }
         else
         {
             MissingFields = MissingFields+"\n\n'Name'  Field can hold only character values";
//          return;
         }
       if(jsTxtAddress.length<=0)
       {
          MissingFields = MissingFields+"\n\n'Address'  Field cannot be left Blank";      
       }
       if(jsTxtTelehone.length <=0 )
      {
         MissingFields = MissingFields+"\n\n'Telephone'  Field cannot be left Blank";
         Phonechk = 'YES';
      }
      
      //Validate for Integers 
          if(Phonechk == 'NO')
          {
                if(jsvalidate_number(jsTxtTelehone) )
                           {           
                             
                           }
                 else
                 {
                  MissingFields = MissingFields+"\n\n'Telephone'  Field can hold only Numbers (0 to 9) and Characters (. , /)";
                  //return;
                 }
          }                
        if(jsFileField.length<=0)
        {
          MissingFields = MissingFields+"\n\n Resume Field cannot be left blank . Please browse & upload your Resume ";
        }
        
        if(MissingFields != "")
             {
                 alert(MissingFields);
                 return;              
             }
             else
             {
               objForm.submit();
             }
                           

  }

/****************  This functions are used for Locate By State jsp   *****************/

function fnStateProd(formName,jsStateName,jsProdId,jsTrace)
 {
   // var form = document.<%=UrlUtils.htmlFormName(pReq, null)%>;
    var form = document.getElementsByName(formName).item(0);
    form.LocationStateProductAction.value='LOCATION_STATE_PRODUCT';
    form.LocationStateName.value=jsStateName;
    form.LocationProductId.value=jsProdId;
	form.TraceLocateByStatePage.value=jsTrace;
//alert("jsTrace value ::"+jsTrace);
//alert("form.TraceLocateByStatePage.value=jsTrace ::"+form.TraceLocateByStatePage.value);
    form.submit();
 }

 function fnCityStateProd(formName,jsProdId,jsStateName,jsCityName,jsTrace)
 {
    //var form = document.<%=UrlUtils.htmlFormName(pReq, null)%>;
    var form = document.getElementsByName(formName).item(0);
    form.LocationStateProductAction.value='LOCATION_STATE_PRODUCT';
    form.LocationStateName.value=jsStateName;
    form.LocationProductId.value=jsProdId;
    form.LocationCityName.value=jsCityName;
	form.TraceLocateByStatePage.value=jsTrace;
//alert("jsTrace value ::"+jsTrace);
//alert("form.TraceLocateByStatePage.value=jsTrace ::"+form.TraceLocateByStatePage.value);

    form.submit();
 }

 function fnZipCodeProd(formName,jsProdId,jsZipCode,jsTrace)
 {
    //var form = document.<%=UrlUtils.htmlFormName(pReq, null)%>;
    var form = document.getElementsByName(formName).item(0);
    form.LocationStateProductAction.value='LOCATION_STATE_PRODUCT';
    form.LocationZipCode.value=jsZipCode;
    form.LocationProductId.value=jsProdId;
	form.TraceLocateByStatePage.value=jsTrace;
//alert("jsTrace value ::"+jsTrace);
//alert("form.TraceLocateByStatePage.value=jsTrace ::"+form.TraceLocateByStatePage.value);
    form.submit();
 }

/******************  This functions are used in LocateByStateByProductBydivision jsp page *******************************/

function fnProduct(formName,jsProdId,jsStageId)
   {
      //var form = document.<%=UrlUtils.htmlFormName(pReq, null)%>;
	    var form = document.getElementsByName(formName).item(0);
      form.LocationStateProductDivisionProductAction.value='LOCATION_STATE_PRODUCT_DIVISION_PRODUCTDETAILS';
      form.StageId.value=jsStageId;
      form.ProductId.value=jsProdId;
      form.submit();
   }

/*************************** This function is used in Location Error Main APge jsp ****************************/

function searchProducts(formName)
 {
   var form = document.getElementsByName(formName).item(0);
  var searchString = form.City_State_Zip_Text.value;
  form.LocationSearchMainAction.value='LocationSearchMainAction';
  form.submit();
 }
 function handleKeyPress(e)
{
    var key= e.keyCode || e.which;
    if(key==13)
    {
        searchProducts();
    }
}   

/************************* This functions are used in Products Description JSP  *************************/

function fnProd(formName,jsProdId,jsStageId)
 {
    //var form = document.<%=strFormName%>;

	var form = document.getElementsByName(formName).item(0);

    form.prodAction.value='Product_Page';
    form.prodStageId.value=jsStageId;
    form.prodProductId.value=jsProdId;
    form.submit();
 }

  function fnProductState(formName,jsProdId,jsStageId)
 {
    //var form = document.<%=strFormName%>;
	var form = document.getElementsByName(formName).item(0);
    form.prodAction.value='Product_States_Page';
    form.prodStageId.value=jsStageId;
    form.prodProductId.value=jsProdId;


    form.submit();
 }

/***************************** This functions are used for Products main PAge JSP ************************/

function fnStage(formName,jsStageId)
 {
	var form = document.getElementsByName(formName).item(0);

	var prodAction =form.prodAction.value;

	form.prodAction.value='Stage_Page'; 
    form.prodStageId.value=jsStageId;
    form.submit();
	
 }

/************************************ This function is used in JobSearchResults JSP *******************************/

function fnGetJobDescription(formName,jobId)
 {
  
	var form = document.getElementsByName(formName).item(0);
  
  form.txtJobId.value=jobId;
  
  form.submit();
 }

/**************************** This functions are used for CategoriesPortlet JSP  *************************/

function fnAction(formName,jsAction,jsCategoryId,jsCategory)
 {

	var form = document.getElementsByName(formName).item(0);
  
  if(jsAction=='add')
  {
    form.hdAction.value=jsAction;
    form.hdCategoryId.value=form.txtCategoryId.value;
    form.hdCategory.value=form.txtCategory.value;    
  }
  
  if(jsAction=='remove')
  {
    form.hdAction.value=jsAction;
    form.hdCategoryId.value=jsCategoryId;
    form.hdCategory.value=jsCategory;
  }

  if(jsAction=='update')
  {
    var jsName = 'txtUpdatedCategory'+jsCategoryId;
    var objTextBox = document.getElementById(jsName);
    
    
    form.hdAction.value=jsAction;
    form.hdCategoryId.value=jsCategoryId;
    form.hdCategory.value=objTextBox.value;
  }
  
  form.submit();
 }
 
 function fnAdd(formName)
 {

	var form = document.getElementsByName(formName).item(0);
    
    //form.txtCategoryId.style.display="";
    form.txtCategory.style.display="";
    form.btnAdd.style.display="";
 }
 
 function fnUpdate(formName,jsCategoryId)
 {

	var form = document.getElementsByName(formName).item(0);
    
    var jsBoxName = 'txtUpdatedCategory'+jsCategoryId;
    var jsButtonName = 'btnSave'+jsCategoryId;
    
    var objTextBox = document.getElementById(jsBoxName);
    var objButton  = document.getElementById(jsButtonName);
    
    objTextBox.style.display="";
    objButton.style.display=""; 
 }
