// Define global Variables
newWindow = '';
popupCalendar = '';

var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());
var monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var dow = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
useMonth = month;
useYear = year;
var serverParts = location.href.split('/');
var thisServer = serverParts[0] + '\/\/' + serverParts[2] + '/';

// Check for DHTML support
if (document.all || document.layers || document.getElementById)
        document.dhtml = true;
		
// Check for popup window
if (document.dhtml) {
        if (document.layers) document.captureEvents(Event.MOUSEUP)
        document.onmouseup = checkPopup;
} else {
        event = null;
}

// Set DOM variables
if (document.layers)
        doc = 'document.layers[', vis = '].visibility', top = '].top', left = '].left', height = '].document.height', trueTop = '].pageY';
if (document.all)
        var doc = 'document.all[', vis = '].style.visibility', top = '].style.posTop', left = '].style.posLeft', height = '].offsetHeight', trueTop = '].offsetTop';
if (document.getElementById)
		var doc = 'document.getElementById(', vis = ').style.visibility', top = ').style.top', left = ').style.left', height = ').offsetHeight';

	
// FUNCTIONS //

// Common graphic mouse-over routines
function imageOver(imageItem,dir,fileformat) {
		document[imageItem].src = dir + imageItem + '2' + fileformat;
		document.swapImage = imageItem;
		document.dirImage = dir;
		document.fileformatImage = fileformat;
        }
		
function imageOut() {
        if(document.swapImage) {
			document[document.swapImage].src = document.dirImage + document.swapImage + '1' + document.fileformatImage;
			}
        }

function imageDisplay(imageItem,dir,fileformat) {
		document[imageItem].src = dir + imageItem + fileformat;
	}	
		
// Open Popup Window, where URL is file to open, x is width and y is height
function openWindow(url,x,y) {
		url = thisServer + url;
		newWindow=open(url,'newWindow','menubar=no,scrollbars=yes,status=yes,resizable=yes,location=no,width=' + x + ',height=' + y);
		if (newWindow.opener == null) newWindow.opener = self;
		newWindow.focus();
        }

// Open Popup Window, special Funding Source listing, where URL is file to open, x is width and y is height, field is field to fill in, fieldnum is number in list
function selectWindow(url,x,y,field,fieldnum,formname) {
		url = thisServer + url + '?' + field + '&' + fieldnum + '&' + formname;
		newWindow=open(url,'newWindow','menubar=no,scrollbars=yes,status=yes,resizable=no,location=no,width=' + x + ',height=' + y);
		newWindow.focus();
        }
		
// Open Information Popup Window
function openInfoWindow(infoNum) {
		url = thisServer + 'popups/popup_info.html?' + infoNum;
		newWindow=open(url,'newWindow','menubar=no,scrollbars=yes,status=yes,resizable=no,location=no,width=450,height=250');
		if (newWindow.opener == null) newWindow.opener = self;
		newWindow.focus();
        }
        
// Checks to see if a popup window is open, forces focus on popup
function checkPopup() {
if (newWindow && !newWindow.closed) {
                alert('Please close popup window before continuing');
                newWindow.focus();
                }
if (popupCalendar && !popupCalendar.closed) {
                alert('Please close calendar window before continuing');
                popupCalendar.focus();
                }
        }

// Returns current date
function printDate() {

        var time = new Date();
        timezoneoffset = time.getTimezoneOffset();
        if ((navigator.appVersion.indexOf('MSIE 3') != -1)) timezoneoffset = timezoneoffset * (-1);
        time.setTime(time.getTime() + timezoneoffset*60*1000);
        time.setTime(time.getTime() + -8*60*60*1000);
        
        time.setHours(time.getHours() + 1);

        var monthNum = time.getMonth();
        var hours = time.getHours(), minutes = padout(time.getMinutes());
        var year = time.getYear(), month = monthNames[monthNum], day = time.getDate(), dowName = dow[time.getDay()];
        // Long Date
        //document.write(dowName + ', ' + month + ' ' + day + ', ' + year + ', ' + hours + ':' + minutes);
		formattedDate = padout(monthNum+1) + '/' + padout(day) + '/' + year;
		return formattedDate;
        }

// Redirects to URL 
function changeURL(thisURL) {
		confirmation = confirm('The page you are about to go to is not part of our site,\nand we are not responsible for its contents.\n\nContinue?');
        if (confirmation)
			location.href = thisURL;
        }
		

function padout(number) { return (number < 10) ? '0' + number : number; }
function padout100(number) { return (number < 10) ? '00' + number : (number < 100) ? '0' + number : number; }

