/*
here is a simple function to tab through a form automatically when a known number of digits or letters is required - the required variables are  spaces (the maximum number of digits for a given field), currLoc (the id of the field being completed), and nextLoc (the field that will be auto-tabbed to), and example of its usage is onkeyup="nextTab(3, 'areacode', 'prefix')" for a form that takes a phone number as an areacode, prefix and number, the onkeyup method will run the function everytime the user inputs a new number
example:
First Field: <input type="text" name="jim" id="jim" maxlength="4" onkeyup="nextTab(4, 'jim', 'bo')"><br />
Next Field: <input type="text" name="bo" id="bo" maxlength="5">

NOTE: Both name and id are required, the javascript requires the id attribute and the form requires the name attribute.

*/

function nextTab(spaces, currLoc, nextLoc) {
countLength = document.getElementById(currLoc);
nextField = document.getElementById(nextLoc);
	if (countLength.value.length >= spaces) {
	nextField.select();
	}
}

/*
this function is used as a rudimentary way to keep people from just putting any old thing into a form that requires an email address
*/

function checkEmail(email) {
emailaddress = document.getElementById(email);
emailre = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/;

if (emailre.test(emailaddress.value)) {
return true;
}
alert("Perhaps, you should try entering a valid email address!");
emailaddress.select();
return false;
}

/*
this function is used to get factorial responses, as the creators of JavaScript for some strange left out this common mathematical function
*/

function factorialisMajor(factorize) {
ourResp = 1
for (i = 1; i <= factorize; i++) {
ourResp = i * ourResp;
}
return ourResp;
}

/*this function turns numbers into a comma in the right place kinda number
Note: it only works with integers
*/

function commarator(jamesFreely) {
jamesFreely = jamesFreely.toString();
workingNum = jamesFreely.split('.');
workingNum1 = workingNum[0];
workingNum2 = workingNum.length > 1 ? '.' + workingNum[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(workingNum1)) {
	workingNum1 = workingNum1.replace(rgx, '$1' + ',' + '$2');
}
return workingNum1 + workingNum2;
}

/* This function is used to format a telephone number
*/

function phoneFormat(phoneNumber) {
phoneNumber = phoneNumber.toString();
phoneStyle = /(\()?(\d{3})(\D+)?(\d{3})(\D+)?(\d{4})(\d+)?/
phoneStyle2 = /(\()?(\d{3})(\D+)?(\d{3})(\D+)?(\d{4})(\D+)?(\d+)/
if (phoneStyle2.test(phoneNumber)) {
phoneNumber = phoneNumber.replace(phoneStyle2, '($2)$4-$6 x$8');
}
if (phoneStyle.test(phoneNumber)) {
phoneNumber = phoneNumber.replace(phoneStyle, '($2)$4-$6');
}
return phoneNumber;
}

/*
this function builds upon the phoneFormat() function by adding the ability to format multiple phone numbers held within a <textarea>
*/
function phoneFormat2(phoneList) {
listreg = /(\n)/;
davey = new Array;
davey = phoneList.split(listreg);
jimmy = "";
for (i=0;i<davey.length;i++) {
jimmy += phoneFormat(davey[i]);
}
return jimmy;
}

/*
this function takes a list of names, on separate lines within a <textarea> and sorts them alphabetically
*/
function nameSort(nameList) {
lineBreaker = /(\n)/;
nameArray = nameList.split(lineBreaker);
nameArray = nameArray.sort();
finalList = "";
for(x=0;x<nameArray.length;x++) {
	if (nameArray[x] != '\n' && nameArray[x] != '') {
	finalList += nameArray[x] + "\n";
	}
}
return finalList;
}

/*
This function can be used to create pop-up text that contains additional information that would not typically fit within the confines of a standard title attribute, it can also contain images or hypertext
This function requires the use of CSS to hide the pop-up information as the default status.
Calls to this function should be made like this infoPop(event, 'elementId') - where elementId is the id attribute given to your pop-up element.
*/
function infoPop(evt, itemId) {
showInfo = document.getElementById(itemId);
if (evt.pageY) {
	if (evt.pageX >= 400) {
	leftLoc = evt.pageX - 270;
	} else {
	leftLoc = evt.pageX + 20;
	}
	if (evt.pageY >= 250) {
	topLoc = evt.pageY - 170;
	} else {
	topLoc = evt.pageY - 20;
	}
}
if (evt.y) {
	if (evt.x >= 400) {
	leftLoc = evt.x - 270;
	} else {
	leftLoc = evt.x + 20;
	}
	if (evt.y >= 250) {
	topLoc = evt.y - 170;
	} else {
	topLoc = evt.y - 20;
	}
}
showInfo.style.top = topLoc;
showInfo.style.left = leftLoc;
if (showInfo.style.display != "block") {
	showInfo.style.display = "block";
	} else {
	showInfo.style.display = "none";
	}
}

/*
This function is the same as the infoPop function, only it requires a complimentary function to close the pop-up window.
*/
function infoPopUp(evt, itemId) {
showInfo = document.getElementById(itemId);
if (evt.pageY) {
	if (evt.pageX >= 400) {
	leftLoc = evt.pageX - 270;
	} else {
	leftLoc = evt.pageX + 20;
	}
	if (evt.pageY >= 250) {
	topLoc = evt.pageY - 170;
	} else {
	topLoc = evt.pageY - 20;
	}
}
if (evt.y) {
	if (evt.x >= 400) {
	leftLoc = evt.x - 270;
	} else {
	leftLoc = evt.x + 20;
	}
	if (evt.y >= 250) {
	topLoc = evt.y - 170 + document.body.scrollTop;
	} else {
	topLoc = evt.y - 170 + document.body.scrollTop;
	}
}
showInfo.style.top = topLoc;
showInfo.style.left = leftLoc;
showInfo.style.display = "block";
}

/* 
This function closes the open pop-up
*/
function infoPopDown(itemId) {
popDown = document.getElementById(itemId);
popDown.style.display = "none";
}

/*
This function allows you to close the pop-up after a brief delay, at least one second is recommended if there are any links within the pop-up. The delayAmt variable is the number of seconds, it is converted to microseconds within the function.
*/
function delayPopDown(delayAmt, itemDown) {
itemDownPass = itemDown;
delayAmt = delayAmt * 1000;
setTimeout("infoPopDown(itemDownPass)", delayAmt);
}

/*
This function is used to format currency, including commas and cents.
*/
function moneyFormat(floyd) {
floyd_1 = Math.floor(floyd);
floyd_2 = Math.round((floyd - floyd_1) * 100);
if (floyd_2 < 10) {
floyd_2 = "0" + floyd_2;
}
floyd_1 = commarator(floyd_1);
return floyd_1 + "." + floyd_2;
}