﻿

//setting the css based on validation

function setToInfo(id) {
    $j('#note-' + id).removeClass('form-note form-error form-success');
    $j('#note-' + id).addClass('form-note');
    $j('#note-' + id).text(info[id]);
    $j('#note-' + id).css('visibility', 'visible');
}

function setToError(id, error) {
    document.getElementById(id).innerHTML=error;
    document.getElementById(id).className='form-error';
}

function setToSuccess(id, success) {
    document.getElementById(id).innerHTML=success;
    document.getElementById(id).className='form-success';
}

// css part ends


var xmlHttp, pass

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;
}

// Check username exists

function showCustomer(str)
{ 
var minLen = 4;
var maxLen = 15;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
if(str == "")
{
    document.getElementById("username").innerHTML="Please Enter Username";
    
}
else
{
    if(str.length < minLen || str.length > maxLen)
    {       
       
        setToError('username', 'Username must contain minimum 4 characters, maximum 15 characters.');
               
    }
    else if((/[<>!@#$%^&*()\"/'()\[\]\{\},?+~]/.test(str)))
    {
      setToError('username', ' User name provided does not appear to be valid.');
    }
    else
    {
        var url="user.aspx";
        url=url+"?q="+str;
        url=url+"&sid="+Math.random();
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }
}
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
var result = xmlHttp.responseText;
    if(result == "yes")
    {
        setToSuccess('username', 'The username is available.');        
    }
    if(result == "no")
    {
        setToError('username', 'The username is already in use.');        
    }
}
}

function showUserMsg()
{
    document.getElementById("username").className='form-note';
    document.getElementById("username").innerHTML="minimum 4 characters, maximum 15 characters.";
}

//Check email address
function showEmailMsg()
{
    document.getElementById("email").className='form-note';
    document.getElementById("email").innerHTML="Enter a valid email address.";
}

function checkEmail(value)
{
    if (value == "")
    {
        setToError('email', 'Enter a valid email address.')
    }
    else
    {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value))
        {
            setToSuccess('email', 'Your email appears valid. This is a good thing.');
        }
        else
        {
            setToError('email', 'Invalid email address.');
        }
    }
}

//password validation
function showPwdMsg()
{
    document.getElementById("password").className='form-note';
    document.getElementById("password").innerHTML="Must be atleast 6 characters long";
}

function checkPwd(value)
{
    var space = " "; // Invalid character is a space
    var minLength = 6; // Minimum length
    var pw1 = value;
    // check for a value.
    if (pw1 != '')
    {
        // check for minimum length
        if (pw1.length < minLength) 
        {
            setToError('password', 'It must contain atleast 6 characters.');
        }
        else
        {
            setToSuccess('password', 'Your password looks good. Remember to remember it.');
        }
    }
    else
    {
        setToError('password', 'Password is a required field.');
    }
}

// Password Hint Required field
function showPwdHintMsg()
{
    document.getElementById("pwdhint").className='form-note';
    document.getElementById("pwdhint").innerHTML="Answer the password Question.";
    
}

function checkPwdHint(value)
{
var minLen = 4;
var maxLen = 40;
 
    if ( value == "" )
    {
        setToError('pwdhint', 'Please answer the password Question.');
    }
    else
    {
        if(value.length < minLen || value.length > maxLen)
        {
            setToError('pwdhint', 'Hint answer should not exceed more than 40 characters and  less then 4.');
            
        }
        else
        {
        setToSuccess('pwdhint','Good');
        }
    }
}



//First name required field
function showFNameMsg()
{
    document.getElementById("fname").className='form-note';
    document.getElementById("fname").innerHTML="Please enter your First Name.";
}

