﻿function openNewWindow(URLtoOpen, windowName, windowFeatures) {
	
newWindow=window.open(URLtoOpen, windowName, windowFeatures); 
}

function popUp(url) {
	openNewWindow(url,"lphpopup", "width=300,height=200,status=no,toolbar=no,menubar=no");
}

function ParseItems(itemRaw) {
var itemObjs = new Array();

	for (var i=0; i<itemRaw.length; i+=4) {
		var itemObj = new Object();
		itemObj.dt = itemRaw[i];
		itemObj.id = itemRaw[i+1];
		itemObj.ct = itemRaw[i+2];
		itemObj.rq = itemRaw[i+3];
		itemObjs.push(itemObj);
	}
//	alert(itemObjs[0].dt);
	return(itemObjs);
}

function ProcessInputs(itemObjs, slang) {
var sMissing = " is missing";
var sCMissing = " 未有填寫";

	if (slang == "c") {
		sMissing = sCMissing;
	}

	var contentText = "";
	var itemValue = "";

	//alert(itemObjs.length);	
	for (var i=0; i<itemObjs.length; i++) {
	var itemDisplayText = "";
	
		switch (itemObjs[i].ct) {
			case "tb":
				itemValue = eval("document.enqform." + itemObjs[i].id + ".value");
				break;
				
			case "rb":
				for (var j=0; j < eval("document.enqform." + itemObjs[i].id + ".length"); j++) {
					if (eval("document.enqform." + itemObjs[i].id + "[" + j + "].checked")) {
						itemValue = eval("document.enqform." + itemObjs[i].id + "[" + j + "].value");
					}
				}
				break;
				
			case "cb":
				if (eval("document.enqform." + itemObjs[i].id + ".checked")) {
					itemValue = "Yes";
				}
				else {
					itemValue = "Not specified.";
				}
				break;
		}
		
		//alert(itemObjs[i].id);
		//alert(itemValue);
		
		if (itemObjs[i].rq == true) {
			if (itemValue == "" || itemValue == null) {
				if (itemObjs[i].ct != "rb") alert("(" + itemObjs[i].dt + ")" + sMissing);
				if (itemObjs[i].ct == "tb") {
					eval("document.enqform." + itemObjs[i].id + ".focus();");
					contentText = "";
					return contentText;
				}
			}
		}
		
		itemDisplayText = itemObjs[i].dt + ":\n" + itemValue + "\n\n";
		//alert("Current Item: " + itemDisplayText);
		contentText += itemDisplayText;
		//alert(contentText);
		itemValue = "";
	}
	
	return contentText;
}

function SubmitEnquiries(submitURL, aitems, slang) {
var sSubject = "Piano House Web Enquiries";
var sCSubject = "Piano House Web 查詢";
var sConfirm = "Is the following information correct?";
var sCConfirm = "確定內容無誤？";

	if (slang == "c") {
		sConfirm = sCConfirm;
		sSubject = sCSubject;
	}

	var processResult = ProcessInputs(aitems, slang);
	
	if (processResult != "") {

		//safety
		if (submitURL == "" || submitURL == null) submitURL = "submitenquries.aspx"

		//set email subject & contents		
		document.enqform.contenttext.value = processResult;
		document.enqform.subject.value = sSubject;
		document.enqform.action = submitURL;
		//document.enqform.target = "lphpopup";

		if (confirm(sConfirm + "\n" + document.enqform.contenttext.value)) {
			//popUp("blank.html");
			document.enqform.submit();
		}

	}
}
