// global javascript functions

// browser compatibility check
if (!(document.getElementById || document.all || document.layers)) {
	// redirect to browser incompatibility page
}
var ie = (document.all) ? 1 : 0;
var ns4 = (document.layers) ? 1 : 0;
var ns6 = (document.getElementById) ? 1 : 0;

// function to start scripts onload
function pageInit(){
	loadImgs();
}

// function to reload NS4 on resize event
 if (ns4) {
    origWidth = innerWidth;
    origHeight = innerHeight;
 }
function reDo() {
   if (innerWidth != origWidth || innerHeight != origHeight) 
      location.reload();
}
if (ns4) onresize = reDo;

// function to load standard rollover images
var loaded = 0;
function loadImgs(color,level){
	// for top nav rollovers
	var nav1Imgs = new Array();
		nav1Imgs[0] = 'comp';
		nav1Imgs[1] = 'prod';
		nav1Imgs[2] = 'news';
		nav1Imgs[3] = 'cont';
		nav1Imgs[3] = 'gett';
			
	var loadedImgs = new Array();
	// declare nav images
	for (i=0; i < nav1Imgs.length; i++ ) {
	  	loadedImgs[i] = new Image();
	  	loadedImgs[i].src = '/images/n1_'+nav1Imgs[i]+'_on.gif';
	}
	loaded = 1;
}

// function to go to URL
function goUrl(url){
	location.href = url;
}

// function to do main rollovers
function swapImgs(img,state,theLayer){
	if (loaded > 0){
		if (!document.layers || !theLayer){
			document.images[img].src = state ? '/images/' + img + '_on.gif' : '/images/' + img + '_off.gif';
		} else {
			var theImg = eval('document.layers["'+theLayer+'"].document.images[img]');
			theImg.src = state ? '/images/' + img + '_on.gif' : '/images/' + img + '_off.gif';
		}
	}
}

// function to open popup windows
function openPop(type,id){
	var bWidth = ns4 ? 332 : 348;
	var params =  'height=520,width='+bWidth+',directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,toolbar=no,screenX=100,screenY=50,left=100,top=50';
	var url = '';
	var win = '';
	
	// for exec bios
	if (type == 'bio'){
		url = id;
		win = 'bioWindow';
	// for job descriptions
	} else if (type == 'job'){
		url = '/includes/comp_joblisting.jsp?position_id=' + id;
		win = 'jobWindow';
	// for directions
	} else if (type == 'contact'){
		url = '/includes/cont_directions.jsp?directions_id=' + id;
		win = 'contactWindow';
	// for customer testimonials
	} else if (type == 'test'){
		url = '/includes/comp_testimonials.jsp';
		win = 'testWindow';
	// for privacy
	} else if (type == 'privacy'){
		params =  'height=200,width='+bWidth+',directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,toolbar=no,screenX=100,screenY=50,left=100,top=50';
		url = '/privacy.jsp';
		win = 'privacyWindow';
	}
	
	// focus window
	var imgWin = window.open(url,win,params);
	if (imgWin){
		imgWin.focus();
	}
}

// function to validate required form fields
function checkForm(theForm){
	var errCount = 0;
	var errMsg = 'Please correct the following errors and then submit the form again.\n';
	
	// first name
	if (theForm.firstName.value.length < 1){
		errMsg += '\n- Please fill in a first name.';
		errCount++;
	}
	// last name
	if (theForm.lastName.value.length < 1){
		errMsg += '\n- Please fill in a last name.';
		errCount++;
	}
	// title
	if (theForm.title.value.length < 1){
		errMsg += '\n- Please fill in a title.';
		errCount++;
	}
	// company
	if (theForm.company.value.length < 1){
		errMsg += '\n- Please fill in a company name.';
		errCount++;
	}
	// phone
	if (theForm.phone.value.length < 1){
		errMsg += '\n- Please fill in a phone number.';
		errCount++;
	}
	// email
	if (theForm.email.value.length < 1){
		errMsg += '\n- Please fill in an email address.';
		errCount++;
	}
	// street address
	if (theForm.streetAddress.value.length < 1){
		errMsg += '\n- Please fill in a street address.';
		errCount++;
	}
	// city
	if (theForm.city.value.length < 1){
		errMsg += '\n- Please fill in a city.';
		errCount++;
	}
	// state
	if (theForm.state.value.length < 1){
		errMsg += '\n- Please fill in a state.';
		errCount++;
	}
	// country
	if (theForm.menuCountry.selectedIndex < 1){
		errMsg += '\n- Please select a country.';
		errCount++;
	}
	// contact method
	var cMethod = false;
	for (i=0; i< theForm.contactMethod.length; i++){
		if(theForm.contactMethod[i].checked == true){
			cMethod = true;
		}
	}
	if (cMethod == false){
		errMsg += '\n- Please select a contact method.';
		errCount++;
	}
	// more info topic
	var infoTopic = false;
	for (i=0; i< theForm.elements.length; i++){
		var fName = theForm.elements[i].name;
		var fType = theForm.elements[i].type;
		if(fName.indexOf('info') != -1 && fType.indexOf('checkbox') != -1){
			if (theForm.elements[i].checked == true){
				infoTopic = true;
				if (fName == 'infoOther' && theForm.infoOtherText.value.length < 1){
					errMsg += '\n- Please fill in another topic for more information.';
					errCount++;
				}
			}
		}
	}
	if (infoTopic == false){
		errMsg += '\n- Please select a topic for more information.';
		errCount++;
	}
	
	// alert if errors or submit if none
	if (errCount > 0){
		alert(errMsg);
		return false;
	} else {
		return true;
	}

}

