﻿function emptyvalidation(entered, alertbox)
{

    with (entered)
    {
        if (value==null || value=="")
        {
            if (alertbox!="") 
            {
                alert(alertbox);
            }
           
           entered.style.background = 'LightYellow'; 
            entered.focus();
            entered.select();
            return false;
        }
        else
        {
            return true;
        }
 
  
  }
  }


function isBlankSpace(field,msg)
    {
    /////////////   var digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*-()+'=_<>?[]{}//\\|.";
   //     field.value=field.value.replace(/(^ +| +$)/, "");
//alert(field.value.indexOf(' '));
var x=trimAll(field.value);

if (x=="") 
{

field.value="";
            field.style.background = 'LightYellow'; 
            field.focus();
            field.select();
            alert(msg);
            return false;
}
 else
        {
            //alert('dddd');
            return true; 
        }
        field.style.background = 'White'; 

//        if(field.value.indexOf(' ')>-1)
//        {
//        field.value="";
//            field.style.background = 'LightYellow'; 
//            field.focus();
//            field.select();
//            alert(msg);
//            return false;
//        }
//        else
//        {
//            //alert('dddd');
//            return true; 
//        }
//        field.style.background = 'White'; 
    }
    
    
    //  Sepcial Characters
    
    function isSpecialChar(field,msg,usfg)
{

var digits;

if (usfg=="fname")
{
var x=NameValidation(field.value);

if (x=="y"){
return true;
}
else
{
 field.style.background = 'LightYellow';
                        field.focus();
                        field.select();
                        alert(msg);
return false;
}
field.style.background = 'White';

}


if (usfg=="address" || usfg=="company")
{
var x=OtherValidation(field.value);


if (x=="y"){
return true;
}
else
{
 field.style.background = 'LightYellow';
                        field.focus();
                        field.select();
                        alert(msg);
return false;
}
field.style.background = 'White';

}

if (usfg=="username" || usfg=="password")
{
var x=UserValidation(field.value);

if (x=="y"){
return true;
}
else
{
 field.style.background = 'LightYellow';
                        field.focus();
                        field.select();
                        alert(msg);
return false;
}
field.style.background = 'White';
}

if (usfg=="other")
{

//digits="`~@!#$%^&,*()+{}[]?/\=-_><";
digits="`~$%^*+{}[]?=><";
}

if (usfg=="zip")
{
var x=CaseValidation(field.value);
if (x=="y"){
return true;
}
else
{
 field.style.background = 'LightYellow';
                        field.focus();
                        field.select();
                        alert(msg);
return false;
}
field.style.background = 'White';
}








if (usfg=="phone")
{
digits="`~@!#$%^&,*()+{}[]?/\=-_><";
}
if (usfg=="city")
{
var x=AlphaNumeric(field.value);
if (x=="y"){
return true;
}
else
{
 field.style.background = 'LightYellow';
                        field.focus();
                        field.select();
                        alert(msg);
return false;
}
field.style.background = 'White';
}





var v= chkcharc(digits,field,msg);
if (v==false)
{
return false;
}
else
{
return true;
}
}

function chkcharc(str,field,msg)
{
 var temp;
     for (var i=0;i<field.value.length;i++)
     {
               temp=field.value.substring(i,i+1);
               if (str.indexOf(temp)>-1)
               {
                        field.style.background = 'LightYellow';
                        field.focus();
                        field.select();
                        alert(msg);
                  return false;
                     
               }
     }   
    field.style.background = 'White';   
 
}

function PasswordConfirm(fld1,msg,fld2)
{

if (fld1.value != fld2.value)

{
alert(msg);
return false;
}
}


function IsNumeric(sText,msg,allowDecimal)
{
   var ValidChars = "0123456789";
   if (allowDecimal == 1)
   {
	   ValidChars = ValidChars + ".";
	   }
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.value.length && IsNumber == true; i++) 
      { 
      Char = sText.value.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         alert(msg);
          sText.style.background = 'LightYellow';
         sText.focus();
         sText.select();
         IsNumber = false;
         }
      }
   return IsNumber;
}


