window.onbeforeunload = confirmLeaving;
var mayLeave = true;
var leaveMessage;

$(document).ready(function(){
	// replace contact info image with contact info text
	$('#contact').html("  <p>&#0115;&#0097;&#0108;&#0101;&#0115;&#0064;jar&#0112;equipment&#0046;&#0099;&#0111;&#0109;</p>\n  <p>806-773-0126</p>\n  <p>432-897-0005</p>\n");
	
	//used for searchbox
	$('#searchbox').bind('focus', searchboxActive);
	$('#searchbox').bind('blur', searchboxInactive);
	
	// Current server time
	serverdate=new Date( $('#realtime').html() );
	setInterval("displaytime()", 1000);
	
	// make table rows links
	trclass = new Array();
	$('tr').each(function(i){
		var id = this.className.split(' ');
		if (id[0] == parseInt(id[0]))
		{
			trclass[i] = this.className;
			$(this).bind('mouseover', itemrowHover);
			$(this).bind('mouseout', itemrowUnhover);
			$(this).bind('click', itemrowClick);
			this.number = i;
			id = '';
		}
	});
	
	// detect if pointer over a link
	$('a').each(function(i){
		$(this).bind('mouseover', linkHover);
		$(this).bind('mouseout', linkUnhover);
	});
	linkHovered = false;
	
	// triggers checkContactForm on submit
	if ( $('#contactform').length )
	{
		leaveMessage = "If you leave this page, the information you entered will be lost.";
		$('#contactform').bind('change', preventLeaving);
		$('#contactform').bind('submit', checkContactForm);
	}
	
	// clear "remember my info" cookies on uncheck
	$('#remember_my_info').bind('change', forgetMe);
	
	//sends list_amount_form on change
	$('#list_amount').bind('change', listAmountSubmit);
	$('#list_amount2').bind('change', listAmountSubmit2);
	
});

//FORM EDITED WARNINGS
// warn on leaving the page (if form edited)
function confirmLeaving()
{
	if (!mayLeave) {
		return leaveMessage;
	}
}

//prevent leaving the page
function preventLeaving()
{
	mayLeave = false;
}
//allow leaving the page
function allowLeaving()
{
	mayLeave = true;
}

//forget a person upon unchecking "Remember Me" box on contact form
function forgetMe()
{
	if (this.checked == false) {
		eraseCookie('name');
		eraseCookie('company');
		eraseCookie('location');
		eraseCookie('email');
		eraseCookie('phone');
		eraseCookie('remember_form');
	}
}

/////////////////////
// COOKIE SCRIPTS //
/////////////////////
// from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// makes table rows links
function itemrowHover()
{
	this.className = trclass[this.number] + ' activerow';
}

function itemrowUnhover()
{
	this.className = trclass[this.number];
}

function itemrowClick()
{
	if (linkHovered == false)
	{
		id = trclass[this.number].split(' ');
		if ( $('#admin').length )
		{
			location.href = '/admin/id/' + id[0];
		}
		else
		{
			location.href = '/id/' + id[0];
		}
	}
}

// detects if pointer is over a link
function linkHover()
{
	linkHovered = true;
}
function linkUnhover()
{
	linkHovered = false;
}

// used for searchbox
function searchboxActive()
{
	if(this.value=='Item #') {this.value='';};
}
function searchboxInactive()
{
	if(this.value=='') {this.value='Item #';};
}

// used for time
function padlength(what)
{
	var output=(what.toString().length==1)? "0"+what : what;
	return output;
}

function displaytime()
{
	serverdate.setSeconds(serverdate.getSeconds()+1);
	var dn="PM";
	var hours=serverdate.getHours();
	if (serverdate.getHours()<12) {dn="AM";}
	if (serverdate.getHours()>12) {hours=serverdate.getHours()-12;}
	if (serverdate.getHours()==0) {hours=12;}
	var timestring=hours+":"+padlength(serverdate.getMinutes())+" "+dn;
	$('#time').html(timestring);
}

// checks if contact form is valid
function checkContactForm()
{
	var errors = '';
	
	// clear form errors
	$('input, textarea').each(function(i){
		if ( $(this).hasClass('formerror') )
		{
			$(this).removeClass('formerror');
		}
	});
	
	if ( !$('#name').val() )
	{
		errors += '\n\267 Your name is required.';
		$('#name').addClass('formerror');
	}
	if ( !$('#location').val() )
	{
		errors += '\n\267 Your location is required.';
		$('#location').addClass('formerror');
	}
	
	if ( $('#email').val().match(/^([a-zA-Z0-9_\.+-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,6})$/) )
	{
		
	}
	else if ( $('#email').val() )
	{
		errors += '\n\267 The email address is invalid.';
		$('#email').addClass('formerror');
	}
	else
	{
		errors += '\n\267 The email address is required.';
		$('#email').addClass('formerror');
	}
	
	if ( !$('#message').val() )
	{
		errors += '\n\267 The message is required.';
		$('#message').addClass('formerror');
	}
	
	if (errors != '') {
		// lists errors
		alert('The form could not be sent because:'+errors);
		return false;
	}
	
	allowLeaving();
}

//submits listAmount on change
function listAmountSubmit()
{
	$('#list_amount_form').submit();
}
function listAmountSubmit2()
{
	$('#list_amount_form2').submit();
}