window.onload = initPlate;

function initPlate() {
	processReg("front", "white");
	processReg("rear", "white");
	//new Draggable("frontplackets");
	//new Draggable("frontslats");
	//new Draggable("rearplackets");
	//new Draggable("rearslats");
}
function toggleCommentsBox() {
	if ($("comments_check").checked == true) {
		if ($("comments_text").style.display == "none") {
			Effect.BlindDown("comments_text");
		}
	} else {
		if ($("comments_text").style.display != "none") {
			Effect.BlindUp("comments_text");
		}
	}
}
function clearFields() {
	if ($("clearfield").value == "no") {
		$("frontnumber").value = "";
		$("rearnumber").value = "";
		$("clearfield").value = "yes"
		updateReg();
	}
}
function badge() {
	var badge = $("badges").value;
	switch (badge) {
		case "None":
			if ($("frontbadge").style.display != "none") {
				Effect.Fade("frontbadge");
				Effect.Fade("rearbadge");
				$("foblong").style.width = "520px";
				$("foblong").style.left = "10px";
				$("roblong").style.width = "520px";
				$("roblong").style.left = "10px";
			}
			break;
		default:
			$("fbadge").src = "../images/" + badge.toLowerCase() + "_badge.gif";
			$("rbadge").src = "../images/" + badge.toLowerCase() + "_badge.gif";
			if ($("frontbadge").style.display == "none") {
				Effect.Appear("frontbadge");
				Effect.Appear("rearbadge");
				$("foblong").style.width = "460px";
				$("foblong").style.left = "60px";
				$("roblong").style.width = "460px";
				$("roblong").style.left = "60px";
			}
	}
	if ((badge == 'IRL') || (badge == 'B') || (badge == 'E')) {
		$("rcolor-white").checked = true;
		if ($("country-color").style.display != "none") {
			new Effect.Fade("country-color");
		}
		$("frontplate").style.backgroundImage = "url(../images/self.jpg)";
	} else if (badge == 'NL') {
		$("rcolor-yellow").checked = true;
		if ($("country-color").style.display != "none") {
			new Effect.Fade("country-color");
		}
		$("frontplate").style.backgroundImage = "url(../images/rearself.jpg)";
	} else {
		if ($("country-color").style.display == "none") {
			new Effect.Appear("country-color");
		}
		$("frontplate").style.backgroundImage = "url(../images/self.jpg)";
	}
	irishTowns();
	updateReg();
}
function updateReg() {
	if ($("badges").value == 'NL') {
		processReg("front", "yellow");
	} else {
		processReg("front", "white");
	}
	if ($("rcolor-white").checked == true) {
		$("rearplate").style.backgroundImage = "url(../images/self.jpg)";
		processReg("rear", "white");
	} else if ($("rcolor-yellow").checked == true) {
		$("rearplate").style.backgroundImage = "url(../images/rearself.jpg)";
		processReg("rear", "yellow");
	}
}
function processReg(position, colour) {
	if ($("autoreg").value == "yes") {
		$("rearnumber").value = $("frontnumber").value;
	}
	var reg = $(position + "number").value.toLowerCase();
	for (var i=0; i<(reg.length); i++) {
		c = reg.charAt(i);
		if (position == "front") {
			$("fimg" + (i+1)).setAttribute("src", displayReg(c, colour));
		} else if (position == "rear") {
			$("rimg" + (i+1)).setAttribute("src", displayReg(c, colour));
		}
	}			
	for (var i=(reg.length); i<9; i++) {
		if (position == "front") {
			$("fimg" + (i+1)).setAttribute("src", "http://www.craigsplates.co.uk/order/images/1by74spacer.gif");
		} else if (position == "rear") {
			$("rimg" + (i+1)).setAttribute("src", "http://www.craigsplates.co.uk/order/images/1by74spacer.gif");
		}
	}
}
function displayReg(reg, colour) {
	if (reg == " ") {
		regsrc = "http://www.craigsplates.co.uk/order/fonts/space.gif";
	} else {
		if (colour == "white") {
			if (reg == "ä") {
				regsrc = "/fonts/umlauts/a_white.gif";
			} else if (reg == "ö") {
				regsrc = "/fonts/umlauts/o_white.gif";
			} else if (reg == "ü") {
				regsrc = "/fonts/umlauts/u_white.gif";
			} else {
				regsrc = "http://www.german-number-plates.com/fonts/german_white/" + reg + ".gif";
			}
		} else if (colour == "yellow") {
			if (reg == "ä") {
				regsrc = "/fonts/umlauts/a_yellow.gif";
			} else if (reg == "ö") {
				regsrc = "/fonts/umlauts/o_yellow.gif";
			} else if (reg == "ü") {
				regsrc = "/fonts/umlauts/u_yellow.gif";
			} else {
				regsrc = "http://www.german-number-plates.com/fonts/german_yellow/" + reg + ".gif";
			}
		}
	}
	return regsrc;
}
function fontColour() {
	var font = $("font").value;
	switch (font) {
		case "Black" :
			$("redux").style.backgroundColor = "black";
			break;
		case "Green" :
			$("redux").style.backgroundColor = "green";
			break;
		case "Silver" :
			$("redux").style.backgroundColor = "silver";
			break;
	}
}
function togglePlateDetail(plate) {
	var detailDiv = $("plate-details" + plate);
	var detailAnchor = $("detail-anchor" + plate);
	if (detailDiv.style.display == "none") {
		Effect.Grow(detailDiv);
		detailAnchor.innerHTML = "Hide Details"; 
	} else {
		Effect.Shrink(detailDiv);
		detailAnchor.innerHTML = "Show Details"; 
	}
}
function checkPosition() {
	var position = $("position").value;
	var oblong_single = $("oblong_single").value;
	var oblong_pair = $("oblong_pair").value;
	var square_single = $("square_single").value;
	var square_pair = $("square_pair").value;
	var county_name = $("county_name").value;
	switch (position) {
		case "front" :
			if ($("rearplate").style.display != "none") {
				Effect.Fade("rearplate");
			}
			if ($("rearplate").style.display != "none") {
				Effect.Fade("rearcontrol");
			}
			if ($("frontplate").style.display == "none") {
				Effect.Appear("frontplate");
			}
			if ($("frontplate").style.display == "none") {
				Effect.Appear("frontcontrol");
			}			
			$("amount").innerHTML = convertPrice(oblong_single).toFixed(2);
			if (($("irltowns").value != "") && ($("badges").value == "IRL")) {
				var irlAmount = parseFloat(convertPrice(oblong_single).toFixed(2)) + parseFloat(convertPrice(county_name).toFixed(2));
				$("amount").innerHTML = convertPrice(irlAmount).toFixed(2);
			}
			break;
		case "rear" :
			if ($("rearplate").style.display == "none") {
				Effect.Appear("rearplate");
			}
			if ($("rearplate").style.display == "none") {
				Effect.Appear("rearcontrol");
			}
			if ($("frontplate").style.display != "none") {
				Effect.Fade("frontplate");
			}
			if ($("frontplate").style.display != "none") {
				Effect.Fade("frontcontrol");
			}
			$("amount").innerHTML = convertPrice(oblong_single).toFixed(2);
			if (($("irltowns").value != "") && ($("badges").value == "IRL")) {
				var irlAmount = parseFloat(convertPrice(oblong_single).toFixed(2)) + parseFloat(convertPrice(county_name).toFixed(2));
				$("amount").innerHTML = convertPrice(irlAmount).toFixed(2);
			}
			break;
		case "pair" :
			if ($("rearplate").style.display == "none") {
				Effect.Appear("rearplate");
			}
			if ($("rearplate").style.display == "none") {
				Effect.Appear("rearcontrol");
			}
			if ($("frontplate").style.display == "none") {
				Effect.Appear("frontplate");
			}
			if ($("frontplate").style.display == "none") {
				Effect.Appear("frontcontrol");
			}
			$("amount").innerHTML = convertPrice(oblong_pair).toFixed(2);
			if (($("irltowns").value != "") && ($("badges").value == "IRL")) {
				var irlAmount = parseFloat(convertPrice(oblong_pair).toFixed(2)) + parseFloat((convertPrice(county_name).toFixed(2)) * 2);
				$("amount").innerHTML = convertPrice(irlAmount).toFixed(2);
			}
			break;
	}
}
function validateReg() {
	if ((($("frontnumber").value == "MY REG") || ($("frontnumber").value == "")) && (($("rearnumber").value == "MY REG") || ($("rearnumber").value == ""))) {
		alert("You forgot to type in your registration number.");
		return false;
	}
	if (($("frontnumber").value.indexOf("-") != -1) || ($("rearnumber").value.indexOf("-") != -1)) {
		//alert("We do not currently provide plates with hyphens (-) in the registration number.");
		//return false;
	}
	return true;
}
function convertPrice(price) {
	var currency = $("currency-rate").value;
	return (parseFloat(currency) * price); 
}
function irishTowns() {
	if ($("badges").value == "IRL") {
		new Effect.Appear("irl-towns");
		var town = $("irltowns").value;
		$("front-irishtext").innerHTML = town;
		$("rear-irishtext").innerHTML = town;
	} else {
		new Effect.Fade("irl-towns");
	}
	if (($("badges").value != "IRL") || ($("irltowns").value == "")) {
		$("foblong").style.top = "28px";
		$("roblong").style.top = "28px";
		$("front-irishtext").innerHTML = "";
		$("rear-irishtext").innerHTML = "";
	} else {
		$("foblong").style.top = "32px";
		$("roblong").style.top = "32px";
	}
	checkPosition();
}
// modded from http://www.alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript/
function insertAtCursor(myField, myValue) {

	// reached patience limit with Opera selection handling..
	is_opera = (navigator.userAgent.indexOf('Opera') != -1);
	
	if (is_opera) {
		myField.focus();
		myField.value += myValue;
	}

	// IE
	// adds to the beginning if the field is not already selected >:-(
	else if (document.selection) {
		
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}

	// moz support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
	} else {
		myField.value += myValue;
	}
	updateReg();
}
