
//Page initialzation
function initPage() {

	//load sightmax (live chat)
	liveChatShow('sightmax');
	getDate();

}

//Fills in the liveChat script
function liveChatShow(theObject) {
	var object = document.getElementById(theObject); 
	object.innerHTML += "<a href=\"javascript: var e = window.open('http://e-chat.Optrics.com/SightMaxAgentInterface/PreChatSurvey.aspx?accountID=1&siteID=31&queueID=62','chatWindow','width=490,height=404,resizable=0,scrollbars=no,menubar=no,status=no');\" onmouseout=\"top.status=''; return true;\" onmousedown=\"top.status='Chat with an Optrics Customer Service Representative';return true;\" onmouseover=\"top.status='Chat with an Optrics Customer Service Representative';return true;\"><img border=0 src=\"http://e-chat.Optrics.com/SightMaxAgentInterface/chat.smgif?accountID=1&siteID=31&queueID=62\" alt=\"Chat with an Optrics Customer Service Representative\"></a>";
}


//Setup the banner image mouseover in the mainpage header
function loadBanner() {
	//preload images
	if (document.images) {
		img01 = new Image();
		img01.src = "http://www.optrics.com/images/banner-left-img01.jpg";
		img02 = new Image();
		img02.src = "http://www.optrics.com/images/banner-left-img02.jpg";
		img03 = new Image();
		img03.src = "http://www.optrics.com/images/banner-left-img03.jpg";		
	}
	document.getElementById('bannerLeftContent').style.background = "url('http://www.optrics.com/images/banner-left-img01.jpg') no-repeat 0 0";
	document.getElementById('btn01').style.backgroundPosition = "0 -90px";
}

function swapImage(theButton) {
	
	var buttonObject = document.getElementById(theButton);
	var bgObject = document.getElementById('bannerLeftContent');
	
	buttonObject.style.backgroundPosition = "0 -90px";
	
	if (theButton == "btn01") {
		document.getElementById('btn02').style.backgroundPosition = "0 0";
		document.getElementById('btn03').style.backgroundPosition = "0 0";
		bgObject.style.backgroundImage = "url('http://www.optrics.com/images/banner-left-img01.jpg')";
	} else if (theButton == "btn02") {
		document.getElementById('btn01').style.backgroundPosition = "0 0";
		document.getElementById('btn03').style.backgroundPosition = "0 0";
		bgObject.style.backgroundImage = "url('http://www.optrics.com/images/banner-left-img02.jpg')";
	} else if (theButton == "btn03") {
		document.getElementById('btn02').style.backgroundPosition = "0 0";
		document.getElementById('btn01').style.backgroundPosition = "0 0";
		bgObject.style.backgroundImage = "url('http://www.optrics.com/images/banner-left-img03.jpg')";
	}
	
}

function resetImage() {

	loadBanner();
	//document.getElementById('btn01').style.backgroundPosition = "0 0";
	document.getElementById('btn02').style.backgroundPosition = "0 0";
	document.getElementById('btn03').style.backgroundPosition = "0 0";
	
}

function getDate() {
	var now = new Date();
	var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
	function fourdigits(number)	{
	return (number < 1000) ? number + 1900 : number;}
	//today =  days[now.getDay()] + ", " +
	today =  months[now.getMonth()] + " " +
	date + ", " +
	(fourdigits(now.getYear()));
	
	document.getElementById('theDate').innerHTML=today;
}


//------For Mid Drop Down Box Selection-------//

function selectProdCat(x) {
	window.location.href = x.options[x.selectedIndex].value;
}

function selectProdCatGo(x) {
	window.location.href = x;
}


//------For Download Forms---------//

function dlInit() {
	document.getElementById('optionalFields').style.display = 'none';
	document.getElementById('requiredFields').style.display = 'block';
}

function hide(theId) {
	document.getElementById(theId).style.display = 'none';
}

function show(theId) {
	document.getElementById(theId).style.display = 'block';
}

function showInline(theId) {
	document.getElementById(theId).style.display = 'inline';
}


//Validation of download forms
function validateDownload(optFields, reqFields) {
	var fName = document.getElementById("FirstName");
	var eAddy = document.getElementById("Email");
	
	//trim email whitespace if necessary
	eAddy.value = trim(eAddy.value);
	
	if (eAddy.value == "") {
		alert("Please enter a valid Email address");
		eAddy.focus();
	} else if (!checkemail(eAddy.value)) {
		alert("Please enter a properly formatted Email address");
		eAddy.focus();
		eAddy.select();
	} else if (fName.value == "") {
		alert("First Name is required.");
		fName.focus();
	}
	else {
		show(optFields);
		hide(reqFields);
	}
}

//checks for valid looking email addy
function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/;	
	return (filter.test(str));
}


//trim whitespace for validation
function trim(s){
	if((s==null)||(typeof(s)!='string')||!s.length)return'';return s.replace(/^\s+/,'').replace(/\s+$/,'')
}

//Checks if "enter" key was pressed
function checkEnter(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable
	
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	} else {
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		validateDownload('optionalFields','requiredFields'); //submit the form
		return false;
	} else {
		return true;
	}

}