// JavaScript Document
var stat;


function FrontPage_Form1_Validator(theForm)
{

  if (theForm.custnum.value == "")
  {
    alert("Please enter a value for the \"Customer Number\" field.");
    theForm.custnum.focus();
    return (false);
  }

  if (theForm.custnum.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"Customer Number\" field.");
    theForm.custnum.focus();
    return (false);
  }

  if (theForm.custnum.value.length > 5)
  {
    alert("Please enter at most 5 characters in the \"Customer Number\" field.");
    theForm.custnum.focus();
    return (false);
  }

  var checkOK = "0123456789-.,";
  var checkStr = theForm.custnum.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Customer Number \" field.");
    theForm.custnum.focus();
    return (false);
  }

  if (decPoints > 1)
  {
    alert("Please enter a valid number in the \"Customer Number\" field.");
    theForm.custnum.focus();
    return (false);
  }
  
  if (theForm.fname.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.fname.focus();
    return (false);
  }

  if (theForm.lname.value == "")
  {
    alert("Please enter a value for the \"Last Name\" field.");
    theForm.lname.focus();
    return (false);
  }
  
   if (theForm.companyname.value == "")
  {
    alert("Please enter a value for the \"Company Name\" field.");
    theForm.lname.focus();
    return (false);
  }
 
  
  if (theForm.email.value == "")
  {
    alert("Please enter a value for the \"E-Mail\" field.");
    theForm.email.focus();
    return (false);
  }
	var emailID=theForm.email
	
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
  
 
 if(theForm.username.value == "") {
      alert("Error: Username cannot be blank!");
      theForm.username.focus();
      return false;
    }
    re = /^\w+$/;
    if(!re.test(theForm.username.value)) {
      alert("Error: Username must contain only letters, numbers and underscores!");
      theForm.username.focus();
      return false;
    }
	
	if (!checkUsername(theForm.username.value)) {
		theForm.username.focus();
		return false;
	}

    if(theForm.pwd1.value != "" && theForm.pwd1.value == theForm.pwd2.value) {
      if(theForm.pwd1.value.length < 6) {
        alert("Error: Password must contain at least six characters!");
        theForm.pwd1.focus();
        return false;
      }
      if(theForm.pwd1.value == theForm.username.value) {
        alert("Error: Password must be different from Username!");
        theForm.pwd1.focus();
        return false;
      }
    } else {
      alert("Error: Please check that you've entered and confirmed your password!");
      theForm.pwd1.focus();
      return false;
    }

 
 
 
 
  return (true);
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		 
 		 return true					
	}
function sendPostData(postValues) {
	var xmlHttp;  // The variable that makes Ajax possible!

	var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
	var url = "supportacctcheck.php";
	stat = 0;


	xmlHttp = new GetXmlHttpObject();
	if (xmlHttp==null) {
  		alert ("Your browser does not support AJAX!");
 		return;
 		} 

	// Create a function that will receive data sent from the server
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){

		//document.getElementById(outputDiv).innerHTML=xmlHttp.responseText;
			//alert ('Response = ' + xmlHttp.responseText);
		if (xmlHttp.responseText != 0) {
			alert ('Username Taken');
			document.getElementById('username').focus();
			stat = 0;
			return false;
			}
		else {stat = true; return true;}	
		}
	}


	xmlHttp.open("post", url,false);
    xmlHttp.setRequestHeader("Content-Type", contentType);
	xmlHttp.send(postValues); 
}

//Browser Support Code
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function checkUsername(field) {
 username = document.getElementById('username').value;
	var postValues = "username="+username;
	var stat2
	
	//alert ('post=  ' + postValues);
	stat2 = sendPostData(postValues);
	//alert ('stat=  ' + stat2);

	return stat;
}