function showhide(link, id, showtext, hidetext) {
	if (document.getElementById) {
		obj = document.getElementById(id);
		if (obj.style.display == "none") {
			obj.style.display = "";
			if (hidetext != "") {
				link.innerHTML = hidetext;
			}
		} else {
			obj.style.display = "none";
			if (showtext != "") {
				link.innerHTML = showtext;
			}
		}
	}
}

/*
 * Search Form Functions
 */
function doCustomerSearch(theForm) {
	var queryString;
	queryString = "&a=search";
	if (theForm.status.value != "") {
		queryString = queryString + "&status=" + theForm.status.value;
	}
	if (theForm.company.value != "") {
		queryString = queryString + "&company=" + theForm.company.value;
	}
	if (theForm.cid.value != "") {
		queryString = queryString + "&cid=" + theForm.cid.value;
	}
	if (theForm.email.value != "") {
		queryString = queryString + "&email=" + theForm.email.value;
	}
	if (theForm.service.value != "") {
		queryString = queryString + "&service=" + theForm.service.value;
	}
	if (theForm.server.value != "") {
		queryString = queryString + "&server=" + theForm.server.value;
	}
	if (theForm.firstname.value != "") {
		queryString = queryString + "&firstname=" + theForm.firstname.value;
	}
	if (theForm.lastname.value != "") {
		queryString = queryString + "&lastname=" + theForm.lastname.value;
	}
	if (theForm.username.value != "") {
		queryString = queryString + "&username=" + theForm.username.value;
	}
	if (theForm.domain.value != "") {
		queryString = queryString + "&domain=" + theForm.domain.value;
	}
	if (theForm.ipaddress.value != "") {
		queryString = queryString + "&ipaddress=" + theForm.ipaddress.value;
	}
	if (theForm.method.value != "") {
		queryString = queryString + "&method=" + theForm.method.value;
	}
	if (theForm.plan.value != "") {
		queryString = queryString + "&plan=" + theForm.plan.value;
	}
	myConn.connect('customers.php', 'GET', queryString, 'customerWindow', fnWhenDone);
}

function doAccountingReport(theForm) {
	var queryString;
	queryString = "&a=doreport";
	if (theForm.service.value != "") {
		queryString = queryString + "&service=" + theForm.service.value;
	}
	if (theForm.startDate.value != "") {
		queryString = queryString + "&startDate=" + theForm.startDate.value;
	}
	if (theForm.endDate.value != "") {
		queryString = queryString + "&endDate=" + theForm.endDate.value;
	}
	if (theForm.vendor.value != "") {
		queryString = queryString + "&vendor=" + theForm.vendor.value;
	}
	for (i = 0; i < 6; i++) {
		if (theForm.timePeriod[i].checked) {
			queryString = queryString + "&timePeriod=" + theForm.timePeriod[i].value;
		}
	}
	if (theForm.numMonths.value != "") {
		queryString = queryString + "&numMonths=" + theForm.numMonths.value;
	}
	if (theForm.company.value != "") {
		queryString = queryString + "&company=" + theForm.company.value;
	}
	if (theForm.type.value != "") {
		queryString = queryString + "&type=" + theForm.type.value;
	}
	myConn.connect('financialreports.php', 'GET', queryString, 'accountingReport', fnWhenDone);
}

/*
 * Order Form Functions
 */
function enableBillingMethod() {
	if (document.getElementById('billingmethod1').checked == true) {
		document.getElementById('creditcard').style.display = "";
		document.getElementById('paypal').style.display = "none";
	} else if (document.getElementById('billingmethod2').checked == true) {
		document.getElementById('creditcard').style.display = "none";
		document.getElementById('paypal').style.display = "";
	} else {
		document.getElementById('creditcard').style.display = "none";
		document.getElementById('paypal').style.display = "none";
	}
}

function checkManaged() {
	if (document.getElementById("os4").checked) {
		document.getElementById("managed1").checked = true;
		document.getElementById("managed2").disabled = true;
		document.getElementById("managed3").disabled = true;
		document.getElementById("managed4").disabled = true;
	} else {
		document.getElementById("managed2").disabled = false;
		document.getElementById("managed3").disabled = false;
		document.getElementById("managed4").disabled = false;
	}
}

/*
 * Validate Form Functions
 */
function isDigit (c) {
	return ((c >= "0") && (c <= "9"))
}

function isInteger (s) {
	var i;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c)) {
			return false;
		}
	}
	return true;
}

function isEmail (s) {
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")) {
		i++
	}
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;
	while ((i < sLength) && (s.charAt(i) != ".")) {
		i++
	}
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) {
		return false;
	} else {
		return true;
	}
}

function validateContact(contactForm) {
	if (contactForm.name.value == "") {
		alert("Please enter your name.");
		contactForm.name.focus();
		return false;
	}
	if (contactForm.email.value == "") {
		alert("Please enter your e-mail address.");
		contactForm.email.focus();
		return false;
	}
	if (!isEmail(contactForm.email.value)) {
		alert("Please enter a valid e-mail address.");
		contactForm.email.focus();
		return false;
	}
	if (contactForm.phone.value == "") {
		alert("Please enter your phone number.");
		contactForm.phone.focus();
		return false;
	}
	if (contactForm.comments.value == "") {
		alert("Please enter your question or comments.");
		contactForm.comments.focus();
		return false;
	}
	return true;
}

