// JavaScript Document
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
// Returns true if string s is empty or 
// whitespace characters only.
var reWhitespace = /^\s+$/
var reEmail = /^.+\@.+\..+$/

function isWhitespace (s)

{   // Is s empty?
    return (isEmpty(s) || reWhitespace.test(s));
}

function isEmail (s)

{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    
    else {
       return reEmail.test(s)
    }
}

function checkNumeric(e) 
		{
		
		var keynum
		var keychar
		var numcheck
		
		if(window.event) // IE
		{
			keynum = e.keyCode
		}
		else if(e.which) // Netscape/Firefox/Opera
		{
			keynum = e.which
		}
		
		
		if(keynum==8)
		{
			return true;	
		}
		else
		{
			keychar = String.fromCharCode(keynum)
			numcheck = /\d/
			return numcheck.test(keychar)	
		}
		/*if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
		else
		cntfield.value = maxlimit - field.value.length;*/
		}
		
function checkNumeric_dot(e) 
		{
		
		var keynum
		var keychar
		var numcheck
		
		if(window.event) // IE
		{
			keynum = e.keyCode
		}
		else if(e.which) // Netscape/Firefox/Opera
		{
			keynum = e.which
		}
		
		
		if(keynum==8)
		{
			return true;	
		}
		else
		{
			keychar = String.fromCharCode(keynum)
			numcheck = /\d|\./
			return numcheck.test(keychar)	
		}
		/*if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
		else
		cntfield.value = maxlimit - field.value.length;*/
		}
function set_textbox(obj,val,opt)
{
	if(opt==1)
	{
		if(obj.value==val)
		{
			obj.value=""
		}
	}
	if(opt==2)
	{
		if(obj.value=="")
		{
			obj.value=val
		}
	}
}

function textCounter(field,cntfield,maxlimit) 
		{
		
		if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
		else
		cntfield.value = maxlimit - field.value.length;
		}
		
function check_password_str(pwd,uid)
{
	
	
	document.getElementById("str_indi").style.visible="hidden"
	document.getElementById("container").style.visibility="hidden"
	
var test=pwd;

 document.getElementById("str_indi").style.width="0px";

//alert(test);
	
var password=document.getElementById(escape(pwd)).value;
var username=document.getElementById(escape(uid)).value;
//	alert(password);
	
  var level=passwordStrength(password,username);
  
//  alert(level);
 var str;
 
 if(level==0)
 {
	     document.getElementById("indicator").style.visibility="hidden"
	 // document.getElementById("indicator").innerHTML=str;  
 }
  
   if(level==1)
   {
	   str="Too short";   
	   document.getElementById("container").style.visibility="visible"
	   document.getElementById("str_indi").style.width="20px";
	   document.getElementById("str_indi").style.backgroundColor="#CC0000";
	   
	    document.getElementById("indicator").style.visibility="visible"
	  document.getElementById("indicator").innerHTML=str; 
   }
   else if(level==2)
   {
	   str="Bad password";
	    document.getElementById("container").style.visibility="visible"
	   document.getElementById("str_indi").style.width="50px";
	    document.getElementById("str_indi").style.backgroundColor="#FF0033";
	    document.getElementById("indicator").style.visibility="visible"
	  document.getElementById("indicator").innerHTML=str;   
   }
   
   else if(level==3)
   {
	   str="Good Password"
	    document.getElementById("container").style.visibility="visible"
	    document.getElementById("str_indi").style.width="150px";
		document.getElementById("str_indi").style.backgroundColor="#0099FF";
	     document.getElementById("indicator").style.visibility="visible"
	  document.getElementById("indicator").innerHTML=str;  
   }
  
  else if(level==4)
  {
	  
	  str="Strong Password";
	   document.getElementById("container").style.visibility="visible"
	  document.getElementById("str_indi").style.width="200px";
	  document.getElementById("str_indi").style.backgroundColor="#00CC66";
	  document.getElementById("indicator").style.visibility="visible"

document.getElementById("indicator").innerHTML=str; 
  }
  //alert(level);
  

 
 
	
	
}



function passwordStrength(password,username)
{
    score = 0 
    
    //password < 4
	 if(password.length<=0)
	 {
		  return 0;  
	 }
    if (password.length < 4 ) { return 1; }// too short
    
    //password == username
    if (password.toLowerCase()==username.toLowerCase()) return 2 // bad pass
    
    //password length
    score += password.length * 4
    score += ( checkRepetition(1,password).length - password.length ) * 1
    score += ( checkRepetition(2,password).length - password.length ) * 1
    score += ( checkRepetition(3,password).length - password.length ) * 1
    score += ( checkRepetition(4,password).length - password.length ) * 1

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 
    
    //password has 2 sybols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 
    
    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 
    
    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 
    
    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 
    
    //password is just a nubers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 
    
    //verifing 0 < score < 100
    if ( score < 0 )  score = 0 
    if ( score > 100 )  score = 100 
    
    if (score < 34 )  return 2 // bad pass
    if (score < 68 )  return 3  // good pass
    return 4  // string pass
}


// checkRepetition(1,'aaaaaaabcbc')   = 'abcbc'
// checkRepetition(2,'aaaaaaabcbc')   = 'aabc'
// checkRepetition(2,'aaaaaaabcdbcd') = 'aabcd'

function checkRepetition(pLen,str) 
{
    res = ""
    for ( i=0; i<str.length ; i++ ) 
	{
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) 
		{
            i+=pLen-1
            repeated=false
        }
        else
		{
            res+=str.charAt(i)
        }
    }
    return res
}

function popupstatic(url,hei,wid)
{
	var t=(screen.height/2)-(hei/2);
	var l=(screen.width/2)-(wid/2);

	newwindow=window.open(url,'name','height='+Math.abs(hei)+',width='+Math.abs(wid)+',top='+Math.abs(t)+',left='+Math.abs(l)+',scrollbars=yes');
	if (window.focus) {newwindow.focus()}
}

function printdoc()
{
	window.print();
}
