var total, subTotal = 0, discount = 0, numItems = 0, payment = "payLater", businessType = "commercial", discountDollars = 0;
var services =      new Array ("businessCard", "professionalBusinessCard", "businessCardDesign", "flyer", "flyerDesign", "bronzeMarketing", "silverMarketing", "goldMarketing", "websiteOptimization", "logoDesign", "templateDesign", "bannerDesign", "buttonDesign", "highResolutionDesign", "basicHostingMonthly", "basicHostingQuarterly", "basicHostingYearly", "expandedHostingMonthly", "expandedHostingQuarterly", "expandedHostingYearly", "multimediaHostingMonthly", "multimediaHostingQuarterly", "multimediaHostingYearly", "monthlyMaintenance", "htmlLesson");
var servicePrices = new Array (24.95         , 109.95                    , 14.95               , 94.95  , 49.95        , 14.95            , 29.95            , 84.95          , 34.95                  , 49.95       , 79.95           , 24.95         , 14.95         , 49.95                 , 7.77                 , 23.31                  , 93.24               , 10.77                   , 32.31                     , 129.24                 , 19.77                     , 59.31                       , 237.24                   , 19.95               , 49.95       );

var cartItems = new Array();
var purchasedServices = new Array();
var loc = " " + document.location.search;
var loaded = 0;
var d = new Date();
var date = d.getDate();
var month = d.getMonth();


// PURCHASE FORM
//** ** ** ** **


// Adds an item to the cart
function add(service) {
	numItems++;
	subTotal += getPrice(service);
	discountDollars += getDiscount(service);
	subTotal = Math.round(subTotal*100) / 100;
	cartItems[numItems-1] = service;
	displaySubTotal();
}

// Removes an item from the cart
function remove(service) {
	numItems--;
	subTotal -= getPrice(service);
	discountDollars -= getDiscount(service);
	subTotal = Math.round(subTotal*100) / 100;
	cartItems.splice(searchCart(service), 1);
	displaySubTotal();
}

// Graphical checkbox-type buttons
function checkboxHandler(cBox) {
	if (cBox.src.indexOf("on") != -1) {
		cBox.src = "acc_check_off.gif";
		remove(cBox.id.substring(6, cBox.id.length));
	}
	else {
		cBox.src = "acc_check_on.gif";
		add(cBox.id.substring(6, cBox.id.length));
	}
}

// Graphical radio-type buttons (selecting one deselects other)
function paymentHandler(radio, otherRadio) {
	radio.src = "acc_check_on.gif";
	document.getElementById(otherRadio).src = "acc_check_off.gif";
	payment = radio.id.substring(6,radio.id.length);
}

// Graphical radio-type buttons (selecting one deselects other)
function businessTypeHandler(radio, otherRadio) {
	if (radio.src.indexOf("acc_check_on.gif") == -1) {
		if (radio.id.indexOf("nonProfit") != -1)
			discount += .15;
		else
			discount -= .15;
		radio.src = "acc_check_on.gif";
		document.getElementById(otherRadio).src = "acc_check_off.gif";
		businessType = radio.id.substring(6,radio.id.length);

		displaySubTotal();
	}
}

// Graphical radio-type buttons (selecting one deselects other)
function hostingHandler(radio) {
	if (radio.src.indexOf("on") != -1) {
		radio.src = "acc_check_off.gif";
		remove(radio.id.substring(6, radio.id.length));
		return;
	}

	var hostingType = radio.id.substring(radio.id.indexOf("_")+1, radio.id.indexOf("Hosting"));
	var hostingService = "check_" + hostingType + "Hosting";
	document.getElementById(hostingService + "Monthly").src = "acc_check_off.gif";
	document.getElementById(hostingService + "Quarterly").src = "acc_check_off.gif";
	document.getElementById(hostingService + "Yearly").src = "acc_check_off.gif";

	radio.src = "acc_check_on.gif";

	if (searchCartSubscriptions(radio.id.substring(6, radio.id.indexOf("Hosting"))) == -1)
		add(radio.id.substring(6, radio.id.length));
	else {
		remove(searchCartSubscriptions(radio.id.substring(6, radio.id.indexOf("Hosting"))));
		add(radio.id.substring(6, radio.id.length));
	}
}