// Pop-up Calendar Functions
function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

function newCalendar(thisField,thisForm) {
        useThisField = thisField;
        useThisForm = thisForm;
        calendarURL = thisServer + '/common/lib/calendar.php';
        popupCalendar=open(calendarURL,'popupCalendar','status=no,resizable=no,width=235,height=250');
        popupCalendar.focus();
        popupCalendar.location.href = calendarURL;
        if (popupCalendar.opener == null) popupCalendar.opener = self;
        }
		
// Sets price strings to .00 format
function setDecimals(cost) {
return (cost == Math.floor(cost)) ? cost + '.00' : (  (cost*10 == Math.floor(cost*10)) ? cost + '0' : cost);
}

// Check valide field formats. Works with FormChek.js
function checkField(thisValue,formName) {
valueGood = 1;
fieldObject = eval('document.' + formName + '[thisValue]');

if (thisValue.indexOf('phone') > -1 || thisValue.indexOf('fax') > -1) {
	digits = eval('document.' + formName + '["' + thisValue + '"].value');
	valueGood = checkUSPhone(fieldObject);
	}
	
if (thisValue.indexOf('PHONE') > -1 || thisValue.indexOf('FAX') > -1) {
	digits = eval('document.' + formName + '["' + thisValue + '"].value');
	valueGood = checkUSPhone(fieldObject);
	}

if ((thisValue.indexOf('email') > -1) || (thisValue.indexOf('EMAIL') > -1)) {
	valueGood = checkEmail(fieldObject);
	}
	
if (thisValue.indexOf('ssn') > -1) {
	valueGood = checkSSN(fieldObject);
	}
	
if ((thisValue.indexOf('zip') > -1) || (thisValue.indexOf('ZIP') > -1)) {
	valueGood = checkZIPCode(fieldObject);
	}
	
if (thisValue.indexOf('cc_number') > -1) {
	cardType = eval('document.' + formName + '.order_cc_type.value');
	valueGood = checkCreditCard(cardType, fieldObject);
	}
	
// checks for valid date and format mm/dd/yyyy
if (thisValue.indexOf('DATE') > -1) {
	date = eval('document.' + formName + '.' + thisValue + '.value');
	if (date.length == 10) {
		year = date.substring(6,10);
		month = date.substring(0,2);
		day = date.substring(3,5);
		valueGood = checkDate(year,month,day,fieldObject);
		}
	else {
		alert('Date must be in dd/mm/yyyy format. Please re-enter or use calendar pop-up');
		return false;
		}
	}

return valueGood;
}

function copyData(formname,from,to) {
	copy_fields = new Array("first_name","last_name","address_1","address_2",
							"city","zip","phone");

	for (i=0;i<copy_fields.length;i++) {
		eval('document.' + formname + '.' + to + '_' + copy_fields[i] + '.value = document.' + formname + '.' + from + '_' + copy_fields[i] + '.value');
		}
	stateField = eval('document.' + formname + '.' + from + '_state');
	fromStateField = eval('document.' + formname + '.' + from + '_state.value');
	for (i=0;i<stateField.length;i++) {
		toStateField = eval('document.' + formname + '.' + to + '_state[' + i + '].value');
		if (toStateField == fromStateField) {
			eval('document.' + formname + '.' + to + '_state.selectedIndex = i');
		}
	}
}

//Shows difference of days between date1 and date2
function compareDays(date1,date2) {
	year1 = date1.substring(6,10);
	month1 = date1.substring(0,2);
	day1 = date1.substring(3,5);
	
	year2 = date2.substring(6,10);
	month2 = date2.substring(0,2);
	day2 = date2.substring(3,5);

	date1 = new Date(year1,(month1-1),day1);
	date2 = new Date(year2,(month2-1),day2);
    var difference =
        Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
      - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}

function moveOption(form,from,to) {
	fromOptions = eval("document." + form + "." + from);
	toOptions = eval("document." + form + "." + to);
		
	// Move Selected over
	for (var i=0; i<fromOptions.length; i++) {
		var o = fromOptions.options[i];
		if (o.selected) {
			toOptions.options[toOptions.options.length] = new Option(o.text, o.value, false, false);
			}
		}
	// Delete Selected from original
	for (var i=0; i<fromOptions.length; i++) {
		var o = fromOptions.options[i];
		if (o.selected) {
			fromOptions.options[i] = null;
			}
		}
	sortSelect(toOptions);
	fromOptions.selectedIndex = -1;
	toOptions.selectedIndex = -1;
}

function sortSelect(obj) {
	var o = new Array();
	if (obj.options==null) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}




