// ORM Javascript

function validateDate(obj, evt) {
	var var1 = obj.value;

	//alert(event);

	var valid = true;

	var vd = '';
	var vm = '';
	var vy = '';

	// ERROR WASHING
	if(var1.length > 10) var1 = var1.substring(0,10);

	if(var1.substring(0,1) == '/') var1 = '';

	var rexp = /\/\//;
	if(rexp.test(var1)) var1 = var1.replace(rexp, '/');

	//rexp2 = /[a-Z]/;
	//var1 = var1.replace(rexp2, '');


	// PARSE AND FIX DATE SYNTAX
	var element_count = 1;
	if(var1.split('/').length > 1) {
		var splitstr = var1.split('/');
		element_count = splitstr.length;
		vm = splitstr[0];
		vd = splitstr[1];
		if(splitstr.length > 2) vy = splitstr[2];
	} else {
		vm = var1;
	}

	// MONTH
	if(vm.length > 2) {
		element_count = 2;
		vd = vm.substring(2);
		vm = vm.substring(0,2);
	}
	if(vm.length < 2 && element_count > 1) vm = '0' + vm;
	if(evt == 'blur' && var1.length > 0 && vm.length == 2 && (vm < 1 || vm > 12)) {
		//alert('Invalid date.');
		vm = '';
		//obj.select();
		valid = false;
	}

	// DAY
	if(vd.length > 2) {
		element_count = 3;
		vy = vd.substring(2);
		vd = vd.substring(0,2);
	}
	if(vd.length < 2 && element_count > 2) vd = '0' + vd;
	if(evt == 'blur' && var1.length > 0 && vd.length > 1 && (vd < 1 || vd > 31)) {
		//alert('Invalid date.');
		vd = '';
		//obj.select();
		valid = false;
	}

	// YEAR
	if(evt == 'key') {}

	if(evt == 'blur') {
		if(vy.length == 2) vy = '20' + vy;
		//if(var1.length > 0 && (vy.length < 2 || vy.length == 3)) valid = false; //alert('Invalid date.');
		if(var1.length > 0 && (vy.length < 4)) valid = false; //alert('Invalid date.');
		//obj.select();
	}

	// FINAL WASH
	if(rexp.test(var1)) var1 = var1.replace(rexp, '/');

	// RECONSTRUCT
	if(element_count > 0) var1 = vm;
	if(element_count > 1) var1 += '/' + vd;
	if(element_count > 2) var1 += '/' + vy;

	obj.value = var1;

	if(!valid) {
		alert('Invalid date (' + obj.name + ')');
		obj.value = '';
		obj.focus();
	}
}



function validatePhone(obj) {
	var1 = obj.value;
	//alert(var1);
	if(var1.length == 3) {
		obj.value += '-';
		//alert(var1);
	} else if(var1.length == 7) {
		obj.value += '-';
		//alert(var1);
	}
}

function validateSSN(obj) {
	var1 = obj.value;
	//alert(var1);
	if(var1.length == 3) {
		obj.value += '-';
		//alert(var1);
	} else if(var1.length == 6) {
		obj.value += '-';
		//alert(var1);
	} else if(var1.length > 1) {
		obj.value = obj.value.substring(0,11);
	}
}

function validateZip(obj) {
	// need to perform NaN check
	var1 = obj.value;
	//alert(var1);
	if(var1.length == 5) {
		obj.value += '-';
		//alert(var1);
	} else if(var1.length > 10) {
		obj.value = obj.value.substring(0,10);
	}
}

function validateEmail(obj) {
	var1 = obj.value;
	if(var1.indexOf('@') == -1 && var1.length > 0) {
		alert('Invalid e-mail format.');
		obj.select();
	}
}


function clearFields(x) {
	if(document.getElementById(x + '_display')) document.getElementById(x + '_display').value = '';
	if(document.getElementById(x + '_id')) document.getElementById(x + '_id').value = '';
}



function validateAmount(obj) {
	var n = new Number(obj.value);
	//alert(n.toFixed(2));
	if(n.toString() == 'NaN') {
		alert('Invalid amount.');
		n = 0;
	}
	obj.value = n.toFixed(2);
}




function validateDate2(obj, evt) {
	var var1 = obj.value;

	//alert(event);

	var valid = true;

	var vm = '';
	var vy = '';

	// ERROR WASHING
	if(var1.length > 7) var1 = var1.substring(0,7);

	if(var1.substring(0,1) == '/') var1 = '';

	var rexp = /\/\//;
	if(rexp.test(var1)) var1 = var1.replace(rexp, '/');

	//rexp2 = /[a-Z]/;
	//var1 = var1.replace(rexp2, '');


	// PARSE AND FIX DATE SYNTAX
	var element_count = 1;
	if(var1.split('/').length > 1) {
		var splitstr = var1.split('/');
		element_count = splitstr.length;
		vm = splitstr[0];
		if(splitstr.length > 1) vy = splitstr[1];
	} else {
		vm = var1;
	}


	// MONTH
	if(vm.length > 2) {
		element_count = 2;
		vy = vm.substring(2);
		vm = vm.substring(0,2);
	}
	if(vm.length < 2 && element_count > 1) vm = '0' + vm;
	if(evt == 'blur' && var1.length > 0 && vm.length == 2 && (vm < 1 || vm > 12)) {
		//alert('Invalid date.');
		vm = '';
		//obj.select();
		valid = false;
	}


	// YEAR
	if(evt == 'key') {}

	if(evt == 'blur') {
		if(vy.length == 2) vy = '20' + vy;
		if(var1.length > 0 && (vy.length < 2 || vy.length == 3)) valid = false; //alert('Invalid date.');
		//obj.select();
	}

	// FINAL WASH
	if(rexp.test(var1)) var1 = var1.replace(rexp, '/');

	// RECONSTRUCT
	if(element_count > 0) var1 = vm;
	if(element_count > 1) var1 += '/' + vy;

	obj.value = var1;

	if(!valid) {
		alert('Invalid date (' + obj.name + ')');
		obj.value = '';
		obj.focus();
	}
}





function selectList(obj,name) {
	if(1 < 2) {
	if(frames[0]) {
		//alert('no parent exists');
		//alert(parent.window.name);
		if(wintype) {
			if(wintype == 'main') {
				if(window['puwin']) {
					//alert('puwin already open');
					puwin.close();
				}
				puwin = window.open('/PU_SelectList.jsp?objtype=' + obj + '&target=' + name + '&currentid=' + document.getElementById(name + '_id').value, 'puwin', 'width=800,height=450,top=50,left=50,status=yes,scrollbars=yes');
			}
		}
	} else if(!frames[0]) {
		//alert('parent exists');
		//alert(parent.window.name);
		//alert(parent.wintype);
		if(parent.wintype == 'pu') {
			parent.frames['frame_list'].location.href = '/if_list_' + obj + '.jsp?objtype=' + obj + '&target=' + name + '&currentid=' + document.getElementById(name + '_id').value;
		} else if(parent.wintype == 'main') {
			if(window['puwin']) {
				//alert('puwin already open');
				puwin.close();
			}
			var puwin = window.open('/PU_SelectList.jsp?objtype=' + obj + '&target=' + name + '&currentid=' + document.getElementById(name + '_id').value, 'puwin', 'width=800,height=450,top=50,left=50,status=yes,scrollbars=yes');
		}
	}
	}
}