function chkEmail(field,msg,type)
{
       
  var v;

v=emptyvalidation(field,'Email-id can not be blank');
if (v==true)
{

}
else
{
return false;
}


v=isBlankSpace(field,'Email-id can not be blank');
if (v==true)
{

}
else
{
return false;
}

       
       
       
       
        var emailPat = /^([a-zA-Z0-9])([a-zA-Z0-9\._-])*@(([a-zA-Z0-9])+(\.))+([a-zA-Z]{2,4})+$/ ;
        
        var emailid=TrimUsingWhileLoop(field.value);
        var matchArray = emailid.match(emailPat);
        if (matchArray == null)
        {
               field.style.background = 'LightYellow';
               field.focus();
               field.select();
               alert(msg);
               return false;
        }
    
        field.style.background = 'White'; 
}


function chkdropdown(fld,msg)
{
var st=fld.options.selectedIndex;
 if(st==0)
  {
   alert("Please Select a Store Type");
   return false;
   }
return true;


}


function chkZip(fld,msg,YesNo)
{
var v;

v=emptyvalidation(fld,'zip can not be blank');
if (v==true)
{

}
else
{
return false;
}


//v=isBlankSpace(fld,'zip can not be blank');
if(fld.value.indexOf(' ')>-1) 
{
fld.style.background = 'LightYellow';
               fld.focus();
               fld.select();
               alert(msg);
return false;
}
else
{
fld.style.background = 'White';

}


if (YesNo==0)
{
v=IsNumeric(fld,msg,0);
}
if (YesNo=="other")
{
v=isSpecialChar(fld,msg,YesNo)
}

if (v==false)
{
//alert(msg);
return false;
}
}

//////////////////////////////////////////
function validateScript(fld,msg,str)
{


if (msg=="name")
{
msg="First Name";
}

if (msg=="lname")
{
msg="Last Name";
}

if (msg=="address")
{
msg="Address";
}

if (msg=="city")
{
msg="City";
}

if (msg=="state")
{
msg="State";
}

if (msg=="zip")
{
msg="Zip";
}

if (msg=="phone")
{
msg="Billing Phone";
}



if (msg=="email")
{
msg="Email-ID";
}

if (msg=="uname")
{
msg="User name";
}

if (msg=="password")
{
msg="password";
}








var v;
var showmsg;


//if (str=="username")
//{
showmsg=msg + " cannot be blank ";
v=emptyvalidation(fld,showmsg);
//}

if (v==true)
{
showmsg=msg+ " cannot be blank ";



v=isBlankSpace(fld,showmsg);



}
showmsg="";


if (v==true)
{
showmsg="Please Enter a Valid " + msg;


//if (str=="username")
//{

v=isSpecialChar(fld,showmsg,str)
//}




}

if (v==false)
{

return false;
}
return true;

}


function chkPhone(field,msg)
{

var v;
v=emptyvalidation(field,'Phone No. Can Not be Blank');
if (v==true)
{

}
else
{
return false;
}


v=isBlankSpace(field,'Phone No. Can Not be Blank');
if (v==true)
{

}
else
{
return false;
}

    var digits="0123456789-()";
    var temp;
     var ph=TrimUsingWhileLoop(field.value);
  
    if(ph.length<10)
    {
                        field.style.background = 'LightYellow'; 
                        field.focus();
                        field.select();
                        alert(msg);
                        return false;
    }
  
    for (var i=0;i<field.value.length;i++)
    {
                  temp=ph.substring(i,i+1);
               if (digits.indexOf(temp)==-1)
               {
                        field.style.background = 'LightYellow'; 
                        field.focus();
                        field.select();
                        alert(msg);
                        return false;
               }
    }  
    field.style.background = 'White'; 
  
}



