// VARIABLE DECLARATIONS
var digits = ".0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Browser Width and Height
var BrowserWidth=0;
var BrowserHeight=0;
window.onresize=get_browser_size;
function get_browser_size() {
	if(document.all) {
		BrowserWidth = document.body.offsetWidth;
		BrowserHeight = document.body.offsetHeight;
		} 
	else{
		BrowserWidth = window.innerWidth;
		BrowserHeight = window.innerHeight;
		}
	}
// Mouse x and y
var MouseX=0;
var MouseY=0;
document.onmousemove = mouseMove;
if(!document.all){document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);}
function mouseMove(e){
	if(document.all) {
		MouseX = event.x + document.body.scrollLeft;
		MouseY = event.y + document.body.scrollTop;
		}
	else{
		MouseX = e.pageX;
		MouseY = e.pageY;
		}
	return true;
	}
//tooltip
function tooltip(message) {
  	var tip = new getObj('tooltip');
	var tipcell = new getObj('tooltipcell');
  	var rx = MouseX;
  	var ry = MouseY;
	if(!message){
		tip.style.visibility ='hidden';
  		tip.style.display = 'none';
		return false;
		}
	get_browser_size();
  	if((MouseX+300) > BrowserWidth){rx=BrowserWidth-350;}
  	if((MouseY+300) > BrowserHeight){ry=BrowserHeight-350;}
	ry=ry+20;
	//alert(rx+'--'+ry);
  	tip.style.left=rx;
  	tip.style.top=ry;
	tipcell.obj.innerHTML=message;
  	tip.style.visibility ='visible';
  	tip.style.display = 'block';
	}
//growBox - Expands form textarea fields to full browser window.
function growBox(theField){
	get_browser_size();
	var gwidth=BrowserWidth-50;
	if(gwidth < 10){gwidth=10;}
	var gheight=BrowserHeight-50;
	if(gheight < 10){gheight=10;}
	var gobj=new getObj(theField);
	window.status="Resized textarea to "+gwidth+" by "+gheight+" pixels.";
	gobj.style.width=gwidth;
	gobj.style.height=gheight;
	}
//FormHide
function FormHide(obj,x) {
  	var fob = new getObj(obj);
	if(x==0){fob.style.visibility ='hidden';}
	else {fob.style.visibility ='hidden';}
	return false;
	}
//showdiv
function MouseObj(div,cmd){
  	var divobj = new getObj(div);
  	var rx = MouseX;
  	var ry = MouseY;
	if(cmd && cmd == 0){
		divobj.style.visibility ='hidden';
  		divobj.style.display = 'none';
		return false;
		}
	get_browser_size();
  	if((MouseX+300) > BrowserWidth){rx=BrowserWidth-350;}
  	if((MouseY+300) > BrowserHeight){ry=BrowserHeight-350;}
	divobj.style.position='absolute';
  	divobj.style.left=rx;
  	divobj.style.top=ry;
	if(cmd && cmd==1){
		divobj.style.visibility ='visible';
		divobj.style.display = 'block';
		}
	else if(cmd && cmd==0){
		divobj.style.visibility ='hidden';
		divobj.style.display = 'none';
		}
	else if(divobj.style.visibility == 'hidden' || divobj.style.display == 'none' || divobj.style.display == 'hide'){
		divobj.style.visibility ='visible';
		divobj.style.display = 'block';
		}
	else{
		divobj.style.visibility ='hidden';
		divobj.style.display = 'none';
		}
	return true;
	}
 // getObj
 function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.obj2 = document.getElementById(name);
		this.style = document.getElementById(name).style;
		} 
	else if (document.all) {
		this.obj = document.all[name];
		this.obj2 = document.all[name];
		this.style = document.all[name].style;
		} 
	else if (document.layers) {
		if (document.layers[name]) {
			this.obj = document.layers[name];
			this.obj2 = document.layers[name];
			this.style = document.layers[name];
			}	
		else {
			this.obj = document.layers.name.layers[name];
			this.obj2 = document.layers.name.layers[name];
			this.style = document.layers.name.layers[name];
			}
		}
	// capture objects x and y value
	var curleft=0;
	var curtop=0;
	while (this.obj2.offsetParent) {
    	curleft += this.obj2.offsetLeft;
		curtop += this.obj2.offsetTop;
		this.obj2 = this.obj2.offsetParent;
		}
	this.x=curleft;
	this.y=curtop;
	}
