/*************************************************************************
Form Validation Functoins
**************************************************************************/
var err_bg = '#ff6666';
var valid_bg = '#ffffff';

function isRequired(obj) {
	return (obj.value != '')?true:false;
}
/*=======================================================================*/
function isNumbers(obj) {
	var val, p;
	val = obj.value;
	if (val == '')
		return true;
	p=parseFloat(val);
	return (isNaN(p))?false:true;
}
/*=======================================================================*/
function isEmail(obj) {
	var val, p;
	val = obj.value;
	if (val == '')
		return true;
	p=val.indexOf('@');
	return (p<1 || p==(val.length-1))?false:true;
}
/*=======================================================================*/
function isPhone(obj) {
	var val, p;
	val = obj.value;
	if (val == '')
		return true;
//	check for valid phone number 
}
/*=======================================================================*/
function inRange(obj, range) {
	var val, p, min, max;
	val = obj.value;	
	p=range.indexOf(':');
	min=range.substring(8,p); 
	max=range.substring(p+1);
	return (val<min || max<val)?false:true; 
}
/*=======================================================================*/
function isEqual(obj1, obj2) {
	return (obj1.value == obj2.value)?true:false;
}
/*=======================================================================*/
function mark(obj, val, star) {
	if (val) { //error
		if (star) {
			aObj = document.getElementById('_' + obj.name);
			aObj.style.display = 'block';
		}
		obj.style.background =  err_bg;
	} else {
		if (star) {
			aObj = document.getElementById('_' + obj.name);
			aObj.style.display = 'none';
		}
		obj.style.background = valid_bg;
	}
}
/*=======================================================================*/
function findObj(n, d) { //v4.01
	var p,i,x;  
	if(!d) d=document; 
  	if((p=n.indexOf("?"))>0&&parent.frames.length) {
    	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
  	if(!(x=d[n])&&d.all) 
  		x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
  	for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=findObj(n,d.layers[i].document);
  	if(!x && d.getElementById) 
		x=d.getElementById(n); 
	return x;
}

function validateForm() {
  	var err_obj, obj,val,i,name,test,range,min,max,errors='',args=validateForm.arguments;
	
	var errDialog = args[0]; //alert list all error
	var errGroup = args[1];  //err group field
	var errGroupId = args[2];// err field id
	var errStar = args[3];// err field id
	
	if (errGroup) {
		err_obj = document.getElementById(errGroupId);
		err_obj.style.display = 'none';
	}

	
  	for (i=4; i<(args.length-2); i+=3) { 
		obj=findObj(args[i]);
		val=args[i+1];
  		test=args[i+2];
		name = obj.name;
		mark(obj, false, errStar);
    	if (obj) { 
      		if (test.indexOf('C')!=-1) {
				obj2 = findObj(val)
				mark(obj2, false);
				if (!isEqual(obj, obj2)) {
					errors+='- Incorrect value for \"'+name+'\" field.\n';
					mark(obj, true, errStar);
					mark(findObj(val), true, errStar);
					continue;
				}
			}
      		if (test.indexOf('E')!=-1) { 
				if (!isEmail(obj)) {
					errors+='- \"'+name+'\" must contain an e-mail address.\n';
					mark(obj, true, errStar);
					continue;
				}
			}
      		if (test.indexOf('N')!=-1) { 
				if (!isNumber(obj)) {
					errors+='- \"'+name+'\" must contain a number.\n';
					mark(obj, true, errStar);
					continue;
				}
			}
      		if (test.indexOf('B')!=-1) { 
				if (!isRange(obj, val)) {
					errors+='- \"'+name+'\" must contain a number between '+ range +'.\n';
					mark(obj, true, errStar);
					continue;
				}
			}
      		if (test.indexOf('R')!=-1) {
				if (!isRequired(obj)) {
					errors += '- \"'+name+'\" is required.\n'; 
					mark(obj, true, errStar);
					continue;
				}
			}
      		if (test.indexOf('P')!=-1) { 
				if (!isPhone(obj)) {
					errors += '- \"'+name+'\" must contain a valid Phone number.\n'; 
					mark(obj, true, errStar);
					continue;
				}
			}
      		if (test.indexOf('X')!=-1) { 
				if (!obj.checked) {
					errors += '- \"'+name+'\" must be checked.\n'; 
					mark(obj, true, errStar);
					continue;
				}
			}
  		} 
	}
		
	if (errors && errDialog) {
		alert('The following fields required:\n' + errors);
	}

	if (errors && errGroup) {
		while (errors.indexOf('\n') != -1)
			errors = errors.replace('\n', '<br/>');
		err_obj.style.display = 'block';
		err_obj.innerHTML = 'The following error(s) occurred:<br/>' + errors;
	}
		
	document.returnValue = (errors == '');
}

/*************************************************************************
Delete Confirmation
**************************************************************************/
function confirmDelete ( url ) {
	var res = confirm("Delete Confirmation!\nThis action will delete the selected record.\nClick Ok to Delete or Cancel to Abort.!");
	if (res)
 		window.location = url;
}