function trimAll(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 ChkBlanknotallowed(field,msg)
    {
    /////////////   var digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*-()+'=_<>?[]{}//\\|.";
   //     field.value=field.value.replace(/(^ +| +$)/, "");
//alert(field.value.indexOf(' '));


        if(field.value.indexOf(' ')>-1)
        {
//        field.value="";
            field.style.background = 'LightYellow'; 
            field.focus();
            field.select();
            alert(msg);
            return false;
        }
        else
        {
            //alert('dddd');
            return true; 
        }
        field.style.background = 'White'; 
    }






////--------------------------------


//function alphanumeric(alphane)
//{
//	var numaric = alphane;
//	for(var j=0; j<numaric.length; j++)
//		{
//		  var alphaa = numaric.charAt(j);
//		  var hh = alphaa.charCodeAt(0);
//		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
//		  {
//		  }
//		else	{
//			 return false;
//		  }
//		}
// return true;
//}

//////---------------------------

function chkUser(ti)
{
	//var ti=document.g.tim.value;
			
			alert(ti);
			if (ti.search(/^[a-zA-Z0-9]$/-/_/) == -1) 
				{
					alert("invalid");
					//document.g.tim.focus();
					return false;
				} 
			return true;
		}



function CaseValidation(alphanum){
alphanum=TrimUsingWhileLoop(alphanum);

var test= /[A-Za-z0-9\._-]\w*$/;


if (!alphanum.match(test)) {
     //alert("Invalid Case.  Must be alpha-numeric.");
     var valid = "n";
    return valid;
     }
     else
     {
     var valid = "y";
     return valid;
     }
}


/////////////////////////////////
function AlphaNumeric(alphanum){
//alert(alphanum);
alphanum=TrimUsingWhileLoop(alphanum);
//var test= /[A-Za-z0-9]\w*$/;

var test=/^[a-zA-Z0-9]*$/;

if (!alphanum.match(test)) {
     var valid = "n";
    return valid;
     }
     else
     {
     var valid = "y";
     return valid;
     }
}







function NameValidation(alphanum){
//alert(alphanum);
alphanum=TrimUsingWhileLoop(alphanum);
var test= /[A-Za-z]\w*$/;

//alert(test);
if (!alphanum.match(test)) {
    // alert("Invalid Case.  Must be alpha-numeric.");
     var valid = "n";
    return valid;
     }
     else
     {
     var valid = "y";
     return valid;
     }
}



function OtherValidation(alphanum){
alphanum=TrimUsingWhileLoop(alphanum);
//var test= /[A-Za-z0-9\._-*&@!+()/\#%{}$]\w*$/;   //april
//var test= /[A-Za-z0-9{@#$)/:;'("}._-]\w*$/;       //May 13
var test= /[A-Za-z0-9 {&@#$(:;/%+! ')""}._-]\w*$/;



if (!alphanum.match(test)) {
    // alert("Invalid Case.  Must be alpha-numeric.");
     var valid = "n";
    return valid;
     }
     else
     {
     var valid = "y";
     return valid;
     }
}





function UserValidation(alphanum){
//var test= /[A-Za-z0-9\._-*&@!+()/\#%{}$]\w*$/;

//alphanum=TrimUsingWhileLoop(alphanum);
var alp=alphanum;
alp=TrimUsingWhileLoop(alp);
var test= /[A-Za-z0-9@#$._-~`!$%^&*()-]\w*$/;

if (!alp.match(test)) {
 //alert("Invalid Case.  Must be alpha-numeric.");
     var valid = "n";
    return valid;
     }
     else
     {
     var valid = "y";
     return valid;
     }
}



function TrimUsingWhileLoop(str)
{
  while(str.charAt(0) == (" ") )
  {  str = str.substring(1);
  }
  while(str.charAt(str.length-1) == " " )
  {  str = str.substring(0,str.length-1);
  }
  return str;
}














