//BROWSER DETECTION
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
 
function DetectBrowser(){ //Detectarea browserului folosit
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bw=new DetectBrowser();

//Email Validation
function emailValid(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function fieldOb(field_name){
	if(ns6) field = eval("document.getElementById('"+field_name+"')");
	else if(ns4) field = eval("document."+field_name);
	else field = eval("document.all."+field_name);
	return field;
}

function changeRowColor(row, color){
	rOb = fieldOb(row);
	rOb.bgColor = color;  
}




var incomplete_fields = '';


//Validates a field
function ValidateField(elem, name){
	if((elem.value == '')||(elem.value == 0))	{
    		//alert('INCOMPLETE!\n\nThe following fields have not been filled: \n'+incomplete_fields);
			alert('Error: Please complete all mandatory fields!');
    		elem.focus();
   	 		return(false);
   	}
	else if(name.indexOf('Email') != -1){
			 	if(!emailValid(elem.value)){
					alert('Invalid E-mail address !');
					elem.focus();
   	 				return(false);
				}
	}
	return(true);
}

//Just Validates a field without (alert & focus)
function JustValidateField(elem){
	if((elem.value == '')||(elem.value == 0))	return(false);
	else if(name.indexOf('email') != -1){
			 	if(!emailValid(elem.value)){
   	 				return(false);
				}
	}
	return(true);
}


function BuildIncompleteFields(form, fields){
	incomplete_fields = '';

	mandatory_fields = fields.split(',');
	for(i=0; i<mandatory_fields.length; i++){
		//single field
		elem = eval('form.' + mandatory_fields[i]);
		if(!JustValidateField(elem, mandatory_fields[i])) incomplete_fields += ', ' + mandatory_fields[i];
	}//for

	incomplete_fields = incomplete_fields.substr(2, incomplete_fields.length-2);
}

//Validate form for Mandatory fields to be filled properly
function Validate(form, fields){
   	BuildIncompleteFields(form, fields);

	mandatory_fields = fields.split(',');
	for(i=0; i<mandatory_fields.length; i++){
		
		if(mandatory_fields[i].indexOf('|')){
			//groupped fields (at least one of them must be filled/selected)
			mandatory_group_fields = mandatory_fields[i].split('|');
			valid = 0;
			for(j=0; j<mandatory_group_fields.length; j++){
				elem = eval('form.' + mandatory_group_fields[j]);
				if(ValidateField(elem, mandatory_group_fields[j]) ) valid = 1;
				else return(false);
			}
			if(!valid){
				//no fields filled
				elem = eval('form.' + mandatory_group_fields[0]);
				if(!ValidateField(elem, mandatory_group_fields[0])) return(false);
			}
			
		}else{
			//single field
			elem = eval('form.' + mandatory_fields[i]);
			if(!ValidateField(elem, mandatory_fields[i])) return(false);
		}
		
	}//for
	
	return(true);
}




function ValidateLogin(form){
   	if(form.login_user.value == '')	{
    		alert('Insert Username!');
    		form.login_user.focus();
   	 	return(false);
   	}
   	if(form.login_pass.value == '')	{
    		alert('Insert Password!');
    		form.login_pass.focus();
   	 	return(false);
   	}
	return(true);
}