// Popup
function popup(obj,rel,x,y) {
	var the_object = new getObj(obj);
	var relative;
	var rx = 0;
	var ry = 0;
	get_browser_size();
	if(rel && rel!="mouse" && rel!="none" && rel!="topright" && rel!="topleft" && rel!="bottomright" && rel!="bottomleft" && rel!="center"){
		relative = new getObj(rel);
		rx = relative.x;
		ry = relative.y;
		}
	if(!rel){rel="mouse";}
	if(!x) x=10;
	if(!y) y=10;
	if(rel=="none" || rel=="topleft"){
		rx=0;
		ry=0;
		} 
	else if(rel=="topright"){
		rx=BrowserWidth + document.body.scrollLeft;
		ry=0;
		} 
	else if(rel=="bottomleft"){
		rx=0;
		ry=BrowserHeight+ document.body.scrollTop;
		} 
	else if(rel=="bottomright"){
		rx=BrowserWidth + document.body.scrollLeft;
		ry=BrowserHeight+ document.body.scrollTop;
		} 
	else if(rel=="center"){
		rx=BrowserWidth/2 + document.body.scrollLeft;
		ry=BrowserHeight/2 + document.body.scrollTop;
		}
	else if(rel=="mouse"){
		rx = MouseX + x;
		ry = MouseY + y;      
		}  
	the_object.style.left=rx+x;
	the_object.style.top=ry+y;
	if(the_object.style.visibility == 'hidden' || the_object.style.display == 'none' || the_object.style.display == 'hide') {
		the_object.style.visibility ='visible';
		the_object.style.display = 'block';
		}	
	else {
		the_object.style.visibility ='hidden';
		the_object.style.display = 'none';
		}
	}
