function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function addErrorIfFieldBlank(field, text) {
	if (field.value == '' || field.value == text) {
		return '- ' + text + '\n';
	}
	return '';
}

function submitTrialRequest(f) {
	// validate the data
	var sMessage = "";
	sMessage += addErrorIfFieldBlank(f.first_name, 'First Name');
	sMessage += addErrorIfFieldBlank(f.last_name, 'Last Name');
	sMessage += addErrorIfFieldBlank(f.company, 'Company');
	sMessage += addErrorIfFieldBlank(f.email, 'Email');
	sMessage += addErrorIfFieldBlank(f.phone, 'Phone');
	sMessage += addErrorIfFieldBlank(f.country, 'Country');
	sMessage += addErrorIfFieldBlank(f.captcha, 'Human Check');
	if (sMessage != "") {
		sMessage = "Please enter the following information:\n\n" + sMessage;
		alert(sMessage);
	} else if (f.captcha.value.toLowerCase() != '28ivw') {
		alert('The human check isn\'t right.  Please type the 5 letters and numbers that you see so we can make sure you\'re a person.');
	} else {
		// it passed validation, so submit the form
		document.getElementById('registerButton').disabled=true;
		f.submit();
		return true;
	}
	// if we reach here, validation failed
	return false;
}

function recalculatePricing(currency) {
	// determine the price per user based on how many users there are of each.
	// the price break objects are an array of [lowest number of users needed, cost] pairs
	var combo1 = $('installType');
	var installType = combo1.options[combo1.selectedIndex].value;
	var individualAdminPrice;
	var individualCommunicationPrice;
	var currencySymbol;
	
	//only show pricing for hosted
	if (installType == 'traditional') {
		$('showPricing').style.display='none';
		$('noShowPricing').style.display='block';
	} else {
		$('showPricing').style.display='block';
		$('noShowPricing').style.display='none';
	
		if (currency == 'cn') {
			currencySymbol = '￥';
			individualAdminPrice = 1050;
			individualCommunicationPrice = 212;
		} else {
			currencySymbol = '$';
			individualAdminPrice = 99;
			individualCommunicationPrice = 20;
		}
		
		// get the number of users
		var admins = $('admins').value;
		var communication = $('communication').value;
		var errorMessage = "";
		
		// make sure they have at least one admin
		// this funky logic also accounts for things like "3g4" and "".
		if ("" != admins && (!(admins >= 1))) {
			errorMessage += "You must have at least one power user<br>";
		}
		else if ("" != admins && (admins >= 15)) {
		 errorMessage += "Please call for pricing on orders larger than 15 power users<br>";	
		}
		$('errorMessage').innerHTML = errorMessage;
		
		// determine the total cost of each group
		var totalAdminPrice = individualAdminPrice * admins;
		var totalCommunicationPrice = individualCommunicationPrice * communication;
		var totalLicensePrice = totalAdminPrice + totalCommunicationPrice;
		
		// determine the connect price
		var connectPrice = 0;
		//if ($('connect').checked) {
		//	connectPrice = Math.round(totalLicensePrice * 0.1);
		//	totalLicensePrice = totalLicensePrice + connectPrice;
		//}
		
		// get the additional fee based on install type
		var additionalFee = 0;
		
		// add up the total
		var grandTotal = totalLicensePrice + additionalFee;
		
		// since nothing has errored out, display all of the information on the page
		displayCurrency(individualAdminPrice, currencySymbol, 'individualAdminPrice');
		displayCurrency(individualCommunicationPrice, currencySymbol, 'individualCommunicationPrice');
		displayCurrency(totalAdminPrice, currencySymbol, 'totalAdminPrice');
		displayCurrency(totalCommunicationPrice, currencySymbol, 'totalCommunicationPrice');
		displayCurrency(connectPrice, currencySymbol, 'connectPrice');
		displayCurrency(grandTotal, currencySymbol, 'grandTotal');
	}
}

// this is used by recalculatePricing() to write currency values out to the screen
function displayCurrency(value, currencySymbol, domId) {
	var formatted = currencySymbol + addCommas(value);
	
	// strip off the cents
	var index = formatted.indexOf('.');
	if (index > 0) {
		formatted = formatted.substring(0, index);
	}
	
	$(domId).innerHTML = formatted;
}

function quoteOver() {
	if ($('OverlayContainer').style.opacity == 0) {
		$('homeQuoteControls').addClass('playIcon');
	}
}

function quoteOff() {
	$('homeQuoteControls').removeClass('playIcon');
}

function quoteClick() {
	if ($('OverlayContainer').style.opacity == 0) {
		show2.next();
		show2.stop();
		$('homeQuoteControls').removeClass('playIcon');
	}
}

function onFocusField(field, text) {
	// if the field still displays default text
	if (field.value == text) {
		// clear the field to allow for user input
		field.value = '';
	}
	// always set the font color to black
	field.style.color = 'black';
}

function onBlurField(field, text) {
	// if the field doesn't have a value
	if (field.value == '') {
		// re-populate the default
		field.value = text;
		// set the color to grey
		field.style.color = '#aaa';
	}
}

function toggleLanguageSelection() {
	var menuNode = document.getElementById('chooseLanguage');
	if (menuNode.style.display == 'block') {
		menuNode.style.display = "none";
	} else {
		menuNode.style.display = "block";
	}
}

function hideLanguageSelection() {
	document.getElementById('chooseLanguage').style.display = "none";
}

function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}

function fixOnMouseOut(element, event, functionToRun) {
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target = event.relatedTarget;
	}
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		functionToRun();
	}
}

function submitCreditCard (f) {
	// Build the missing value message
	var sMessage = "";
	sMessage += addErrorIfFieldBlank(f.first_name, 'First Name');
	sMessage += addErrorIfFieldBlank(f.last_name, 'Last Name');
	sMessage += addErrorIfFieldBlank(f.company, 'Company');
	sMessage += addErrorIfFieldBlank(f.email, 'Email');
	sMessage += addErrorIfFieldBlank(f.phone, 'Phone');
	sMessage += addErrorIfFieldBlank(f.address, 'Street Address');
	sMessage += addErrorIfFieldBlank(f.country, 'Country');
	sMessage += addErrorIfFieldBlank(f.city, 'City');
	sMessage += addErrorIfFieldBlank(f.zip, 'Zip Code');
	sMessage += addErrorIfFieldBlank(f.amount, 'Purchase Amount');
	sMessage += addErrorIfFieldBlank(f.CardName, 'Name on Card');
	sMessage += addErrorIfFieldBlank(f.CardNumber, 'Card Number');
	
	if (sMessage != "") {
		sMessage = "Please enter the following information:\n\n" + sMessage;
		alert(sMessage);
	} else if (!checkCreditCard (f.CardNumber.value, f.CardType.value)) {
		// it passed blanks validation, so check to make sure the credit card number is in the correct form
		alert (ccErrors[ccErrorNo]);
	} else {
		document.getElementById('sendCardButton').disabled=true;
		f.submit();
		return true;
	}
	
	// if we reach here, validation failed
	return false;
}