// Gets the price of a service based on the price prescribed in the servicePrices array
function getPrice(s) {
	for (i = 0; i < services.length; i++) {
		if (services[i] == s) {
			return servicePrices[i];
		}
	}

	return 0;
}

function getDiscount(s) {
	for (i = 0; i < services.length; i++) {
		if (services[i] == s) {
			if (month == 11 && date >= 13 && date <= 31) {
				return servicePrices[i]*.2;
			}
		}
	}

	return 0;
}

// Finds a string in the cart array
function searchCart(s) {
	for (i = 0; i < cartItems.length; i++)
		if (cartItems[i] == s)
			return i;

	return -1;
}

// Finds a string in the cart array
function searchCartSubscriptions(s) {
	for (i = 0; i < cartItems.length; i++)
		if (cartItems[i].indexOf(s) != -1)
			return cartItems[i];

	return -1;
}

// Prints the subtotal in correct $x.xx format
function displaySubTotal() {
	var s = "$" + subTotal;
	var d = ((Math.round(100*(subTotal * discount + discountDollars))/100));
	total = "$" + (Math.round(100*(subTotal - d))/100);
	d = "$" + d;
	document.getElementById("itemCount").value = numItems;
	document.getElementById("subtotal").value = ((s.indexOf(".") > s.length-3)? s + "0" : s) + ((s.indexOf(".") == -1)?".00":"");
	document.getElementById("discount").value = ((d.indexOf(".") > d.length-3)? d + "0" : d) + ((d.indexOf(".") == -1)?".00":"");
	document.getElementById("total").value = ((total.indexOf(".") > total.length-3)? total + "0" : total) + ((total.indexOf(".") == -1)?".00":"");
}

// Proceeds to personal info input
function proceedToOrderForm() {
	var services = "";

	for (i = 0; i < cartItems.length; i++) {
		services += cartItems[i] + ((i != cartItems.length-1)?"+":"");
	}

	var urlAddons = "?" + payment + "=" + total + "&businessType=" + businessType + "&services=" + services;
	document.location.href = "order.shtml" + urlAddons;
}


// ORDER FORM
//** ** ** **

function displaySpecificFields() {
	getServices();

	for (j = 0; j < purchasedServices.length; j++) {
		if (purchasedServices[j].indexOf("Hosting") != -1)
			displayHostingInfo(purchasedServices[j]);
		else if (purchasedServices[j].indexOf("Design") != -1 || purchasedServices[j].indexOf("flyer") != -1 || purchasedServices[j].indexOf("business") != -1)
			displayGraphicDesignInfo(purchasedServices[j]);
		else if (purchasedServices[j].indexOf("Marketing") != -1)
			displayMarketingInfo(purchasedServices[j]);
		else
			displayCommentInfo(purchasedServices[j]);

		document.write('<tr><td colspan=2><hr></td></tr>');
	}

	document.write('<input type="hidden" name="total" id="total" value="'+total+'">');

	displayPaymentInfo();
}

function displayCommentInfo(name) {
	document.write('  <tr>');
	document.write('    <td colspan=2 align="center"><font class="red"><b>' + getProperName(name) + '</b></font></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td>Comments:</td>');
	document.write('    <td><textarea name="' + name + '_Comments" id="' + name + '_Comments" rows=3 cols=31></textarea><br></td>');
	document.write('  </tr>');
}

