/* $Id: validate_reg.js,v 1.38.2.17 2007/03/27 17:20:33 strouchelle Exp $ */

var submit_flag = 0;
var defaultEmptyOK = false;

// these functions are form FormCheck by Netscape

//--------------------------------------------------------------------------------------------------------
// isInteger (STRING s [, BOOLEAN emptyOK])
//
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true),
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)  {
  var i;

    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)  {
        var c = s.charAt(i);

        if (!isDigit(c))                 // Check that current character is number.
            return false;
    }

    return true;        // All characters are numbers.
}

//--------------------------------------------------------------------------------------------------------
// isSignedInteger (STRING s [, BOOLEAN emptyOK])
//
// Returns true if all characters are numbers;
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true

function isSignedInteger (s)  {

    if (isEmpty(s)) {
           return false;
    } else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )            // skip leading + or -
           startPos = 1;

        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

//--------------------------------------------------------------------------------------------------------
// isPositiveInteger (STRING s [, BOOLEAN emptyOK])
//
// Returns true if string s is an integer > 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isPositiveInteger (s)  {

    var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a positive, not negative, number

    return (isSignedInteger(s) && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

//--------------------------------------------------------------------------------------------------------
// Check whether string s is empty.

function isEmpty(s)  {

  return ((s == null) || (s.length == 0))
}

//--------------------------------------------------------------------------------------------------------
// Returns true if character c is a digit
// (0 .. 9).

function isDigit (c)  {

  return ((c >= "0") && (c <= "9"))
}

//--------------------------------------------------------------------------------------------------------
function validate_us_phone(phone){

  var str = String(validate_phone(phone));

     tmp = str;
    open_paren = /\(/;
    tmp = tmp.replace(open_paren, "");

   close_paren = /\)/;
   tmp = tmp.replace(close_paren, "");

   dash = /\-/g;
   tmp = tmp.replace(dash, "");

   space = /\s/g;
   tmp = tmp.replace(space, "");


   if(isNaN(tmp) || tmp.length != 10)  {
     return false;
   }

  return true;
}

//--------------------------------------------------------------------------------------------------------
function validate_phone(str)  {

  var i = 0;
  var l = str.length;

    while (i < l)  {
        if(str.charAt(i) == '-')  {

            str = str.slice(0, i) + str.slice(i+1, str.length);

            l = str.length;

        } else {
            i++;
        }
    }

  return (str);
}

//--------------------------------------------------------------------------------------------------------
function validate_us_zip(zip)  {

    zip = trim(zip);
    zip = String(zip);

    if(zip.length < 5){
        return("The zip code does not appear to be valid.\r\n");
    }

    var first_part = zip.slice(0, 5);

    if(!isNumber(first_part)){
        return("The zip code does not appear to be valid.\r\n");
    }

    if(zip.length > 5){

        if(zip.length != 10){
            return("The zip code does not appear to be valid.\r\n");
        }

        if(zip.charAt(5) != '-'){
            return("The zip code does not appear to be valid.\r\n");
        }

        var second_part = zip.slice(6, zip.length);

        if(!isNumber(second_part)){
            return("The zip code does not appear to be valid.\r\n");
        }
    }

  return ('');
}

//--------------------------------------------------------------------------------------------------------
function form_submit(url)    {

    document.forms['reg'].action= url;
    document.forms['reg'].submit();

  return true;
}

//--------------------------------------------------------------------------------------------------------
function verifyForm()  {
  var email;
//  var email2;  
  var first_name;
  var last_name;
  var postitle;
  var company;
//  var street;
//  var city;
  var state;
//  var zip;
  var country;
  var phone;
  //var fax;
  var msg = '';
  var referral;
//  var industry;
  var employees;
  var timeframe;
  var servers;
  var password;
//  var password2;

     //employees = document.forms['reg'].elements['employees'][document.forms['reg'].elements['employees'].selectedIndex].value
  email = document.forms['reg'].email.value;
//  email2 = document.forms['reg'].email2.value;
  password = document.forms['reg'].password.value;
//  password2 = document.forms['reg'].password2.value;
  first_name = document.forms['reg'].firstname.value;
  last_name = document.forms['reg'].lastname.value;
  postitle = document.forms['reg'].pos.value
  company = document.forms['reg'].company.value;
  street = document.forms['reg'].street.value;
  city = document.forms['reg'].city.value;
  state = document.forms['reg'].elements['state'][document.forms['reg'].elements['state'].selectedIndex].value
  zip = document.forms['reg'].zip.value;
  country = document.forms['reg'].elements['country'][document.forms['reg'].elements['country'].selectedIndex].value;
  phone = document.forms['reg'].phone.value;
//  industry = document.forms['reg'].elements['industry'].selectedIndex

  if (!email) {
    msg = msg + "Please enter your email address.\r\n";
  } else {
    // var pattern = /^[\w-\.]+@[\w-\.]+\.[\w-\.]{2,}$/;
    var pattern = /^[\w-\.]+@([-\w]+\.)+\w\w+$/;
      if (!pattern.test(email) || test_nonreg(email,"")!= '')  {
        msg = msg + "This does not appear to be a valid email address.\r\n";
      }
    }

//	if (!email2)
//      msg = msg + "Please confirm your email address.\r\n";
//    else if(email != email2)
//      msg = msg + "Your email addresses do not match.\r\n";
	  
    msg = msg +  test_nonreg(email,"Incorrect email address: ");
	
    if (!password)
      msg = msg + "Please enter in your password.\r\n";
	else if(password.length < 6)
      msg = msg + "Your password must be at least 6 characters.\r\n";

//    if (!password2)
//      msg = msg + "Please confirm your password.\r\n";
//    else if(password != password2)
//      msg = msg + "Your passwords do not match.\r\n";
//    else
//    if(password.length < 6)
//      msg = msg + "Your password must be at least 6 characters.\r\n";

    msg = msg +  test_nonreg(password,"Incorrect password: ");

    if (!first_name)  {
      msg = msg + "Please enter your first name.\r\n";
    }

    msg = msg +  test_nonreg(first_name,"Incorrect first name: ");

    if (!last_name)    {
        msg = msg + "Please enter your last name.\r\n";
    }

    msg = msg +  test_nonreg(last_name,"Incorrect last name: ");

    if (!postitle)    {
        msg = msg + "Please enter your position or title.\r\n";
    }

    msg = msg +  test_nonreg(postitle,"Incorrect position or title: ");

    if (!company)    {
        msg = msg + "Please enter your company name.\r\n";
    }

    if(company.length < 3 && company) {
         msg = msg + "Please enter a company name with more than 2 characters.\r\n";
    }

    msg = msg +  test_nonreg(company,"Incorrect company: ");

    if (!street)    {
        msg = msg + "Please enter your business street address.\r\n";
    }

    msg = msg +  test_nonreg(street,"Incorrect street: ");

    if (!city)    {
        msg = msg + "Please enter the city in which your company/branch conducts business.\r\n";
    }

    msg = msg +  test_nonreg(city,"Incorrect city: ");

    if (country=="203")    {
        if (state == -1 || state > 61)    {
            msg = msg + "Please enter the state in which your company/branch conducts business.\r\n";
           } else if (state < 11)    {
            msg = msg + "Please enter the valid US state in which your company/branch conducts business.\r\n";
        }
    }

    if (country == 33 && (state < 63 && state > 10))    {
        msg = msg + "Please enter your Province.\r\n";
    }


    if (!zip)    {
        msg = msg + "Please enter your business ZIP code.\r\n";
    }  else  {
        if (country == "203")    {
            msg = msg + validate_us_zip(zip, "");
        }
    }

    msg = msg +  test_nonreg(zip,"Incorrect ZIP code: ");

    if (!country)    {
        msg = msg + "Please choose the country in which your company/branch conducts business.\r\n";
    }  else  {
        if (country=="Choose one")  {
            msg = msg + "Please choose the country in which your company/branch conducts business.\r\n";
        }
    }

    if (!phone)  {
        msg = msg + "Please enter your telephone number.\r\n";
    }  else if(phone.length < 7 )  {
        msg = msg + "Your telephone number does not appear to be valid.\r\n";
    }  else if(country=="203" && !validate_us_phone(phone)) {
        msg = msg + "Your telephone number does not appear to be valid.\r\n";
    }

    msg = msg +  test_nonreg(phone,"Incorrect phone: ");

//    if (!industry)  {
//        msg = msg + "Please select your Industry.\r\n";
//    }


  if (msg.length > 0)  {
    alert(msg);
    return false;
  }

  if (submit_flag != 0) {
    return false;
  } else  {
    submit_flag = 1;
  }

  return true;
}

function verifySupportForm()  {
  var msg = '';
  if (!document.forms['reg'].flname.value || document.forms['reg'].flname.value == ' ') {
    msg = msg + "Please enter your name\n";
  }
  if (!document.forms['reg'].company.value) {
    msg = msg + "Please enter your company\n";
  }
  if (!document.forms['reg'].phone.value) {
    msg = msg + "Please enter your phone number\n";
  }
  if (!document.forms['reg'].email.value) {
    msg = msg + "Please enter your E-mail\n";
  }
  if (!document.forms['reg'].product.selectedIndex) {
    msg = msg + "Please specify a product\n";
  }
  if (!document.forms['reg'].ver.value) {
    msg = msg + "Please specify product version\n";
  }
  if (!document.forms['reg'].desc.value) {
    msg = msg + "Please enter problem description\n";
  }
  if (msg.length > 0)  {
    alert(msg);
    return false;
  }
  return true;
}


