// Clinix Javascript
function getDaysDiff(obj1, obj2, obj_out, offset) {
	var start = obj1.value;
	var end = obj2.value;
	if(start.length == 10 && end.length == 10) {
		//alert(start.substring(6,10));
		//alert(start.substring(3,5));	
		//alert(start.substring(0,2));
		var start_date = new Date(start.substring(6,10), start.substring(0,2) - 1, start.substring(3,5));
		var end_date = new Date(end.substring(6,10), end.substring(0,2) - 1, end.substring(3,5));
		//alert(start_date);
		//alert(end_date);
		var diff = (end_date.getTime() - start_date.getTime()) + (86400000 * offset) + 86400000;
		var days = (diff/86400000);
		//alert(end_date.getTime() + ' - ' + start_date.getTime());
		//alert(end_date.toString() + ' - ' + start_date.toString());
		//alert(days);
		obj_out.value = Math.round(days);
	} else {
		obj_out.value = '0';
	}
}



function getDaysDiff2(obj1, obj2, obj_out, offset) {
	var start = obj1.value;
	var end = obj2.value;
	if(start.length == 10 && end.length == 10) {
		alert(start.substring(6,10));
		alert(start.substring(3,5));	
		alert(start.substring(0,2));
		var start_date = new Date(start.substring(6,10), start.substring(0,2) - 1, start.substring(3,5));
		var end_date = new Date(end.substring(6,10), end.substring(0,2) - 1, end.substring(3,5));
		alert(start_date);
		alert(end_date);
		var diff = end_date.getTime() - start_date.getTime();
		var days = (diff/86400000);
		alert(end_date.getTime() + ' - ' + start_date.getTime());
		alert(end_date.toString() + ' - ' + start_date.toString());
		alert(days);
		obj_out.value = Math.round(days);
	} else {
		obj_out.value = '0';
	}
}


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 + ')');
		alert('Invalid date.');
		obj.value = '';
		obj.focus();
	}

	//if(var1.length == 10) if(flagChange) flagChange();
}



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);
	}
	*/

	//alert('validate zip');

	if(var1.length > 5 && var1.charAt(5) != '-') {
		var1 = var1.substring(0,5) + '-' + var1.substring(5,var1.length);
		obj.value = 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));
	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 + ')');
		alert('Invalid date.');
		obj.value = '';
		obj.focus();
	}
}





function selectList(obj,name,batch) {
	//alert(name);
	var go_batch = false;
	if(batch != null) go_batch = batch;
	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=950,height=550,top=20,left=20,status=yes,scrollbars=yes');
				puwin = window.open('/PU_ContactManager.jsp?objtype=' + obj + '&target=' + name + '&currentid=' + document.getElementById(name + '_id').value + '&batch=' + batch + '&height=550', 'puwin', 'width=950,height=550,top=20,left=20,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=950,height=550,top=20,left=20,status=yes,scrollbars=yes');
			var puwin = window.open('/PU_ContactManager.jsp?objtype=' + obj + '&target=' + name + '&currentid=' + document.getElementById(name + '_id').value + '&batch=' + batch + '&height=550', 'puwin', 'width=950,height=550,top=20,left=20,status=yes,scrollbars=yes');
		}
	}
	}
}





// MOM (Mouse-Over-Menu)
/*
<div id='pu_info' onMouseOver="showmom=true" onMouseOut="showmom=false; hideMenu()" style='background-color:#cccccc;margin:0;visibility:hidden; width:220;height:220;z-index:1;overflow:hidden'></div>
*/


var mom_obj;
var mom_text = "this is a test";
var showmom = false;

function showMom(evt, mom_obj_name) {
	mom_obj = document.getElementById(mom_obj_name);
	//alert(evt.clientX + 'x' + evt.clientY);
	var posX = evt.clientX + 5;
	var posY = evt.clientY;
	//mom_obj.style.position = 'absolute; left:' + posX + 'px; top:' + posY + 'px';
	mom_obj.style.position = 'absolute';
	mom_obj.style.left = posX;
	mom_obj.style.top = posY;
	mom_obj.style.visibility = 'visible';
	mom_obj.innerHTML = mom_text;
}

function hideMom() {
	setTimeout("closeMom()", 50);
}

function closeMom() {
	if(!showmom) mom_obj.style.visibility = 'hidden';
	//if(showmom) alert(showmom);
}


function insertAtCursor(myField, myValue) {
	//alert(myField.innerHeight);
	//alert(myField.scrollTop);
	var ori_pos = myField.scrollTop;
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	} else if (myField.selectionStart || myField.selectionStart == '0') {
		//MOZILLA/NETSCAPE support
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
	myField.scrollTop = ori_pos;
}




function playAudio(fileid) {
	window.open('player.php?autostart=true&fileid=' + fileid, 'audio_player', 'width=200,height=100,top=200,left=200,scrollbars=no,menubar=no,status=no,titlebar=no');
}

