﻿/*
  * Date	Version	Changes
 * 080604	0.1		File created: copied/created functions from separate files.
 * 080605	0.2		Added functions used to create the starting tag for full-size product image links.
 * 080617	0.2.1		generateMainMenu: changed Jäähdytys into Itula lattiaviilennys and moved it up one step.
 * 080626	0.3		createImageLink: changed to do all the printing work, can handle alternative images too.
 * 080627	0.3.1		calculate -> calcMaterialCosts: calculates total area and costs.
 * 080630	0.3.2		calcMaterialCosts: fixed sum operations (added type cast).
 * 080701	0.3.3		calcMaterialCosts and checkForm: changed field names.
 * 080701	0.4		Added function calcInstallationCosts.
 * 080702	0.4.1		calcMaterialCosts: changed rounding to 1 decimal.
 * 080702	0.4.2		generateMainMenu: class selected -> sel.
 * 080707	0.4.3		calcMaterialCosts, calcInstallationCosts: append sum with zeros, add euro character.
 * 080715	0.5		calcShippingCosts, calcTotalCosts: changed to weight-based calculation.
 * 080715	0.5.1		calcMaterialCosts, calcInstallationCosts: minimum area for which to count is now 30.
 * 080716	0.5.2		calcMaterialCosts, calcInstallationCosts: write sums to hidden input fields.
 */

/* generateMainMenu
 *
 * Generates main menu with class=selected.
 * 
 * Called from: index
 */
function generateMainMenu() {
	document.write("<ul>");
	
	document.write ("<li");
	// 3 = product list, 4 = shopping cart, 5 = order form, 6 = payment confirmation (after order), 8 = order confirmation, 9 = product card
	if (pageid != 3 && pageid != 4 && pageid != 5 && pageid != 6 && pageid != 8 && pageid != 9 && page == "") {document.write (" class=\"sel\"");}
	document.write ("><a href=\"PublishedService?&amp;frontpage=true\" title=\"Etusivu\">Etusivu</a></li>");

	document.write ("<li");			
	if (page == "Itula Avaimet käteen") document.write (" class=\"sel\"");
	document.write ("><a href=\"PublishedService?file=page&amp;pageID=18&amp;freePage=287\" title=\"Itula Avaimet käteen\">Itula Avaimet käteen</a></li>");
	
	document.write ("<li");
	if (page == "Itula Easy") document.write (" class=\"sel\"");
	document.write ("><a href=\"PublishedService?file=page&amp;pageID=18&amp;freePage=288\" title=\"Itula Easy\">Itula Easy</a></li>");
	
	document.write ("<li");
	if (page == "Itula lattiaviilennys") document.write (" class=\"sel\"");
	document.write ("><a href=\"PublishedService?file=page&amp;pageID=18&amp;freePage=299\" title=\"Itula lattiaviilennys\">Itula lattiaviilennys</a></li>");

	document.write ("<li");
	if (page == "Proheat Free") document.write (" class=\"sel\"");
	document.write ("><a href=\"PublishedService?file=page&amp;pageID=18&amp;freePage=289\" title=\"Proheat Free\">Proheat Free</a></li>");
	
	document.write ("<li");
	if (page == "Lisävarusteet") document.write (" class=\"sel\"");
	document.write ("><a href=\"PublishedService?file=page&amp;pageID=18&amp;freePage=291\" title=\"Lisävarusteet\">Lisävarusteet</a></li>");
	
	document.write ("<li");
	if (page == "Yritysasiakkaat") document.write (" class=\"sel\"");
	document.write ("><a href=\"PublishedService?file=page&amp;pageID=18&amp;freePage=290\" title=\"Yritysasiakkaat\">Yritysasiakkaat</a></li>");
	
	document.write("</ul>");
}

/* calcMaterialCosts
 *
 * Calculator for material costs.
 * 
 * Called from: Itula Avaimet käteen rfo form, Itula Easy order form
 */
