// JavaScript Document
var ctrlPressed=0;
var altPressed=0;
var shiftPressed=0;
function test_keys(e){
  var evt = navigator.appName=="Netscape" ? e:event;
  if (navigator.appName=="Netscape" && parseInt(navigator.appVersion)==4) {
   // NETSCAPE 4 CODE
   var mString =(e.modifiers+32).toString(2).substring(3,6);
   shiftPressed=(mString.charAt(0)=="1");
   ctrlPressed =(mString.charAt(1)=="1");
   altPressed  =(mString.charAt(2)=="1");
   self.status="modifiers="+e.modifiers+" ("+mString+")"
  }
  else {
   // NEWER BROWSERS [CROSS-PLATFORM]
   shiftPressed=evt.shiftKey;
   altPressed  =evt.altKey;
   ctrlPressed =evt.ctrlKey;
  }
}
function validate(e,validation){
	test_keys(e);
	var correct=false;
	var unicode;
	try{ unicode=e.keyCode;	} catch(e){	unicode=e.keyCode;	}
	//window.status=unicode;
	if((unicode>7 && unicode<10) || (unicode>34 && unicode<40) || unicode==13 || unicode==27 || unicode==127 || unicode==116 || unicode==46 || unicode==16){
		return true;	
	}
	if(validation.indexOf("n")>-1){
		if( ((unicode>=48 && unicode<=57) && shiftPressed==0)|| (unicode>=96 && unicode<=105) )correct=true;
	}
	if(validation.indexOf("a")>-1){
		if((unicode>=65 && unicode<=90))correct=true;
	}
	if(validation.indexOf("S")>-1){
		if(unicode==32)correct=true;
	}
	if(validation.indexOf(".")>-1 && shiftPressed==0){
		if(unicode==190)correct=true;
	}
	if(validation.indexOf("-")>-1 && shiftPressed==0){
		if(unicode==109)correct=true;
	}
	if(validation.indexOf("E")>-1){
		if(unicode==189 || (unicode==189 && shiftPressed==1) || (unicode==190 && shiftPressed==0) || (unicode==50 && shiftPressed==1) || ((unicode>=48 && unicode<=57) && shiftPressed==0) || (unicode>=65 && unicode<=90))correct=true;
	}
	if(validation.indexOf("p")>-1)	if( unicode==107 || (unicode==61 && shiftPressed==1) || (unicode==187 && shiftPressed==1))correct=true;
	if(correct!=true)return false;
	return true;
}
function get_id(id){
    var obj = null;
    if(document.getElementById){
        obj = document.getElementById(id);
    }else if(document.all){
        obj = document.all[id];
    }
    return obj;
}
function check_email(eml) {
  var eml_filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (eml_filter.test(eml))return true;
  else return false;
}
function set(type1,ITEMHERE){
	get_id(ITEMHERE).style.float="left";
	get_id(ITEMHERE).style.width="155px";			
	if(type1=="good"){

			get_id(ITEMHERE).style.backgroundColor="#EEFFEE";
			get_id(ITEMHERE).style.borderLeft="#33BB66 5px solid";

	}else{
			get_id(ITEMHERE).style.backgroundColor="#FFEEEE";		
			get_id(ITEMHERE).style.borderLeft="#FF9999 5px solid";											
	}
}
function matchit(item1,in_item2) {
  var re = new RegExp(item1);
  var m = re.exec(in_item2);
  if (m == null) return false;
  else return (m.index+1);
}
var bad="<img src='images/invalid.gif' border='0' alt='Error' align='absmiddle' /> ";
var good="<img src='images/validate.gif' border='0' alt='Valid' align='absmiddle' /> ";	
function validate_all(form_id){	
	var the_return=true;
	var form_here=get_id(form_id);
	for(i=0;i<form_here.elements.length;i++){
		if(form_here.elements[i].getAttribute("authtype") )
		{
			if(validate_item(form_here.elements[i].id,form_here.elements[i].getAttribute("authtype"))==false){
				the_return=false;
			}
		}
	}
	return the_return;
}
function initiate_validation(form_id){	
	var form_here=get_id(form_id);
	for(i=0;i<form_here.elements.length;i++){
		if(form_here.elements[i].getAttribute("authtype"))
		{
			form_here.elements[i].onblur=function(){
				validate_item(this.id,this.getAttribute("authtype"));
			}
		}
	}
}
function validate_item(itemID,check_against,min_size){
	if(!min_size)min_size=0;
	var msg=good;
	if(get_id(itemID+"_validation"))
	{
		finalID=itemID+"_validation";
	}else{
		var valid=document.createElement("span");
		valid.id=itemID+"_validation";
		if(get_id(itemID).nextSibling){
			get_id(itemID).nextSibling.parentNode.insertBefore(valid,get_id(itemID).nextSibling);
		}else{
			get_id(itemID).parentNode.appendChild(valid);
		}
		finalID=itemID+"_validation";
	}

	switch(check_against){	
	case "not_empty":
		if(get_id(itemID).value.length==0){
			msg=bad;
		}else{
			msg=good;
		}
	break;
	case "username":
		if(get_id("original_"+itemID)){
			if(get_id(itemID).value==get_id("original_"+itemID).value){
				msg=good;
			}
		}	
		if(matchit("[0-9]",get_id(itemID).value)==1){
			msg=bad;
		}
		else if(get_id(itemID).value.length<4){
			msg=bad;
		}
		else{
			msg=good;
		}
	break;
	case "password":
		if(get_id(itemID).value.length<6)
		{
			msg=bad;
		}
		else{
			msg=good;
		}
	break;	
	case "email":
		if(matchit("[0-9]",get_id(itemID).value)==1){
			msg=bad;
		}		
		else if(check_email(get_id(itemID).value)==false){
			msg=bad;
		}
		else{
		}	

	break;
	case "numerical":
		if(get_id(itemID).value.toString()!=parseInt(get_id(itemID).value.toString())){
			msg=bad;
		}		
		else{
			msg=good;			
		}	

	break;
	}
	if(get_id(itemID).getAttribute("required") && 
		get_id(itemID).getAttribute("required")=="false" && get_id(itemID).value==""){
		msg=good;	
	}
	if(get_id(itemID).getAttribute("min_size") &&  get_id(itemID).value.length<get_id(itemID).getAttribute("min_size")){ 
		if(get_id(itemID).getAttribute("required") && 
		get_id(itemID).getAttribute("required")=="false" && get_id(itemID).value==""){
			msg=good;	
		}else{
			msg=bad;
		}
	}
	if(get_id(itemID).getAttribute("repeat") && get_id(itemID).value!=get_id(get_id(itemID).getAttribute("repeat")).value){
		msg=bad;	
	}
	if(get_id(finalID)){
		get_id(finalID).style.fontSize="9px";
		get_id(finalID).innerHTML=msg;
		get_id(finalID).style.height="auto";
		get_id(finalID).style.fontFamily="Tahoma";
	}	
	if(msg.indexOf(bad)>-1){
		get_id(itemID).style.border='#DD0000 1px solid';
		get_id(itemID).style.background='#FFEEEE';
		//	get_id(itemID).style.marginTop="1px";get_id(itemID).style.marginBottom="1px";		
		return false;
	}
	else{
		get_id(itemID).style.border='#7F9DB9 1px solid';		
		get_id(itemID).style.background='#FFFFFF';
		//	get_id(itemID).style.marginTop="1px";get_id(itemID).style.marginBottom="1px";				
		return true;		
	}	

}