// declare layer array
var menuLayer = new Array( );
	menuLayer[0] = "layerWWide";

// set initial layer states
var keep = -1;
var hideit = -1;

// function for displaying menu
function showMenu(theLayer) {
	keep = theLayer;
 	// hideall( );
  	showLayer(theLayer);
}

// function to show menu layers
function showLayer(theLayer) {
	if(ie){
    	eval(menuLayer[theLayer]+'.style.visibility="visible"');
	} else if (ns4){
		eval('document.'+menuLayer[theLayer]+'.visibility="show"');
	} else if (ns6){
		eval('document.getElementById(\"'+menuLayer[theLayer]+'\").style.visibility="visible"');
	}
}

// function to initiate menu hiding
// timeout prevents mouse flickering
function hideMenu() {
	keep = -1;
 	setTimeout('hideall( )',100);
}

// function to hide menu layers
function hideall( ) {
	for (i = 0; i < menuLayer.length; i++ ) {  
    hideit = 0;
    checkmousepos(i);
	    if (ie && keep != i) { 
			if (hideit) {
				eval(menuLayer[i]+'.style.visibility="hidden"');
			} 
	    } else if (ns4 && keep != i){ 
			if(hideit) {
				eval('document.'+menuLayer[i]+'.visibility="hide"');
			}
	    } else if (ns6 && keep != i){ 
			if(hideit) {
				eval('document.getElementById(\"'+menuLayer[i]+'\").style.visibility="hidden"');
			}
		} // end conditional
	} // end for loop
} // end function


// code to capture mouse events
document.onmousemove = getmousepos;
if (ns4) {
  	document.captureEvents(Event.MOUSEMOVE);
}

// function to find mouse position
function getmousepos(e) {
	if (window.Event) {
		mousex = e.pageX;
		mousey = e.pageY;
	} else {
		mousex = window.event.clientX;
		mousey = window.event.clientY;
	}
}

// function to compare mouse position with layer boundary
function checkmousepos(i) {
	if (ie) {     
		x_min = eval(menuLayer[i]+'.style.pixelLeft');
		x_max = x_min + eval(menuLayer[i]+'.scrollWidth');
		y_min = eval(menuLayer[i]+'.style.pixelTop');
		y_max = y_min + eval(menuLayer[i]+'.scrollHeight');
		      
	} else if(ns4) {     
		x_min = eval('document.'+menuLayer[i]+'.left');
		x_max = x_min + eval('document.'+menuLayer[i]+'.clip.width');
		y_min = eval('document.'+menuLayer[i]+'.top');
		y_max = y_min + eval('document.'+menuLayer[i]+'.clip.height');
	
	} else if (ns6) {
		x_min = parseInt(eval('document.getElementById(\"'+menuLayer[i]+'\").style.left'));
		x_width = eval('document.getElementById(\"'+menuLayer[i]+'\").style.width');
		x_max = x_min + parseInt(x_width);
		y_min = parseInt(eval('document.getElementById(\"'+menuLayer[i]+'\").style.top'));
		y_height = eval('document.getElementById(\"'+menuLayer[i]+'\").style.height');
		y_max = y_min + parseInt(y_height);
	}

	// subtract text rollover from layer height
	//y_min = y_min + 12;
	//alert('xMin='+x_min+', xMax='+x_max+', yMin='+y_min+', yMax='+y_max);
	
  	// if mouse is outside layer boundaries, hide layer
	if (mousex >= x_min && mousex <= x_max && mousey >= y_min && mousey <= y_max ) {
		hideit = 0;
    	setTimeout('hideall()',100);
  	} else {
    	hideit = 1;
	}
  	return hideit;
}

// utility function to set style properties
function setIdProperty(id, property, value ){
	if (document.all){
		document.all[id].style[property] = value;
   	} else if (document.layers){
        document[id][property] = value;
	} else if (document.getElementById){
        var styleObject = document.getElementById(id);
        if (styleObject != null){
            styleObject = styleObject.style;
            styleObject[property] = value;
		}
	}
}