function calcMaterialCosts() {
	var bet = 18.5;
	var puu = 45.5;
	var sum = 0;
	var qua = 0;
	var area1 = document.phform.area1.value.replace(',', '.');
	var area2 = document.phform.area2.value.replace(',', '.');
	var area3 = document.phform.area3.value.replace(',', '.');
	
	if (area1 >= 0 && area2 >= 0 && area3 >= 0) {
		qua = Number(area1) + Number(area2) + Number(area3);
		
		if (qua >= 30) {
			// Floor 1
			if (document.phform.floor1[0].checked) {
				sum += bet * area1;
			}
			else {
				sum += puu * area1;
			}
			
			// Floor 2
			if (document.phform.floor2[0].checked) {
				sum += bet * area2;
			}
			else {
				sum += puu * area2;
			}
			
			// Floor 3
			if (document.phform.floor3[0].checked) {
				sum += bet * area3;
			}
			else {
				sum += puu * area3;
			}
		}
	}
	
	// Round
	sum = Math.round(sum * 10) / 10;
	
	qua = String(qua);
	sum = String(sum);
	
	// Append with zeros
	if (sum.indexOf('.') < 0) {
		sum += '.00';
	}
	
	if (sum.indexOf('.') == (sum.length - 2)) {
		sum += '0';
	}
	
	// Replace dot
	qua = qua.replace('.', ',');
	sum = sum.replace('.', ',');
	sum += ' €';
	
	// Show totals
	document.phform.material_costs.value = sum;	// hidden field, to be sent to the script
	document.getElementById('total_area').innerHTML = qua;
	document.getElementById('material_costs').innerHTML = sum;
}

/* calcInstallationCosts
 *
 * Calculator for installation costs.
 * 
 * Called from: Itula Avaimet käteen rfo form
 */
function calcInstallationCosts() {
	var sum = 0;
	var qua = 0;
	var area1 = document.phform.area1.value.replace(',', '.');
	var area2 = document.phform.area2.value.replace(',', '.');
	var area3 = document.phform.area3.value.replace(',', '.');
	
	if (area1 >= 0 && area2 >= 0 && area3 >= 0) {
		qua = Number(area1) + Number(area2) + Number(area3);
		
		if (qua >= 30) {
			// Area 1
			if (document.phform.target_location[0].checked) {
				if (qua < 50) {
					sum = 299;
				}
				else if (qua >= 50 && qua < 95) {
					sum = 5 * qua + 49;
				}
				else if (qua >= 95 && qua < 165) {
					sum = 4.8928 * qua + 31.5;
				}
				else if (qua >= 165 && qua < 195) {
					sum = 5 * qua + 29;
				}
				else if (qua >= 195) {
					sum = 5.7 * qua - 125;
				}
			}
			// Area 2
			else {
				sum = 6.9 * qua;
			}
		}
	}
	
	// Round
	sum = Math.round(sum);
	
	sum = String(sum);
	sum += ',00 €';
	
	// Show total
	document.phform.installation_costs.value = sum;	// hidden field, to be sent to the script
	document.getElementById('installation_costs').innerHTML = sum;
}

/* checkForm
 *
 * Check for obligatory fields.
 * 
 * Called from: Itula Avaimet käteen rfo form, Itula Easy order form
 */
function checkForm(form) {
	if (!form.target_street_address.value || !form.target_postal_number.value || !form.target_city.value || !form.target_build_year.value || !form.name.value || !form.street_address.value || !form.post.value || !form.telephone.value || !form.email.value) {
		alert("Pakollisia tietoja puuttuu!");
		return false;
	}
	
	return true;
}

/* createImageLink
 *
 * Shows a product image, with a link to full-size version if needed.
 * 
 * Called from: Product list, product page
 */
