
//************************************************************************
//
//  JavaScript Common Functions
//
//************************************************************************

//************************************************************************
// GLOBAL VARIABLES
//************************************************************************
var gsErrorMsg = '';
var gsCR = '';
var gsFormCode = '';
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = y2k(today.getYear());
var whichOne;
var myWindow;
//var valid='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_,.- ';
var gbDirty = 0;
//************************************************************************

//************************************************************************
// FUNCTIONS LIST
//************************************************************************
// longDate()
//************************************************************************

function longDate() {
	var d, s, dayName, monthName;
	dayName = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	monthName = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

	d = new Date();
	s = dayName[d.getDay()] + ', ';
	s += monthName[d.getMonth()] + ' ';
	s += d.getDate() + ', ';
	s += d.getFullYear();

	return(s);
}
//************************************************************************
// end function: longDate
//************************************************************************

function preload(imgObj,imgSrc) {
	if (document.images) {
		eval(imgObj + "= new Image()");
		eval(imgObj + '.src = "'+imgSrc+'"');
	}
}

function loadEm() {
	preload('contact_light','../images/contact_light.jpg');
	preload('contact_dark','../images/contact_dark.jpg');
	preload('home_light','../images/home_light.jpg');
	preload('home_dark','../images/home_dark.jpg');
	preload('jobs_light','../images/jobs_light.jpg');
	preload('jobs_dark','../images/jobs_dark.jpg');
	preload('new_light','../images/new_light.jpg');
	preload('new_dark','../images/new_dark.jpg');
	preload('services_light','../images/services_light.jpg');
	preload('services_dark','../images/services_dark.jpg');
}

function swapImgRestore() { //v3.0
	var i, x, a = document.sr;
	for(i=0; a&&i<a.length&&(x=a[i])&&x.oSrc; i++) x.src=x.oSrc;
}

function preloadImages() { //v3.0
	var d=document; 
	if(d.images) {
		if(!d.p) d.p=new Array();
		var i,j=d.p.length,a=preloadImages.arguments;
		for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){
			d.p[j]=new Image; d.p[j++].src=a[i];
		}
	}
}

function findObj(n, d) { //v3.0
	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);
	return x;
}

function swapImage() { //v3.0
	var i,j=0,x,a=swapImage.arguments;
	document.sr=new Array;
	for(i=0; i<(a.length-2); i+=3)
	if ((x=findObj(a[i]))!=null) {
		document.sr[j++]=x;
		if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
	}
}

function replaceString(fullS, oldS, newS) {
	// Replaces oldS with newS in the string fullS
	for (var i=0; i<fullS.length; i++) { 
		if (fullS.substring(i,i+oldS.length) == oldS) {
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
		}
	}
	return fullS;
}

//************************************************************************
//	Function:		validateField
//	Purpose:		Checks string for invalid characters.
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:		09/14/2000
//	Input:			string, additional valid characters
//	Output:			true if only valid characters, false if any not valid
//	Notes:			
//************************************************************************
function validateField(string, addValid) {
	var valid = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
	valid += addValid;

	for (var i = 0; i < string.length; i++) {
		if (valid.indexOf(string.charAt(i)) < 0) {
			//allow line feed/carriage return
			if (string.charCodeAt(i) != 10 && string.charCodeAt(i) != 13) {
				return false;
			}
		}
	}
	return true;
} 
//************************************************************************
//	end function validateField(string, addvalid)
//************************************************************************

//************************************************************************
//	Function:		isReserveChar
//	Purpose:		Checks string for reserved character.
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:		09/25/2004
//	Input:			string, reserved character
//	Output:			true if reserved character used, false if not
//	Notes:			
//************************************************************************
function isReserveChar(string, reserveChar) {
	for (var i = 0; i < string.length; i++) {
		if (reserveChar.indexOf(string.charAt(i)) == 0) {
			return true;
		}
	}
	return false;
} 
//************************************************************************
//	end function isReserveChar(string, reserveChar)
//************************************************************************

//************************************************************************
//	Function:	javatrim
//	Purpose:	Removes leading/trailing spaces from a string.
//	Developer:	?
//	Updated:	?
//	Input:		value
//	Output:		value without leading or trailing spaces
//	Notes:		
//************************************************************************
function javatrim(value) {
	
	/* Strip leading spaces from input */
	
	var startposn=0;
	while((value.charAt(startposn)==' ')&&(startposn<value.length)) {
		startposn++;
	}
	
	if(startposn==value.length) {
		value='';
	}
	else {

		/* If anything left of string after stripping leading spaces strip trailing */
		/* spaces as well */
		value=value.substring(startposn,value.length);
		endposn=(value.length)-1;
		while(value.charAt(endposn)==' ') {
			endposn--;
		}
		value=value.substring(0,endposn+1);
	}
	return(value);
}
//************************************************************************
//	End Function:	javatrim
//************************************************************************

