$.ui.dialog.defaults.bgiframe = true;
$(function(){
	$('#full-banner-preview-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 880,
		height: 250
	});
	$('#full-banner-preview-link').click(function(){
		$('#full-banner-preview-dialog').dialog('open');
		return false;
	});
	
	$('#help-cubeguard-complete-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 341
	});
	$('#help-cubeguard-complete-link').click(function(){
		$('#help-cubeguard-complete-dialog').dialog('open');
		return false;
	});
	
	$('#help-cartridge-only-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 230
	});
	$('#help-cartridge-only-link').click(function(){
		$('#help-cartridge-only-dialog').dialog('open');
		return false;
	});
	
	$('#standard-complete-volume-pricing-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 350
	});
	$('#standard-complete-volume-pricing-link').click(function(){
		$('#standard-complete-volume-pricing-dialog').dialog('open');
		return false;
	});
	$('#standard-cartridge-only-volume-pricing-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 350
	});
	$('#standard-cartridge-only-volume-pricing-link').click(function(){
		$('#standard-cartridge-only-volume-pricing-dialog').dialog('open');
		return false;
	});
	$('#byo-complete-volume-pricing-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 350
	});
	$('#byo-complete-volume-pricing-link').click(function(){
		$('#byo-complete-volume-pricing-dialog').dialog('open');
		return false;
	});
	$('#byo-cartridge-only-volume-pricing-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 350
	});
	$('#byo-cartridge-only-volume-pricing-link').click(function(){
		$('#byo-cartridge-only-volume-pricing-dialog').dialog('open');
		return false;
	});
	
	$('#post-mount-closeup-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 506,
		height: 527
	});
	$('#post-mount-closeup-link').click(function(){
		$('#post-mount-closeup-dialog').dialog('open');
		return false;
	});	
	$('#banner-closeup-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 506,
		height: 540
	});
	$('#banner-closeup-link').click(function(){
		$('#banner-closeup-dialog').dialog('open');
		return false;
	});
	$('#cartridge-closeup-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 506,
		height: 527
	});
	$('#cartridge-closeup-link').click(function(){
		$('#cartridge-closeup-dialog').dialog('open');
		return false;
	});
	
	$('#help-order-number-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 340
	});
	$('#help-order-number-link').click(function(){
		$('#help-order-number-dialog').dialog('open');
		return false;
	});
	$('#help-thankyou-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 300,
		buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
	});
		$('#standard-designs-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 375,
		height: 425
	});
	$('#standard-designs-link').click(function(){
		$('#standard-designs-dialog').dialog('open');
		return false;
	});
	$('#corporate-sales-thankyou-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 300,
		buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
	});
	
	$('#shipping-rate-table-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 620
	});
	$('#shipping-rate-table-link').click(function(){
		$('#shipping-rate-table-dialog').dialog('open');
		return false;
	});
	
	$('#newsletter-signup-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 630,
		height: 620
	});
	
	$('#error-msg-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 400,
		buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
	});
	
	$('#confirmation-msg-dialog').dialog({
		autoOpen: false,
		resizable: false,
		width: 400,
		buttons: {
				Yes: function() {
					$(this).dialog('close');
					document.form2.submit();
				},
				No: function() {
					$(this).dialog('close');
				}
			}
	});
	
	if(window.location.href.indexOf("ty=1") != -1) {
		$('#help-thankyou-dialog').dialog('open');
		$('#corporate-sales-thankyou-dialog').dialog('open');
	};
});
function newsletterSignUp() {
	document.ccoptin.submit();
	$('#newsletter-signup-dialog').dialog('open');	
}
function isEmail(string) {
	return (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}
function isOrderNumber(sText) {
	var ValidChars = "0123456789 ";
	var isOrderNumber=true;
	var Char;
	for (i = 0; i < sText.length && isOrderNumber == true; i++) 
		{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			 {
			 isOrderNumber = false;
			 }
		}
	return isOrderNumber;
}

function CountLeft(field, count, max) {
	if (field.value.length > max)
		field.value = field.value.substring(0, max);
	else
		count.value = max - field.value.length;
}
function CountUppercase(field) {
	if ((field.value.length > 18) && (field.value.replace(/[^A-Z]/g, "").length > 4) || (field.value.length > 17) && (field.value.replace(/[^A-Z]/g, "").length > 11))
		return true;
}
function updatePreviewRepeat() {
	url = 'http://previews.cubeguard.com/createimage-repeat-segment.asp?';
	url = url + 'img=' + escape(document.form1.imagefilename.value);
	for( i = 0; i < document.form1.fonts.length; i++ )	{
		if( document.form1.fonts[i].checked == true ) {
		url = url + '&font=' + document.form1.fonts[i].value;
		break;
			}
	}
	url = url + '&msg1=' + escape(document.form1.msg1.value);
	url = url + '&msg2=' + escape(document.form1.msg2.value);
	url = url + '&msg3=' + escape(document.form1.msg3.value);	
	if (CountUppercase(document.form1.msg1) || CountUppercase(document.form1.msg2) || CountUppercase(document.form1.msg3)) {
		$('#error-msg').text("We're sorry.  Due to space limitations, you will have to use fewer Upper Case characters to fit everything on this line.");
		$('#error-msg-dialog').dialog('open');
	}
	else
		document.BannerPreview.src = url;
}
function openFullPreviewRepeat() {
	url = 'http://previews.cubeguard.com/createimage-repeat-full.asp?';
	url = url + 'img=' + escape(document.form1.imagefilename.value) + '-full.jpg';
	for( i = 0; i < document.form1.fonts.length; i++ )	{
		if( document.form1.fonts[i].checked == true ) {
		url = url + '&font=' + document.form1.fonts[i].value;
		break;
		}
	}
	url = url + '&msg1=' + escape(document.form1.msg1.value);
	url = url + '&msg2=' + escape(document.form1.msg2.value);
	url = url + '&msg3=' + escape(document.form1.msg3.value);	
	document.FullBannerPreview.src = url;
	$('#full-banner-preview-dialog').dialog('open');
}
function updatePreviewPanoramic() {
	url = 'http://previews.cubeguard.com/createimage-panoramic-segment.asp?';
	url = url + 'img=' + escape(document.form1.imagefilename.value) + '-segment.jpg';
	for( i = 0; i < document.form1.fonts.length; i++ )	{
		if( document.form1.fonts[i].checked == true ) {
		url = url + '&font=' + document.form1.fonts[i].value;
		break;
		}
	}
	url = url + '&msg=' + escape(document.form1.msg.value);
	document.BannerPreview.src = url;
}
function openFullPreviewPanoramic() {
	url = 'http://previews.cubeguard.com/createimage-panoramic-full.asp?';
	url = url + 'img=' + escape(document.form1.imagefilename.value) + '-full.jpg';
	for( i = 0; i < document.form1.fonts.length; i++ )	{
		if( document.form1.fonts[i].checked == true ) {
		url = url + '&font=' + document.form1.fonts[i].value;
		break;
		}
	}
	url = url + '&msg=' + escape(document.form1.msg.value);
	document.FullBannerPreview.src = url;
	$('#full-banner-preview-dialog').dialog('open');
}
function changeFormAction(productid) {
	document.form2.action = "http://order.store.yahoo.net/cgi-bin/wg-order?yhst-25797697937865+" + productid;
}
function addToCartBYORepeat() {
	if (!document.form1.termsconditions.checked) {
		//alert("must agree to terms and conditions!");
		$('#error-msg').text("Please read and accept our Terms & Conditions.");
		$('#error-msg-dialog').dialog('open');
		return false;
	}
	else {		
		document.form2['vw-inscription-4C696E652031'].value = document.form1.msg1.value;
		document.form2['vw-inscription-4C696E652032'].value = document.form1.msg2.value;
		document.form2['vw-inscription-4C696E652033'].value = document.form1.msg3.value;
		for( i = 0; i < document.form1.fonts.length; i++ )	{
			if( document.form1.fonts[i].checked == true ) {
				switch (document.form1.fonts[i].value) {
				case "CaflischScriptPro":
					document.form2.Font.value = "Caflisch Script Pro";
					break;
				case "HelveticaNeueBlack":
					document.form2.Font.value = "Helvetica";
					break;
				case "ZiptyDoStandard":
					document.form2.Font.value = "Zipty Do";
					break;
				case "CalibanStandard":
					document.form2.Font.value = "Caliban Std";
					break;
				case "TrajanPro":
					document.form2.Font.value = "Trajan Pro Bold";
					break;
				case "Stubble":
					document.form2.Font.value = "Stubble Bold";
					break;
				default:
					document.form2.Font.value = "Helvetica";
					break;
				}
			}
		}		
		document.form2['Terms and Conditions Accepted'].value = "Y";
		if ((document.form1.msg1.value=="") && (document.form1.msg2.value=="") && (document.form1.msg3.value=="")) {
			$('#confirmation-msg').text("Yes, we get that our banner images are cool all on their own, but are you completely sure you want to order yours without a message?");
			$('#confirmation-msg-dialog').dialog('open');
			return false;
		}
		else {
			return true;
		}
	}
}
function addToCartBYOPanoramic() {
	if (!document.form1.termsconditions.checked) {
		//alert("must agree to terms and conditions!");
		$('#error-msg').text("Please read and accept our Terms & Conditions.");
		$('#error-msg-dialog').dialog('open');
		return false;
	}
	else {		
		document.form2['vw-inscription-4C696E652031'].value = document.form1.msg.value;
		for( i = 0; i < document.form1.fonts.length; i++ )	{
			if( document.form1.fonts[i].checked == true ) {
				switch (document.form1.fonts[i].value) {
				case "CaflischScriptPro":
					document.form2.Font.value = "Caflisch Script Pro";
					break;
				case "HelveticaNeueBlack":
					document.form2.Font.value = "Helvetica";
					break;
				case "ZiptyDoStandard":
					document.form2.Font.value = "Zipty Do";
					break;
				case "CalibanStandard":
					document.form2.Font.value = "Caliban Std";
					break;
				case "TrajanPro":
					document.form2.Font.value = "Trajan Pro Bold";
					break;
				case "Stubble":
					document.form2.Font.value = "Stubble Bold";
					break;
				default:
					document.form2.Font.value = "Helvetica";
					break;
				}
			}
		}		
		document.form2['Terms and Conditions Accepted'].value = "Y";
		if (document.form1.msg.value=="") {
			$('#confirmation-msg').text("Yes, we get that our banner images are cool all on their own, but are you completely sure you want to order yours without a message?");
			$('#confirmation-msg-dialog').dialog('open');
			return false;
		}
		else {
			return true;
		}
	}
}
function emailSupport() {
	showForgotSomethingMsg = false;
	showCheckEntriesMsg = false;
	requireOrderNumber = false;
	document.getElementById("msg-forgetsomething").style.display = 'none';
	document.getElementById("msg-checkentries").style.display = 'none';
	document.getElementById("FullNameLabel").style.color="#606779";
	document.getElementById("EmailLabel").style.color="#606779";
	document.getElementById("InquiryTypeLabel").style.color="#606779";
	document.getElementById("OrderNumberLabel").style.color="#606779";
	document.getElementById("YourMessageLabel").style.color="#606779";
	if( document.helpform.FullName.value == "" ) {
		document.getElementById("FullNameLabel").style.color="#FF0000";
		showForgotSomethingMsg = true;
	}
	if( document.helpform.Email.value == "" ) {
		document.getElementById("EmailLabel").style.color="#FF0000";
		showForgotSomethingMsg = true;
	}
	if( document.helpform.InquiryType.value == "" ) {
		document.getElementById("InquiryTypeLabel").style.color="#FF0000";
		showForgotSomethingMsg = true;
	}
	else if ( (document.helpform.InquiryType.value == "Problem with order") || (document.helpform.InquiryType.value == "Problem with product") || (document.helpform.InquiryType.value == "Request RMA") ) {
		requireOrderNumber = true;
		if( document.helpform.OrderNumber.value == "" ) {
			document.getElementById("OrderNumberLabel").style.color="#FF0000";
			showForgotSomethingMsg = true;
		}
	}
	if( document.helpform.YourMessage.value == "" ) {
		document.getElementById("YourMessageLabel").style.color="#FF0000";
		showForgotSomethingMsg = true;
	}
	if( showForgotSomethingMsg == true ) {
		document.getElementById("msg-forgetsomething").style.display = 'block';
		return false;
	}
	if(!isEmail(document.helpform.Email.value)) {
		document.getElementById("EmailLabel").style.color="#FF0000";
		showCheckEntriesMsg = true;
	}
	if(requireOrderNumber == true) {
		if(!isOrderNumber(document.helpform.OrderNumber.value)) {
			document.getElementById("OrderNumberLabel").style.color="#FF0000";
			showCheckEntriesMsg = true;
		}
	}
	if( showCheckEntriesMsg == true ) {
		document.getElementById("msg-checkentries").style.display = 'block';
		return false;
	}
	return true;
}

function emailSales() {
	showForgotSomethingMsg = false;
	document.getElementById("corporate-sales-msg-forgetsomething").style.display = 'none';
	document.getElementById("YourNameLabel").style.color="#606779";
	document.getElementById("YourEmailLabel").style.color="#606779";
	document.getElementById("InquiryTypeLabel").style.color="#606779";
	if( document.helpform.YourName.value == "" ) {
		document.getElementById("YourNameLabel").style.color="#FF0000";
		showForgotSomethingMsg = true;
	}
	if( document.helpform.YourEmail.value == "" ) {
		document.getElementById("YourEmailLabel").style.color="#FF0000";
		showForgotSomethingMsg = true;
	}
	if( document.helpform.InquiryType.value == "" ) {
		document.getElementById("InquiryTypeLabel").style.color="#FF0000";
		showForgotSomethingMsg = true;
	}
	if( showForgotSomethingMsg == true ) {
		document.getElementById("corporate-sales-msg-forgetsomething").style.display = 'block';
		return false;
	}
	return true;
}

function formatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		if(cents>49)
		num = num + 1;
		num = Math.floor(num/100).toString();
		//if(cents<10)
		//cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num);
}
function CalculateROI() {
	hasError = false;
	document.getElementById("msg-avgnumberinterruptions").style.display = 'none';
	document.getElementById("msg-percentinterruptionseliminated").style.display = 'none';
	document.getElementById("msg-avgtimeimpactofinterruption").style.display = 'none';
	document.getElementById("msg-numberofemployees").style.display = 'none';
	document.getElementById("msg-avgannualcost").style.display = 'none';
	if( isNaN(document.roicalculator.AvgNumberInterruptions.value) || (document.roicalculator.AvgNumberInterruptions.value == "") || (parseInt(document.roicalculator.AvgNumberInterruptions.value) < 1) ) {
		document.getElementById("msg-avgnumberinterruptions").style.display = 'inline';
		hasError = true;
	}
	if( isNaN(document.roicalculator.PercentInterruptionsEliminated.value) || (document.roicalculator.PercentInterruptionsEliminated.value == "") || (parseInt(document.roicalculator.PercentInterruptionsEliminated.value) < 1) || (parseInt(document.roicalculator.PercentInterruptionsEliminated.value) > 100) ) {
		document.getElementById("msg-percentinterruptionseliminated").style.display = 'inline';
		hasError = true;
	}
	if( isNaN(document.roicalculator.AvgTimeImpactOfInterruption.value) || (document.roicalculator.AvgTimeImpactOfInterruption.value == "") || (parseInt(document.roicalculator.AvgTimeImpactOfInterruption.value) < 1) ) {
		document.getElementById("msg-avgtimeimpactofinterruption").style.display = 'inline';
		hasError = true;
	}
	if( isNaN(document.roicalculator.NumberOfEmployees.value) || (document.roicalculator.NumberOfEmployees.value == "") || (parseInt(document.roicalculator.NumberOfEmployees.value) < 1) ) {
		document.getElementById("msg-numberofemployees").style.display = 'inline';
		hasError = true;
	}
	if( isNaN(document.roicalculator.AvgAnnualCost.value) || (document.roicalculator.AvgAnnualCost.value == "") || (parseInt(document.roicalculator.AvgAnnualCost.value) < 1) ) {
		document.getElementById("msg-avgannualcost").style.display = 'inline';
		hasError = true;
	}
	if( hasError == true ) {
		return false;
	}
	document.roicalculator.ProductivitySavingsPerDay.value = parseInt(document.roicalculator.AvgNumberInterruptions.value) * (parseInt(document.roicalculator.PercentInterruptionsEliminated.value) / 100) * parseInt(document.roicalculator.AvgTimeImpactOfInterruption.value);
	document.roicalculator.AnnualProductivitySavings.value = (parseInt(document.roicalculator.ProductivitySavingsPerDay.value) / 480) * parseInt(document.roicalculator.NumberOfEmployees.value) * parseInt(document.roicalculator.AvgAnnualCost.value);
	document.roicalculator.CostToEquipEmployees.value = parseInt(document.roicalculator.NumberOfEmployees.value) * 19.99;
	document.roicalculator.DaysToBreakEven.value = formatNumber(255 * parseInt(document.roicalculator.CostToEquipEmployees.value) / parseInt(document.roicalculator.AnnualProductivitySavings.value),1,true,false,true);
	document.roicalculator.FirstYearROI.value = formatNumber(100 * ((document.roicalculator.AnnualProductivitySavings.value - document.roicalculator.CostToEquipEmployees.value) / document.roicalculator.CostToEquipEmployees.value),0,true,false,true) + '%';
	document.roicalculator.AnnualProductivitySavings.value = formatCurrency(document.roicalculator.AnnualProductivitySavings.value);
	document.roicalculator.CostToEquipEmployees.value = formatCurrency(document.roicalculator.CostToEquipEmployees.value);
	document.roicalculator.ProductivitySavingsPerDayPercentage.value = formatNumber((parseInt(document.roicalculator.ProductivitySavingsPerDay.value) / 480) * 100,1,true,false,true) + '%';
	document.roicalculator.ProductivitySavingsPerDay.value = formatNumber(document.roicalculator.ProductivitySavingsPerDay.value,0,true,false,true) + 'min';
	return false;
}