// JavaScript Document
var a = null;
var b = null;
var height = null;

function loginToggle()

{
	  //Get a reference to the link on the page
	  // with an id of "mylink"
	  var a = document.getElementById("login");

	  //Set code to run when the link is clicked
	  // by assigning a function to "onclick"
		// Your code here...
		if(a.style.display == 'inline')

		{
			document.getElementById('loginInputEmail').value = '';
			document.getElementById('loginInputPassword').value = '';
			a.style.display = 'none';
		}
		else
		{
			a.style.display = 'inline';
		}
}

function animate()
{
	height = parseInt(b.style.height);
	b.style.height = parseInt(b.style.height)+10+'px';
	if(height < 150)
	{
		setTimeout(animate, 20);
	}
}

function init()
{
	b = document.getElementById("loginForm");
	b.style.height = '0px';
	animate();
}

function loginClear()
{
	document.getElementById('invalidEmail').style.display = 'none';
	document.getElementById('invalidPassword').style.display = 'none';
}

function showLogin()
{
	loginToggle();
	init();
	loginClear();
}

function isValidEmail(sEmail)
{
	var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
	var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
	var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
	var sQuotedPair = '\\x5c[\\x00-\\x7f]';
	var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
	var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
	var sDomain_ref = sAtom;
	var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
	var sWord = '(' + sAtom + '|' + sQuotedString + ')';
	var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
	var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
	var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
	var sValidEmail = '^' + sAddrSpec + '$'; // as whole string
	
	var reValidEmail = new RegExp(sValidEmail);
	
	if (reValidEmail.test(sEmail))
	{
	return true;
	}
	return false;
}

function checkLoginFields(emailField, passwordField)
{
	var loginVal = true;
	var email = emailField.value;
	var password = passwordField.value;
	if (!isValidEmail(email))
	{
		document.getElementById('invalidEmail').style.display = 'inline';
		document.getElementById('loginInputEmail').value = '';
		loginVal = false;
	}
	else
	{
		document.getElementById('invalidEmail').style.display = 'none';
	}
	if ((password.length < 6)||(password.length > 20))
	{
		document.getElementById('invalidPassword').style.display = 'inline';
		document.getElementById('loginInputPassword').value = '';
		loginVal = false;
	}
	else if ((password.length >= 6)&&(password.length <= 20))
	{
		document.getElementById('invalidPassword').style.display = 'none';
	}
	return loginVal;
}

var whichDiv = null;
var divHeight = null;
var theChildren = [];
var divTotal = null;
var theParent = null;
var lastSelected = null;
var signsDiv = null;
var hardwareDiv = null;
var servicesDiv = null;

function animateDiv()
{
	divHeight = parseInt(whichDiv.style.height);
	whichDiv.style.height = parseInt(whichDiv.style.height)+10+'px';
	if(divHeight < (divTotal-10))
	{
		setTimeout(animateDiv, 20);
	}
}

function animateInit()
{
	theChildren = whichDiv.getElementsByTagName("div");
	theChildren = ((theChildren.length/2));
	divTotal = ((theChildren*40));
	whichDiv.style.height = '0px';
	animateDiv();
}

function showSidebar(element)
{
	signsDiv = document.getElementById('signs');
	hardwareDiv = document.getElementById('hardware');
	servicesDiv = document.getElementById('services');
	theParent = element.parentNode;
	if(lastSelected != element)
	{
		lastSelected = element;
		
		switch(theParent.id)
		{
			case 'navSigns':
				whichDiv = document.getElementById('signs');
				whichDiv.style.height = '0px';
				animateInit();
				break;
			case 'navHardware':
				whichDiv = document.getElementById('hardware');
				whichDiv.style.height = '0px';
				animateInit();
				break;
			case 'navServices':
				whichDiv = document.getElementById('services');
				whichDiv.style.height = '0px';
				animateInit();
				break;
		}
	}
}

function searchValue()
{
	var q = document.getElementById('q');
	
	if(q.value == 'Search')
	{
		q.value = '';
		q.style.color = '#323534';
	}
	else if(q.value.replace(/^\s+|\s+$/g,"") == '')
	{
		q.value = 'Search';
		q.style.color = '#323534';
	}
}

function phoneInput(input, next)
{
	var inputVal = input.value;
	
	if(inputVal.length == 3)
	{
		document.getElementById(next).focus();
	}
}

function searchSubmit()
{	
	var searchReturn = true;
	var searchInput = document.getElementById('q');
	
	if((searchInput.value == '')||(searchInput.value.replace(/^\s+|\s+$/g,"") == '')||(searchInput.value == 'Search'))
	{
		searchReturn = false;
		searchInput.value = 'Search';
	}
	
	return searchReturn;
}

function whatsThis(object)
{
	document.getElementById('login').style.display = 'inline';
	document.getElementById('loginForm').style.display = 'none';
	alert(object.value);
}

function homeBoxes(box)
{
	var title = document.getElementById(box.id + 'Title');
	var boxLink = document.getElementById(box.id + 'Link');
	var boxDescription = document.getElementById(box.id + 'Description');
	var welcome = document.getElementById('welcome');
	
	//check if title is showing
	if(title.style.display == 'none')
	{
		title.style.display = 'inline';
		boxLink.style.display = 'none';
		boxDescription.style.display = 'none';
		welcome.style.display = 'inline-block';
	}
	else
	{
		title.style.display = 'none';
		boxLink.style.display = 'inline';
		boxDescription.style.display = 'inline-block';
		welcome.style.display = 'none';
	}
}

function navSwap(tab)
{	
	var navImage = document.getElementById(tab.id + 'Image');
	var navHover = document.getElementById(tab.id + 'Hover');
	
	if(navImage.style.display != 'none')
	{
		navImage.style.display = 'none';
		navHover.style.display = 'inline-block';
	}
	else
	{
		navHover.style.display = 'none';
		navImage.style.display = 'inline-block';
	}
}
