//Move to the NDC Library, if possible
function trim1(inputString) {
   
   if (typeof inputString != "string") { 
             return inputString; 
   }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; 
}

function TrackCount(fieldObj,countFieldName,maxChars)
{
  var countField = eval("fieldObj.form."+countFieldName);
  var diff = maxChars - fieldObj.value.length;

  // Need to check & enforce limit here also in case user pastes data
  if (diff < 0)
  {
    fieldObj.value = fieldObj.value.substring(0,maxChars);
    diff = maxChars - fieldObj.value.length;
  }
  //countField.value = diff;
}

function LimitText(fieldObj,maxChars)
{
  var result = true;
  if (fieldObj.value.length >= maxChars)
    result = false;

  if (window.event)
    window.event.returnValue = result;
  return result;
}


function redirect_form(a)
    {


       if (a==0)
       {
          window.location.href="index.php?action=change";
       }
       if (a==1)
       {
          window.location.href="index.php?action=display1";
       }

       if (a==2)
       {

          window.location.href="index.php?action=display2";

       }
       if (a==3)
       {

          window.location.href="index.php?action=active1";

       }



    }

function modify_date(date1)
{
     len = date1.length;
     parts = date1.split("/");
     
     if (parts[0].length==1)
     {
          parts[0] = "0"+parts[0];

     }

     if (parts[1].length==1)
     {
          parts[1] = "0"+parts[1];

     }
     if (parts[2].length==2)
     {
          parts[2] = "20"+parts[2];

     }

     return (parts[0]+"/"+parts[1]+"/"+parts[2]);






}

function refreshParent() {
  window.opener.location.href = window.opener.location.href;

}


function check_new_user(a){
  var fnd;
  var fnd1;

  if(a){
        fnd = found_it();
        fnd1 = fnd.replace('_',' ');

        if (trim1(document.frm1.txt.value)==""){
                alert(fnd1 + " Cannot be blank!!!");
                return false;
        }
        if(fnd1=='Email'){
             pos1 = document.frm1.txt.value.indexOf('@');
             pos2 = document.frm1.txt.value.indexOf('.');

             if (pos1==-1){
                alert("Email address is not correct. Please check!!!");
                return false;
             }
             if (pos2==-1){
                alert("Email address is not correct. Please check!!!");
                return false;
             }


        }
        document.frm1.action.value="proceed";
        document.frm1.cust_info.value=fnd;
        document.frm1.submit();

  }
  else{
      window.location.href="/header.php?action=logout";
      //document.frm1.action.value="cancel";
      //document.frm1.submit();
  }

}
function found_it(){

        var found_it
        for (var i=0; i<document.frm1.cust.length; i++)  {
           if (document.frm1.cust[i].checked)  {
                found_it = document.frm1.cust[i].value

           }
        }
        return found_it;
}

function validate_new_user(){

        if (trim1(document.frm1.email.value)==""){
            alert("Error: Email address cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.first.value)==""){
            alert("Error: First Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.last.value)==""){
            alert("Error: Last Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.title.value)==""){
            alert("Error: User Title cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.user.value)==""){
            alert("Error: User Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.cname.value)==""){
            alert("Error: Company Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.address.value)==""){
            alert("Error: Company Address cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.city.value)==""){
            alert("Error: City Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.state.value)==""){
            alert("Error: State Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.zip.value)==""){
            alert("Error: Zipcode cannot be blank!!");
            return false;
        }
        pos1 = document.frm1.email.value.indexOf('@');
        pos2 = document.frm1.email.value.indexOf('.');

             if (pos1==-1){
                alert("Email address is not correct. Please check!!!");
                return false;
             }
             if (pos2==-1){
                alert("Email address is not correct. Please check!!!");
                return false;
             }
             
        document.frm1.submit();

}
function validate_user_request(){


        if (trim1(document.frm1.email1.value)==""){
            alert("Error: Email address cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.email2.value)==""){
            alert("Error: First Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.name.value)==""){
            alert("Error: Full Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.title.value)==""){
            alert("Error: User Title cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.user_name.value)==""){
            alert("Error: User Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.password.value)==""){
            alert("Error: Password cannot be blank!!");
            return false;
        }

        if (trim1(document.frm1.company.value)==""){
            alert("Error: Company Name cannot be blank!!");
            return false;
        }
        if (trim1(document.frm1.city.value)==""){
            alert("Error: City Name cannot be blank!!");
            return false;
        }

        if ((trim1(document.frm1.email2.value)!=trim1(document.frm1.email1.value))){
            alert("Email addresses are not the same. Please check!!!");
            return false;

        }
        pos1 = document.frm1.email1.value.indexOf('@');
        pos2 = document.frm1.email1.value.indexOf('.');

             if (pos1==-1){
                alert("First Email address is not correct. Please check!!!");
                return false;
             }
             if (pos2==-1){
                alert("First Email address is not correct. Please check!!!");
                return false;
             }

        pos3 = document.frm1.email2.value.indexOf('@');
        pos4 = document.frm1.email2.value.indexOf('.');

             if (pos3==-1){
                alert("Second Email address is not correct. Please check!!!");
                return false;
             }
             if (pos4==-1){
                alert("Second Email address is not correct. Please check!!!");
                return false;
             }
             
        //Phone Number
        var stripped = document.frm1.phone.value.replace(/[\(\)\.\-\ ]/g, '');  
        
            if (document.frm1.phone.value == "") {
	        alert("You didn't enter a phone number.");
        	return false;
	    } else if (isNaN(parseInt(stripped))) {
	        alert("The phone number contains illegal characters.");
        	return false;
	    } else if (!(stripped.length == 10)) {
	        alert("The phone number must be a minimum of 10 digits.  Make sure you included an area code.");
        	return false;
    	    }         
        
        //alert("Thank you for your interest. Your request has been successfully sent!!!. Please note that this may take up to 72 hours to complete the process. Please click OK to continue.");
        alert("Thank you for your interest. Your user name will be checked for duplicates.  If dupicates exist you will asked to modify your user name.  Please note that registration may take up to 72 hours to complete. Please click OK to continue.");
        document.frm1.submit();

}

function validate_user_email(){


        if (trim1(document.frm1.email.value)==""){
            alert("Error: Email address cannot be blank!!");
            return false;
        }
        pos1 = document.frm1.email.value.indexOf('@');
        pos2 = document.frm1.email.value.indexOf('.');

             if (pos1==-1){
                alert("Email address is not correct. Please check!!!");
                return false;
             }
             if (pos2==-1){
                alert("Email address is not correct. Please check!!!");
                return false;
             }

        document.frm1.submit();
}