function createImageLink(url, pic, default_pic) {
	var img_found	= false;
	var file 		= url.substring(url.lastIndexOf("/")+1, url.length);	// File name with extension
	var file_no_ext	= file.substring(0, file.lastIndexOf("."));				// File name without extension
	
	// If the thumbnail isn't the default picture begin examining full-size image file extension
	if (file_no_ext != "0defaultPictureUpload" && file_no_ext != "1defaultPictureUpload" && file_no_ext != "2defaultPictureUpload" && file_no_ext != "3defaultPictureUpload" && file_no_ext != "4defaultPictureUpload" && file_no_ext != "5defaultPictureUpload") {
		var exts	= new Array("jpg", "gif", "JPG", "GIF", "jpeg", "JPEG", "png", "PNG", "bmp", "BMP");	// Allowed extensions
		var ext		= null;								// Extension currently being examined
		var i		= 0;
	
		do {
			ext = exts[i];	// Pick an extension from the array
			
			// Check if the file exists with this extension
			if (isFile("filemanager/productpics/" + file_no_ext + "." + ext)) {
				img_found = true;	// Variable on a calling page
			}
			
			i++;
		} while (!img_found && i < exts.length);
	
		// If a full-size image with one of the allowed extension was found print starting link tag
		if (img_found) {
			document.write('<a href=\"filemanager/productpics/' + file_no_ext + '.' + ext + '\" rel=\"lightbox\">' + pic + '</a>');
		}
		else {
			document.write(pic);
		}
	}
	// There are no product pictures so show the "No image available" picture if this is the default picture place (on the left)
	else if (default_pic) {
		document.write(pic);
	}
}

/* isFile
 *
 * Checks if a file exists.
 * 
 * Called from: function createImageLink.
 */
function isFile(file) {
	var fileObj = AJ();
	
	if (!fileObj) {
		return false;
	}
	
	try {
		fileObj.open("HEAD", file, false);
		fileObj.send(null);
		return (fileObj.status == 200) ? true : false;
	}
	catch (er) {
		return false;
	}
}

/* AJ
 *
 * Used when checking if a file exists - creates a new XMLHttpRequest or ActiveXObject.
 * 
 * Called from: function isFile.
 */
function AJ() {
	var obj;
	
	if (window.XMLHttpRequest) {
		obj = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try {
			obj = new ActiveXObject('MSXML2.XMLHTTP.3.0');
		}
		catch(er) {
			obj = false;
		}
	}
	
	return obj;
}

/* calcShippingCosts
 *
 * Calculator for shipping costs.
 * 
 * Called from: order process
 */
function calcShippingCosts(order_weight) {
	if (order_weight == 0) {
		document.write("0,00 €");
	}
	else if (order_weight > 0 && order_weight <= 2) {
		document.write("12,90 €");
	}
	else if (order_weight > 2 && order_weight <= 5) {
		document.write("14,40 €");
	}
	else if (order_weight > 5 && order_weight <= 15) {
		document.write("16,90 €");
	}
	else {
		document.write("19,90 €");
	}
}

/* calcTotalCosts
 *
 * Calculator for total costs.
 * 
 * Called from: order process
 */
function calcTotalCosts(order_total, order_weight) {
	var sum = 0;
		
	// Replace comma
	order_total = order_total.replace(",", ".");
	
	// Remove currency symbol
	order_total	= order_total.substring(0, order_total.lastIndexOf(" "));
	
	if (order_weight == 0) {
		sum = order_total;
	}
	else if (order_weight > 0 && order_weight <= 2) {
		sum = Number(order_total) + 12.9;
	}
	else if (order_weight > 2 && order_weight <= 5) {
		sum = Number(order_total) + 14.4;
	}
	else if (order_weight > 5 && order_weight <= 15) {
		sum = Number(order_total) + 16.9;
	}
	else {
		sum = Number(order_total) + 19.9;
	}
	
	sum = Math.round(sum * 100) / 100;
	
	sum = String(sum);
	
	// Append with zeros
	if (sum.indexOf('.') < 0) {
		sum += '.00';
	}
	
	if (sum.indexOf('.') == (sum.length - 2)) {
		sum += '0';
	}

	// Replace dot
	sum = sum.replace('.', ',');
	
	document.write(sum + " €");
}