function displayGraphicDesignInfo(name) {
	document.write('  <tr>');
	document.write('    <td colspan=2 align="center"><font class="red"><b>' + getProperName(name) + '</b></font><br><font class="smaller">Please give as much information as you can</font></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td>Size:</td>');
	document.write('    <td><input type="text" name="' + name + '_Fonts" id="' + name + '_Size" style="width: 205"><br></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td>Fonts:</td>');
	document.write('    <td><input type="text" name="' + name + '_Fonts" id="' + name + '_Fonts" style="width: 205"><br></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td>Colors:</td>');
	document.write('    <td><input type="text" name="' + name + '_Colors" id="' + name + '_Colors" style="width: 205"><br></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td><font class="red">*</font>Comments:</td>');
	document.write('    <td><textarea name="' + name + '_Comments" id="' + name + '_Comments" rows=3 cols=31></textarea><br></td>');
	document.write('  </tr>');
	document.getElementById("required").value += ','+name+'_Comments';
}

function displayHostingInfo(name) {
	document.write('  <tr>');
	document.write('    <td colspan=2 align="center"><font class="red"><b>' + getProperName(name) + '</b></font></td>');
	document.write('  </tr>');
	document.write('  <tr valign="top">');
	document.write('    <td><font class="red">*</font>Domain:</td>');
	document.write('    <td>www.<input type="text" name="' + name + '_Domain" id="' + name + '_Domain" style="width: 205"><br><center><a href="http://www.000domains.com/whois" target=_blank class="smaller">Is it available?</a></center></td>');
	document.write('    <input type="hidden" name="' + name + '_Transfer" id="' + name + '_transfer" value="no">');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td colspan=2><table cellspacing=10><tr valign="top"><td><font class="red">*</font>Username:<br>');
	document.write('    <input type="text" name="' + name + '_Username" id="' + name + '_Username" style="width: 150"></td>');
	document.write('    <td><font class="red">*</font>Password:<br>');
	document.write('    <input type="password" name="' + name + '_Password" id="' + name + '_Password" style="width: 150"><br>');
	document.write('    <font class="red">*</font>Confirm Password:<br>');
	document.write('    <input type="password" name="' + name + '_VerifyPassword" id="' + name + '_VerifyPassword" style="width: 150" onkeyup="document.getElementById(\''+ name +'_Verify\').src=((this.value == document.getElementById(\''+ name +'_Password\').value)?\'check.gif\' : \'x.gif\')"> <img src="spacer.gif" id="'+ name + '_Verify"></td></tr></table><img src="acc_check_off.gif" name="check_'+name+'_transfer" onmouseup="this.src = ((this.src.indexOf(\'on\') != -1) ? \'acc_check_off.gif\' : \'acc_check_on.gif\'); document.getElementById(\'' + name + '_transfer\').value = ((this.src.indexOf(\'off\') != -1) ? \'no\' : \'yes\');" alt="Click to transfer domain"> I am transfering from another host.<br></td>');
	document.write('  </tr>');
	document.getElementById("required").value += ','+name+'_Domain,'+name+'_Username,'+name+'_Password,'+name+'_VerifyPassword';
}

function displayMarketingInfo(name) {
	document.write('  <tr>');
	document.write('    <td colspan=2 align="center"><font class="red"><b>' + getProperName(name) + '</b></font></td>');
	document.write('  </tr>');
	document.write('  <tr valign="top">');
	document.write('    <td><font class="red">*</font>Site Title:</td>');
	document.write('    <td><input type="text" name="' + name + '_siteTitle" id="' + name + '_siteTitle" style="width: 205"></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td><font class="red">*</font>URL:</td>');
	document.write('    <td><input type="text" name="' + name + '_URL" id="' + name + '_URL" value="http://" style="width: 205"></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td><font class="red">*</font>Keywords:</td>');
	document.write('    <td><input type="text" name="' + name + '_keywords" id="' + name + '_keywords" style="width: 205"></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td><font class="red">*</font>Brief Description:<br><font class="smaller">up to 200 characters</font></td>');
	document.write('    <td><textarea name="' + name + '_briefDescription" id="' + name + '_briefDescription" rows=3 cols=31 maxlength=200></textarea></td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td>&nbsp; Long Description:</td>');
	document.write('    <td><textarea name="' + name + '_description" id="' + name + '_description" rows=3 cols=31></textarea></td>');
	document.write('  </tr>');
	document.getElementById("required").value += ','+name+'_siteTitle,'+name+'_URL,'+name+'_keywords,'+name+'_briefDescription';
}