//************************************************************************
//	Function:	newWindow
//	Purpose:	Creates a new browser window
//	Developer:	Matt Kaiser
//	Updated:	07/07/2000
//	Input:		target (page), options (browser window parameters)
//	Output:		none
//	Notes:		
//************************************************************************
function newWindow(target, options) {
	var pointer;
	if (options == null) {
		//defaults: 'channelmode=no,directories=yes,fullscreen=no,height=100,left=0,location=yes,menubar=yes,resizable=yes,scrollbars=no,status=yes,titlebar=yes,toolbar=yes,top=0,width=100';
		options = 'channelmode=no,directories=no,fullscreen=no,height=400,left=100,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=yes,toolbar=no,top=100,width=400';
	}
	pointer = self.open(target, 'newWindow', options);
}
//************************************************************************
//	End Function:	newWindow
//************************************************************************

//************************************************************************
//	Function:	formatCurrency
//	Purpose:	Validates a numeric field for currency format
//	Developer:	Matt Kaiser
//	Updated:	09/17/2001
//	Input:		field name
//	Output:		true or false
//	Notes:		
//************************************************************************
function formatCurrency(fieldname) {

//find character at position 3rd from right
// that character should be "."
// if not, ask to format as 9~999.00
//  if yes, format and return true
//  if no, return false

	var field = eval('document.thisForm.' + fieldname);
	if (javatrim(field.value) != '') {
		if (isNum(field.value)) {
			if (field.value.charAt(field.value.length - 3) != '.') {
				var whatToDo = window.confirm('This field is currency. Do you wish to enter ' + field.value + '.00?');
				if (whatToDo) {
					field.value = field.value + '.00';
					return true;
				}
				else {
					return false;
				}
			}
			else {
				return true;
			}
		}
		else {
			alert('This field must be numeric \(currency\)!');
			return false;
		}
	}
	else {
		return true;
	}
}
//************************************************************************
//	End Function:	formatCurrency
//************************************************************************

//************************************************************************
//	Function:		checkEmail
//	Purpose:		Checks email address for standard format.
//	Developer:		Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:		01/02/2001
//	Input:			email address string
//	Output:			true or false
//	Notes:			
//************************************************************************
function checkEmail(emailAddress) {
	var i = 1;
	var sLength = emailAddress.length;

	//look for @ after the first character
	while (( i < sLength) && (emailAddress.charAt(i) != '@')) {
		i++;
	}
	if ((i >= sLength) || emailAddress.charAt(i) != '@') {
		return false;
	}
	else {
		i += 2;
	}
	//look for . after @
	while ((i < sLength) && (emailAddress.charAt(i) != '.')) {
		i++;
	}
	//there must be at lease one character after the .
	if ((i >= sLength - 1) || (emailAddress.charAt(i) != '.')) {
		return false;
	}
	else {
		return true;
	}
}
//************************************************************************
//	End Function:	checkEmail
//************************************************************************

//************************************************************************
//	Function:	y2k, padout, calrestart, calWindow
//	Purpose:	popup calendar functions
//	Developer:	Matt Kaiser
//	Updated:	10/31/2000
//	Input:		n/a
//	Output:		date
//	Notes:		
//************************************************************************
function y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

function padout(number) {
	return (number < 10) ? '0' + number : number;
}

function calrestart() {
	var field = eval('document.thisForm.' + whichOne);
	field.value = '' + padout(month - 0 + 1) + '/' + padout(day) + '/' + year;
	myWindow.close();
	refreshMe();
}

function calWindow(field) {
    whichOne = field;
    myWindow = open('_ScriptLibrary/cal.htm', 'myname', 'resizable=no, width=350, height=270');
}
//************************************************************************
//	end function y2k, padout, calrestart, calWindow
//************************************************************************

//************************************************************************
//	Function:	isNum
//	Purpose:	Validates a numeric field
//	Developer:	?
//	Updated:	?
//	Input:		value
//	Output:		boolean
//	Notes:		
//************************************************************************
function isNum(value) {
	if (javatrim(value) == '') {
		return false;
	}
	for (i=0; i<value.length; i++) {
		if (value.charAt(i) < '0') {
			if (value.charAt(i) != '.')
			    return false;
		}
		if (value.charAt(i) > '9') {
			if (value.charAt(i) != '.')
				return false;
		}
	}
	return true;
}
//************************************************************************
//	End Function:	isNum
//************************************************************************

function proof()
{
alert('Proof I was here!');
}

//************************************************************************
//	Function:	goDirty
//	Purpose:	Marks page as changed.
//	Developer:	Matthew Kaiser, Excave Systems Consulting Inc.; www.excave.com
//	Updated:	01/26/2006
//	Input:		n/a
//	Output:		sets gbDirty
//	Notes:		
//************************************************************************
function goDirty() {
	gbDirty = 1;
} 
function checkDirty() {
	if (gbDirty) event.returnValue = "Changes you made to this event plan will be lost if you select OK.\n\nClick Cancel to continue editing the event plan and to save changes. "
}
//************************************************************************
//	end function goDirty()
//************************************************************************