//---------------------------
// Alphabetic check
function alphaCheck(theField){
	var val=theField.value;
	var chk = /[a-zA-Z_ ']+/;
	if (chk.test(val)){return 1;}
	else{
		alert('This field only accepts valid alphabetic characters (A-Za-z_ \').');
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Alphanumeric check script-
function alphanumericCheck (theField){
	var val=theField.value;
	var chk = /[0-9a-zA-Z_ ']+/;
	if (chk.test(val)){return 1;}
	else{
		alert("Please input a alphanumeric string. A-Za-z0-9_");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// currency check script-
function currencyCheck (theField){
	var val=theField.value;
	var anum=/(^\d+\.{0,1}\d{0,2}$)/;
	if (anum.test(val)){}
	else{
		alert("Please input a valid US currency amount.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Date Check
function dateCheck (theField){
	var val=theField.value;
	var formats = 'Accepted Formats MM-DD-YY, MM/DD/YY, MM-DD-YYYY, MM/DD/YYYY, YYYY-MM-DD';
	var pattern = /^(1[0-2]|0?[1-9])[\/\-](0?[1-9]|[12][0-9]|3[01])[\-\/]([0-9]{2}|[0-9]{4})$/;
	var pattern2 = /^([0-9]{4})[\-](1[0-2]|0?[1-9])[\-](0?[1-9]|[12][0-9]|3[01])$/;
	if (pattern.test(val) || pattern2.test(val)){}
	else{
		alert("Please input a valid date.\n" + formats);
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Get the number of day in a given month and year.
function getLastDay(y, m) {
	var d = 0;
	if(m == 2) {
		if((y % 4) != 0) {d = 28;} 
		else if((y % 400) == 0){d = 29;} 
		else if((y % 100) == 0) {d = 28;} 
		else {d = 29;}
		} 
	else if((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m ==10) || (m == 12)){d = 31;}
	else {d = 30;}
	return d;
	}
// Email Check
function emailCheck (theField){
	var val=theField.value;
	var filter=/^.+@.+\..{2,6}$/;
	if (filter.test(val)){}
	else{
		alert("Please input a valid email address.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Returns the Date - returns Wednesday January 31, 2001
function getDate (){
	var mydate=new Date();		
	var year=mydate.getYear();		
 	if (year<2000){year=1900+year;}		
	var day=mydate.getDay();		
	var month=mydate.getMonth();		
	var daym=mydate.getDate();		
	if (daym<10){daym="0"+daym}		
	var dayarray=new Array("Sun","Mon","Tues","Wed","Thur","Fri","Sat");		
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var result=""+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+"";
	return result;
	}
// Integer Check
function integerCheck (theField){
	var val=theField.value;
	var anum=/(^\d+$)/;
	if (anum.test(val) || $val==0){}
	else{
		alert("This field only accepts valid positive integers.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Real Number check script-
function realnumberCheck (theField){
	var val=theField.value;
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	if (anum.test(val)){}
	else{
		alert("Please input a valid real number.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
//Reformat
function reformat (s){
	var arg;
	var sPos = 0;
	var resultString = "";
	for (var i = 1; i < reformat.arguments.length; i++) {
		arg = reformat.arguments[i];
		if (i % 2 == 1){ resultString += arg;}
		else {
			resultString += s.substring(sPos, sPos + arg);
			sPos += arg;
			}
		}
	return resultString;
	}
// Removes all characters which appear in string bag from string s.
function stripChar(s){
	var i;
	var returnString = '';
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (digits.indexOf(c) > 0){returnString += c;}
		}
	return returnString;
	}
// Set focus on a field in a form
function setFocus(theField) {theField.focus();}
// Time Check
function timeCheck (theField){
	var val=theField.value;
	var anum=/(^\d{1,2}\:\d{2}$)/;
	if (anum.test(val)){}
	else{
		alert("Please input a valid time - military format. HH:MM");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Zip Code Check -
function zipCheck(val,theField){
	var sval=stripChar(val);
	if((sval.length == 9)||(sval.length == 5)){
		if(sval.length == 9){
			var x=reformat(sval,"",5,"-",4,"",0);
			theField.value=x;
			}
		}
	else{
		alert("Please input a valid US Zip Code!");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// SSN Check -
function ssnCheck(theField){
	var val=theField.value;
	var sval=stripChar(val);
	var x=reformat(sval,"",3,"-",2,"-",4);
	var anum=/\d\d\d\-\d\d\-\d\d\d\d/;
	if (anum.test(x)){theField.value=x;}
	else{
		alert("Please input a valid SSN number.\n\nFormat: 999-99-9999");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Format Phone - sets the value of the field to a phone number format.
function phoneCheck(theField){
	var val=theField.value;
	var sval=stripChar(val);
	if(sval.length == 10){
		var x=reformat(sval, "(", 3, ") ", 3, "-", 4);
		theField.value=x;
		}
	else{
		alert("Please input a valid 10 digit phone number.\n\nFormat: 999 999-9999");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Custom1 - custom1Check - Web Images
function custom1Check(theField){
	var val=theField.value;
	var anum=/\.(jpg|gif|png)$/;
	if (anum.test(val)){}
	else{
		alert("Please select a valid web image.\n\n Gif,Jpg, and Png files are accepted.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Custom2 - custom2Check
function custom2Check(theField){
	var val=theField.options[selectedIndex].value;
	if(val == 1 && document.addedit.scheduled_date.length == 0){
		alert('Schedule Date is required when Customer Status=Scheduled');
		document.addedit.scheduled_date.focus();
		return 0;
		}
	if(val == 2 && document.addedit.install_date.length == 0){
		alert('Install Date is required when Customer Status=Installed');
		document.addedit.install_date.focus();
		return 0;
		}
	if(val == 3 && document.addedit.bundled_date.length == 0){
		alert('Bundled Date is required when Customer Status=Bundled');
		document.addedit.bundled_date.focus();
		return 0;
		}
	return 1;
	}
// Custom3 - custom3Check
function custom3Check(theField){
	var date=theField.value;
	var alertmsg = 'The resubmit date cannot be earlier than today';
    window.status='';
    if( date == saved ){return true;}   // don't check if no change
    if( date.charAt(2) != '-' || date.charAt(5) != '-' || date.substring( 0, 2 ) < 1 || date.substring( 0, 2 ) > 12 || date.substring( 3, 5 ) < 1 || date.substring( 3, 5 ) > 31 ){
    	alert('Invalid date.');
      	return false;
    	}
    if( saved != '' ){alertmsg += '(unless you keep the old date of '+saved+').';}
    else{alertmsg += '.';}
    var now = new Date();
    if( date.substring( 6, 10 ) < now.getYear() ){
    	alert(alertmsg);
      	return false;
    	}
    else if( date.substring( 6, 10 ) == now.getYear() ){
      	if( date.substring( 0, 2 ) < now.getMonth()+1 ){
        	alert(alertmsg);
        	return false;
      		}
      	else if( date.substring( 0, 2 ) == now.getMonth()+1 ){
        	if( date.substring( 3, 5 ) < now.getDate() ){
          		alert(alertmsg);
          		return false;
        		}
      		}
    	}
    return true;
	}
// Custom4 - custom4Check
function custom4Check(theField){return 1;}
// Custom5 - custom5Check
function custom5Check(theField){return 1;}
// The following functions are used in the List Results call
function Check(){
   if (!document.listdata.checkall) {return;}
   var t = document.listdata.elements.length;
   var c = 0;
   if (document.listdata.checkall.checked) {c = 1;}
   for (var i = 0; i < t; i++) {
	if (document.listdata.elements[i].name == "i_selected") {
    	document.listdata.elements[i].checked = c;
		}
   	}
}
function buildmail(){
	var t = document.listdata.elements.length;
	var chek = Array(50);
	var x=0;
	for (var i = 0; i < t; i++) {
		if (document.listdata.elements[i].name == "i_selected" && document.listdata.elements[i].checked) {
			chek[x]=document.listdata.elements[i].value;
			x=x+1;
			}
		}
	var list = "";
	for (var i = 0; i < t; i++) {
		for (var y = 0; y<x; y++){
			var em = "i_email_"+chek[y];
			if (document.listdata.elements[i].name == em) {
				list = list + document.listdata.elements[i].value + ",";
				}
			}
		}
	parent.location.href='mailto:'+list;
	}
//SetNav
function Setnav(nav){
	document.i_menu.i_nav.value=nav;
	return 1;
	}
//Date Picker
 function DoCal(elTarget) {
	if (window.showModalDialog) {
    	var sRtn;
		//sRtn = tooltip('<iframe src="/infiles/calendar.htm" width=250 height=250></iframe>');
    	sRtn = showModalDialog("/infiles/calendar.htm","","Help=0;Center=1;Resizable=1;scroll=0;Status=0;dialogWidth=146pt;dialogHeight=165pt");
    	if (sRtn!=""){elTarget.value = sRtn;return true;}
		else {return false;}
		}
	else{alert("Your Browser does not support ModalDialogs. Enter a Date: MM/DD/YY.")
		return false;
		}
	}
//Color Picker
 function DoColor(elTarget) {
	if (window.showModalDialog) {
    	var sRtn;
    	sRtn = showModalDialog("/infiles/color.htm","","Help=0;Center=1;Resizable=1;scroll=0;Status=0;dialogWidth=145pt;dialogHeight=175pt;");
    	if (sRtn!=""){elTarget.value = sRtn;return true;}
		else {return false;}
		}
	else{alert("Your Browser does not support ModalDialogs. Enter a color in hex format.")}
	}
function Show_Hide(obj,cmd){
	//alert('obj '+obj+' cmd= '+cmd);
    var x = new getObj(obj);
	//alert('x='+x);
	if(cmd && cmd==1){
		x.style.visibility ='visible';
		x.style.display = 'block';
		}
	else if(cmd && cmd==2){
		x.style.visibility ='hidden';
		x.style.display = 'none';
		}
	else if(x.style.visibility == 'hidden' || x.style.display == 'none' || x.style.display == 'hide'){
		x.style.visibility ='visible';
		x.style.display = 'block';
		}
	else{
		x.style.visibility ='hidden';
		x.style.display = 'none';
		}
	return true;
	}
//verify
function verify(theField1,theField2,message){
	if(theField1.value != theField2.value){
		if(message){alert(message);}
		else{alert('Verify Field Mismatch. Please re-enter');}
		theField2.focus();
		return false;
		}
	return 0;
	}
//Reserved -  alerts if a reserved word is passed to it
function Reserved(theField){
	var w=theField.value;
	var rwords=new Array(
		'action','add','all','allfields','alter','and','as','asc','auto_increment','between','bigint','bit','binary','blob','both','by',
		'cascade','char','character','change','check','column','columns','create',
		'data','database','databases','date','datetime','day','day_hour','day_minute','day_second','dayofweek','dec','decimal','default','delete','desc','describe','distinct','double','drop','escaped','enclosed',
		'enum','explain','fields','float','float4','float8','foreign','from','for','full',
		'grant','group','having','hour','hour_minute','hour_second',
		'ignore','in','index','infile','insert','int','integer','interval','int1','int2','int3','int4','int8','into','is','inshift','in1',
		'join','key','keys','leading','left','like','lines','limit','lock','load','long','longblob','longtext',
		'match','mediumblob','mediumtext','mediumint','middleint','minute','minute_second','month',
		'natural','numeric','no','not','null','on','option','optionally','or','order','outer','outfile',
		'partial','precision','primary','procedure','privileges',
		'read','real','references','rename','regexp','repeat','replace','restrict','rlike',
		'select','set','show','smallint','sql_big_tables','sql_big_selects','sql_select_limit','sql_log_off','straight_join','starting',
		'table','tables','terminated','text','time','timestamp','tinyblob','tinytext','tinyint','trailing','to',
		'use','using','unique','unlock','unsigned','update','usage',
		'values','varchar','varying','varbinary','with','write','where',
		'year','year_month','zerofill');
		
	for (var i = 0; i < rwords.length; i++){
		if(w.toLowerCase()  == rwords[i]){
			alert('The word [ '+w+' ] is a reserved word in most databases.\n\nPlease try another word instead');
			theField.focus();
			theField.select();
			return false;
			}
		}
	return true;
	}
// End

	