function displayPayPalForm() {
document.write('<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target=_blank>');
document.write('<input type="hidden" name="cmd" value="_xclick">');
document.write('<input type="hidden" name="business" value="ian@degreethree.com">');
document.write('<input type="hidden" name="item_name" value="Services - Degree Three Design">');
document.write('<input type="hidden" name="amount" value="'+total+'">');
document.write('<input type="hidden" name="no_shipping" value="1">');
document.write('<input type="hidden" name="no_note" value="1">');
document.write('<input type="hidden" name="currency_code" value="USD">');
document.write('<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" id="PayPalSubmit" alt="Make payments with PayPal - it\'s fast, free and secure!" style="height:0; width:0; border: 0px;">');
document.write('</form>');
loaded = 1;
}

function displayPaymentInfo() {
	document.write('  <tr>');
	document.write('    <td colspan=2><br><center><h3>Payment</h3></center> You have chosen to <b>' + getProperName(payment) + '</b>.  Your total is:<b>' + total + '</b><br><br>');
	if (payment.indexOf("Now") != -1)
		document.write('    Upon completion of this form, you will be asked for your personal information again.  This is for billing purposes only.<br><br>');
	else
		document.write('    We will contact you with further information concerning your billing.<br><br>');
	document.write('    </td>');
	document.write('  </tr>');
}

function findElement(e, arr) {
	for (i = 0; i < arr.length; i++) {
		if (arr[i] == e)
		    return i;
	}

	return -1;
}

function getServices() {
	var s = loc.substring(loc.indexOf("?")+1, loc.length);
	var i = 0;

	payment = s.substring(0,s.indexOf("="));
	s = s.substring(s.indexOf("=")+1,s.length);

	total = s.substring(0,s.indexOf("&"));
	s = s.substring(s.indexOf("=")+1,s.length);

	businessType = s.substring(0,s.indexOf("&"));
	s = s.substring(s.indexOf("=")+1,s.length);

	while (s.indexOf("+") != -1) {
		purchasedServices[i] = s.substring(0, ((s.indexOf("+") != -1)?s.indexOf("+"):s.length));
		s = s.substring(((s.indexOf("+") != -1)?s.indexOf("+")+1:s.length), s.length);
		i++;
	}

	purchasedServices[i] = s.substring(0, s.length);

}

// INQUIRY
//** ** **

// Graphical checkbox-type buttons
function inquiryHandler(cBox) {
	if (cBox.src.indexOf("on") != -1) {
		cBox.src = "acc_check_off.gif";
		inquiryRemove(cBox.id.substring(6, cBox.id.length));
	}
	else {
		cBox.src = "acc_check_on.gif";
		inquiryAdd(cBox.id.substring(6, cBox.id.length));
	}
}

function inquiryAdd(service) {
	var html = ' <b>' + getProperName(service)+':</b><br><textarea id='+service+' name='+service+' rows=3 cols=45></textarea><br><br> ';

	document.getElementById("commentFields").innerHTML += html.toLowerCase();
	sizeMenu();
}

function inquiryRemove(service) {
	var q = (window.navigator.appName.indexOf("Explorer") != -1)?"":((window.navigator.appName.indexOf("Opera") != -1)?"\'":"\"");
	var html = '<b>' + getProperName(service)+':</b><br><textarea id='+q+service+q+' name='+q+service+q+' rows='+q+'3'+q+' cols='+q+'45'+q+'></textarea><br><br>';
	html = html.toLowerCase();
	var innerHTML = document.getElementById("commentFields").innerHTML.toLowerCase();
	innerHTML = innerHTML.replace(html,"");
	document.getElementById("commentFields").innerHTML = innerHTML;
	sizeMenu();
}