function validateEmploy(employForm) {
	if (employForm.firstname.value == "") {
		alert("Please enter your first name.");
		employForm.firstname.focus();
		return false;
	}
	if (employForm.lastname.value == "") {
		alert("Please enter your last name.");
		employForm.lastname.focus();
		return false;
	}
	if (employForm.position.value == "") {
		alert("Please enter the position you are interested in.");
		employForm.position.focus();
		return false;
	}
	if (employForm.email.value == "") {
		alert("Please enter your e-mail address.");
		employForm.email.focus();
		return false;
	}
	if (!isEmail(employForm.email.value)) {
		alert("Please enter a valid e-mail address.");
		employForm.email.focus();
		return false;
	}
	if (employForm.phone.value == "") {
		alert("Please enter your phone number.");
		employForm.phone.focus();
		return false;
	}
	if (employForm.coverletter.value == "") {
		alert("Please enter your cover letter.");
		employForm.coverletter.focus();
		return false;
	}
	return true;
}

function validateCreditCard(cardType, cardNumber) {
	var cdReg="^\\d{15,16}$";
	if ( !cardNumber.match(cdReg) ) {
		return false;
	}
	var ckCdLen = cardNumber.length;
	if (cardType.toLowerCase() == "visa" && !(isVisa(cardNumber))) {
		return false;
	}
	if ((cardType.toLowerCase() == "master") && !(isMasterCard(cardNumber))) {
		return false;
	}
	var cdN1 = new Number;
	var cdN2 = new Number;
	for (i = 0; i < ckCdLen; i ++) {
		if (i % 2 != 0 ) {
			cdN1 = eval(cardNumber.substr(i,1)); 
		}
		if (i % 2 == 0) {
			cdN1 = eval(cardNumber.substr(i,1)) * 2;
		}
		if (cdN1 > 9 ) {
			cdN2 += cdN1 - 9;
		} else {
				cdN2 += cdN1;
		}
	}
	mod10 = cdN2 % 10;
	if (mod10 == 0){ 
		return true;
	} else {
		return false;
	}
}

function isVisa(cc) {
	if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4)) {
		return true;
      } else {
		return false;
	}
}

function isMasterCard(cc) {
	firstdig = cc.substring(0,1);
	seconddig = cc.substring(1,2);
	if ((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5))) {
		return true;
	} else {
		return false;
	}
}

function validateOrder(theForm) {
	if (theForm.firstname.value == "") {
		alert("Please enter your first name.");
		theForm.firstname.focus();
		return (false);
	}
	if (theForm.lastname.value == "") {
		alert("Please enter your last name.");
		theForm.lastname.focus();
		return (false);
	}
	if (theForm.street1.value == "") {
		alert("Please enter the address where you recieve your credit card statements.");
		theForm.street1.focus();
		return (false);
	}
	if (theForm.city.value == "") {
		alert("Please enter the city of your billing address.");
		theForm.city.focus();
		return (false);
	}
	if (theForm.state.value == "") {
		alert("Please enter the state of your billing address.");
		theForm.state.focus();
		return (false);
	}
	if (theForm.zip.value == "") {
		alert("Please enter the zip code of your billing address.");
		theForm.zip.focus();
		return (false);
	}
	if (theForm.country.value == "") {
		alert("Please enter the country of your billing address.");
		theForm.country.focus();
		return (false);
	}
	if (theForm.phone.value == "") {
		alert("Please enter a phone number where you can be reached.");
		theForm.phone.focus();
		return (false);
	}
	if (theForm.email.value == "") {
		alert("Please enter your ISP e-mail address.");
		theForm.email.focus();
		return (false);
	}
	if ( !isEmail(theForm.email.value) ) {
		alert("Please enter a valid ISP e-mail address.");
		theForm.email.focus();
		return (false);
	}
	if (theForm.password.value == "") {
		alert("Please enter your preferred password.");
		theForm.password.focus();
		return (false);
	}
	if (theForm.password2.value == "") {
		alert("Please confirm your password.");
		theForm.password2.focus();
		return (false);
	}
	if (theForm.password.value != theForm.password2.value) {
		alert("Your passwords do not match.");
		theForm.password.focus();
		return (false);
	}
	if (!theForm.method[0].checked && !theForm.method[1].checked) {
		alert("Please select a billing method.");
		theForm.method[0].focus();
		return (false);
	}
	if (theForm.method[0].checked) {
		if (theForm.nameoncard.value == "") {
			alert("Please enter the name as it appears on your credit card.");
			theForm.nameoncard.focus();
			return (false);
		}
		if (theForm.accountnum.value == "") {
			alert("Please enter your 16 digit account number without dashes or spaces.");
			theForm.accountnum.focus();
			return (false);
		}
		if ( !isInteger(theForm.accountnum.value) ) {
			alert("Please enter your 16 digit account number without dashes or spaces.");
			theForm.accountnum.focus();
			return (false);
		}
		if (theForm.accountnum.value.charAt(0) == "4") {
			if ( !validateCreditCard("visa", theForm.accountnum.value) ) {
				err = "Please enter a valid credit card number.";
				theForm.accountnum.focus();
				alert(err);
				return false;
			}
		} else {
			if ( !validateCreditCard("master", theForm.accountnum.value) ) {
				err = "Please enter a valid credit card number.";
				theForm.accountnum.focus();
				alert(err);
				return false;
			}
		}
		if (theForm.cvv2.value == "") {
			alert("Please enter the three digit cvv2 number located on the back of your credit card.");
			theForm.cvv2.focus();
			return (false);
		}
		if ( !isInteger(theForm.cvv2.value) ) {
			alert("Please enter a valid three digit cvv2 number located on the back of your credit card.");
			theForm.cvv2.focus();
			return (false);
		}
		if (theForm.cvv2.value.length > 3) {
			alert("Please enter a valid THREE digit cvv2 number.");
			theForm.cvv2.focus();
			return (false);
		}
	}
	if (theForm.method[1].checked) {
		if (theForm.paypalemail.value == "") {
			alert("Please enter your PayPal e-mail address. PayPal payments from any other address will be denied");
			theForm.paypalemail.focus();
			return (false);
		}
		if ( !isEmail(theForm.paypalemail.value) ) {
			alert("Please enter your valid PayPal e-mail address. PayPal payments from any other address will be denied");
			theForm.paypalemail.focus();
			return (false);
		}
	}
	if ( theForm.years18.checked == false ) {
		alert("If you are not 18 years of age you will need to get someone who is 18 years of age or older to sign up.");
		return (false);
	}
	if ( theForm.readtos.checked == false ) {
		alert("You must agree to our Terms of Service before continuing.");
		return (false);
	}
	return (true);
}