function checkFName(value)
{
var minLen = 1;
var maxLen = 30;

    if (value == "")
    {
        setToError('fname', 'First Name is a required field.');
    }
    else
    {
     if(value.length < minLen || value.length > maxLen)
        {
            setToError('fname', 'First Name should not exceed more than 30 characters and  less then 1.');
            
        }
         else if((/[<>!@#$%^&*()\"/'()\[\]\{\},?+~]/.test(value)))
        {
        setToError('fname', 'First name provided does not appear to be valid.');
        }
        else
        {
        setToSuccess('fname', 'Good');
        }
    }
}

//Last name required field
function showLNameMsg()
{
    document.getElementById("lname").className='form-note';
    document.getElementById("lname").innerHTML="Please enter your last name.";
}

function checkLName(value)
{
var minLen = 1;
var maxLen = 30;

    if (value == "")
    {
        setToError('lname', 'Last Name is a required field.');
    }
    else
    {
    if(value.length < minLen || value.length > maxLen)
        {
            setToError('lname', 'Last Name should not exceed more than 30 characters and  less then 1.');
            
        }
        else if((/[[<>!@#$%^&*()\"/'()\[\]\{\},?+~]/.test(value)))
        {
        setToError('lname', 'Last name provided does not appear to be valid.');
        }
        else
        {
         setToSuccess('lname', 'Good');
        }
    }
}
// Business  required field
function showBusinessMsg()
{

    document.getElementById("Busi").className='form-note';
    document.getElementById("Busi").innerHTML="Please enter your Business.";
}

function checkBusiness(value)
{
var minLen = 4;
var maxLen = 30;

 if (value == "")
    {
      
    }
    else
    {
       if(value.length < minLen || value.length > maxLen)
        {
            setToError('Busi', 'Business should not exceed more than 30 characters and  less then 4.');
            
        }
         else if((/[<>!@#$%^&*()\"/'()\[\]\{\},?+~]/.test(value)))
        {
        setToError('Busi', 'Business name provided does not appear to be valid.');
        }
        else
        {
        setToSuccess('Busi', 'Good');
        }
   }
   
}
//Address required field
function showAddressMsg()
{
    document.getElementById("address").className='form-note';
    document.getElementById("address").innerHTML="Please enter your Address.";
}

function checkAddress(value)
{

    if (value == "")
    {
        setToError('address', 'Address is a required field.');
    }
    else
    {
        setToSuccess('address', 'Good');
    }
}


//City required field
function showCityMsg()
{
    document.getElementById("city").className='form-note';
    document.getElementById("city").innerHTML="Please enter your City Name.";
}

function checkCity(value)
{
var minLen = 3;
var maxLen = 30;

    if (value == "")
    {
        setToError('city', 'City is a required field.');
    }
    else
    {
      if(value.length < minLen || value.length > maxLen)
        {
            setToError('city', 'City should not exceed more than 30 characters and  less then 3.');
            
        }
        else if((/[<>!@#$%^&*()\"/',1234567890?_+~\[\]\{\}]/.test(value)))
        {
        setToError('city', 'City name provided does not appear to be valid.');
        }
        else
        {
        setToSuccess('city', 'Good');
        }
    }
}

//State required field
function showStateMsg()
{
    document.getElementById("state").className='form-note';
    document.getElementById("state").innerHTML="Please enter your State Name.";
}

function checkState(value)
{
var minLen = 2;
var maxLen = 2;

    if (value == "")
    {
        setToError('state', 'State is a required field.');
    }
    else
    {
       if(value.length < minLen || value.length > maxLen)
        {
            setToError('state', 'State should not exceed more than 2 characters and  less then 2.');
            
        }
         else if((/[<>!@#$%^&*()\"/',1234567890?_+~\[\]\{\}]/.test(value)))
        {
        setToError('state', 'State name provided does not appear to be valid.');
        }
        else
        {
        setToSuccess('state', 'Good');
        }
    }
}


//Zip code validation
function showZipMsg()
{
    document.getElementById("zip").className='form-note';
    document.getElementById("zip").innerHTML="Please enter Zip Code.";
}

function checkZip(value)
{
    if (value == "")
    {
        setToError('zip', 'Zip code is a required field.');
    }
    else
    {
        if (value.length != 5)
        {
            setToError('zip', 'Please enter 5 digit zip code');
        }
        else
        {
            if(!(/^\d+$/.test(value)))
            {
                setToError('zip', 'Zip code provided does not appear to be valid.');
            }
            else
            {
                setToSuccess('zip', 'Good');
            }
        }
    }
}



//Web Address validation
function showWAddressMsg()
{
    document.getElementById("WAddress").className='form-note';
    document.getElementById("WAddress").innerHTML="Please enter Web Address.";
}

function checkWAddress(value)
{

if (value != "")
    {
        if(!(/^\www.\w+(\.\w{2,3})+$/.test(value)))
            {
                setToError('WAddress', 'Web Address provided does not appear to be valid.');
            }
            else
            {
                setToSuccess('WAddress', 'Good');
            }
    }
    else
    {
        setToSuccess('WAddress', 'Good');
    }   
}


// function for phone validation starts

    <!-- This script is based on the javascript code of Roman Feldblum (web.developer@programmer.net) -->
<!-- Original script : http://javascript.internet.com/forms/format-phone-number.html -->
<!-- Original script is revised by Eralper Yilmaz (http://www.eralper.com) -->
<!-- Revised script : http://www.kodyaz.com -->

var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}

ParseForNumber1(object)

if(keycode > 48){
ValidatePhone(object)
}
}

function backspacerDOWN(object,e) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}
ParseForNumber2(object)
} 

function GetCursorPosition(){

var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i=0; i<t1.length; i++)
{
if (t1.substring(i,1) != t2.substring(i,1)) {
if(!bool) {
cursorposition=i
bool=true
}
}
}
}

function ValidatePhone(object){

var p = phonevalue1

p = p.replace(/[^\d]*/gi,"")

if (p.length < 3) {
object.value=p
} else if(p.length==3){
pp=p;
d4=p.indexOf('(')
d5=p.indexOf(')')
if(d4==-1){
pp="("+pp;
}
if(d5==-1){
pp=pp+")";
}
object.value = pp;
} else if(p.length>3 && p.length < 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

object.value = pp; 

} else if(p.length >= 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

l40 = pp.length;
p40 = pp.substring(0,8);
p40 = p40 + "-"

p41 = pp.substring(8,l40);
ppp = p40 + p41;

object.value = ppp.substring(0, maxphonelength);
}

GetCursorPosition()

if(cursorposition >= 0){
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1=object.value.indexOf(')')
e2=object.value.indexOf('-')
if (e1>-1 && e2>-1){
if (e2-e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}

var txtRange = object.createTextRange();
txtRange.moveStart( "character", cursorposition);
txtRange.moveEnd( "character", cursorposition - object.value.length);
txtRange.select();
}

}

function ParseChar(sStr, sChar)
{
if (sChar.length == null) 
{
zChar = new Array(sChar);
}
else zChar = sChar;

for (i=0; i<zChar.length; i++)
{
sNewStr = "";

var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);

while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

sStr = sNewStr;
}

return sNewStr;
}

//function for phone validation ends

//Phone1 validation
function showPhone1Msg()
{
    document.getElementById("phone1").className='form-note';
    document.getElementById("phone1").innerHTML="Please enter your phone number.";
}

function checkPhone1(value)
{
    if (value.length == 13)
    {
        setToSuccess('phone1', 'Good');
    }
    else
    {
        setToError('phone1', 'Please enter a valid phone number');
    }
}

//Phone2 validation if entered
function showPhone2Msg()
{
    document.getElementById("phone2").className='form-note';
    document.getElementById("phone2").innerHTML="Please enter your phone number";
}

function checkPhone2(value)
{
    if (value != "")
    {
        if (value.length == 13)
        {
            setToSuccess('phone2', 'Good');
        }
        else
        {
            setToError('phone2', 'Please enter a valid phone number');
        }
    }
    else
    {
        setToSuccess('phone2', 'Good');
    }
}

//Company

function showCompany()
{
    document.getElementById("company").className='form-note';
    document.getElementById("company").innerHTML="Please enter your First Name.";
}

function checkCompany(value)
{
var minLen = 1;
var maxLen = 30;

    if (value == "")
    {
        setToError('company', 'Company is a required field.');
    }
    else
    {
     if(value.length < minLen || value.length > maxLen)
        {
            setToError('company', 'Company should not exceed more than 30 characters and  less then 1.');
            
        }
         else if((/[<>!@#$%^&*()\"/'()\[\]\{\},?+~]/.test(value)))
        {
        setToError('company', 'Company provided does not appear to be valid.');
        }
        else
        {
        setToSuccess('company', 'Good');
        }
    }
}


//Comments/Queries required field
function showCommentsMsg()
{
    document.getElementById("Comments").className='form-note';
    document.getElementById("Comments").innerHTML="Please enter your Comments.";
}

function checkComments(value)
{

    if (value == "")
    {
        setToError('Comments', 'Comments is a required field.');
    }
    else
    {
        setToSuccess('Comments', 'Good');
    }
}