function validateTicket(theForm) {
	if (theForm.subject.value == '') {
		alert("Please enter a subject for your ticket.");
		theForm.subject.focus();
		return (false);
	}
	if (theForm.question.value == '') {
		alert("Please enter your question/comment.");
		theForm.question.focus();
		return (false);
	}
	return (true);
}

/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
	if (element.addEventListener) {
		element.addEventListener(eventType, lamdaFunction, useCapture);
		return true;
	} else if (element.attachEvent) {
		var r = element.attachEvent('on' + eventType, lamdaFunction);
		return r;
	} else {
		return false;
	}
}

/* 
 * Kills an event's propagation and default action
 */
function knackerEvent(eventObject) {
	if (eventObject && eventObject.stopPropagation) {
		eventObject.stopPropagation();
	}
	if (window.event && window.event.cancelBubble ) {
		window.event.cancelBubble = true;
	}
	if (eventObject && eventObject.preventDefault) {
		eventObject.preventDefault();
	}
	if (window.event) {
		window.event.returnValue = false;
	}
}

/* 
 * Safari doesn't support canceling events in the standard way, so we must
 * hard-code a return of false for it to work.
 */
function cancelEventSafari() {
	return false;        
}

/* 
 * Cross-browser style extraction, from the JavaScript & DHTML Cookbook
 * <http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html>
 */
function getElementStyle(elementID, CssStyleProperty) {
	var element = document.getElementById(elementID);
	if (element.currentStyle) {
		return element.currentStyle[toCamelCase(CssStyleProperty)];
	} else if (window.getComputedStyle) {
		var compStyle = window.getComputedStyle(element, '');
		return compStyle.getPropertyValue(CssStyleProperty);
	} else {
		return '';
	}
}

/* 
 * CamelCases CSS property names. Useful in conjunction with 'getElementStyle()'
 * From <http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp>
 */
function toCamelCase(CssProperty) {
	var stringArray = CssProperty.toLowerCase().split('-');
	if (stringArray.length == 1) {
		return stringArray[0];
	}
	var ret = (CssProperty.indexOf("-") == 0) ? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1) : stringArray[0];
	for (var i = 1; i < stringArray.length; i++) {
		var s = stringArray[i];
		ret += s.charAt(0).toUpperCase() + s.substring(1);
	}
	return ret;
}

/*
 * Disables all 'test' links, that point to the href '#', by Ross Shannon
 */
function disableTestLinks() {
	var pageLinks = document.getElementsByTagName('a');
	for (var i = 0; i < pageLinks.length; i++) {
		if (pageLinks[i].href.match(/[^#]#$/)) {
			addEvent(pageLinks[i], 'click', knackerEvent, false);
		}
	}
}

/* 
 * Cookie functions
 */
function createCookie(name, value, days) {
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = '; expires=' + date.toGMTString();
	}
	document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
	var cookieCrumbs = document.cookie.split(';');
	var nameToFind = name + '=';
	for (var i = 0; i < cookieCrumbs.length; i++) {
		var crumb = cookieCrumbs[i];
		while (crumb.charAt(0) == ' ') {
			crumb = crumb.substring(1, crumb.length); /* delete spaces */
		}
		if (crumb.indexOf(nameToFind) == 0) {
			return crumb.substring(nameToFind.length, crumb.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, '', -1);
}