﻿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,location=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 = " 未有填寫";
var sWarning = "Please provide a valid email or phone number";
var sWarningC = "必需填寫電話或有效電郵";
var frm = document.enqform;

	if (slang == "c") {
		sMissing = sCMissing;
      sWarning = sWarningC;
	}

	var contentText = "";
	var itemValue = "";

//	alert(itemObjs.length);
   var itemCount = itemObjs.length;
	for (var i=0; i<itemCount; i++) {
	var itemDisplayText = "";
	
		switch (itemObjs[i].ct) {
			case "tb":
				itemValue = eval("frm." + itemObjs[i].id + ".value");
				break;
				
			case "rb":
				for (var j=0; j < eval("frm." + itemObjs[i].id + ".length"); j++) {
					if (eval("frm." + itemObjs[i].id + "[" + j + "].checked")) {
						itemValue = eval("frm." + itemObjs[i].id + "[" + j + "].value");
					}
				}
				break;
				
			case "cb":
				if (eval("frm." + itemObjs[i].id + ".checked")) {
					itemValue = "Yes";
				}
				else {
					itemValue = "Not specified.";
				}
				break;

			case "sl":
			   var e = eval("frm." + itemObjs[i].id);
			   itemValue = e.options[e.selectedIndex].text;
			   break;
		}
		
		//alert(itemObjs[i].id);
		//alert(itemObjs[i].id + " : " + 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("frm." + itemObjs[i].id + ".focus();");
					contentText = "";
					return contentText;
				}
			}
		}

		itemDisplayText = itemObjs[i].dt + ":\n" + itemValue + "\n\n";
		//alert("Current Item: " + itemDisplayText);
		contentText += itemDisplayText;
		//alert(contentText);
		itemValue = "";
	}

   //either email or phone must not be empty
   var sEmail = frm.email.value;
   var sPhone = frm.phone.value;
   var sReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
/*
   if (sEmail == "" && frm.cbemail.checked) {
      alert(sWarning);
      contentText = "";
   }

   if (sPhone == "" && frm.cbphone.checked) {
      alert(sWarning);
      contentText = "";
   }
*/
   if (sPhone == "" && !sReg.test(sEmail)) {
      alert(sWarning);
      contentText = "";
  	}

	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 = "確定內容無誤？";
var frm = document.enqform;

	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		
		frm.contenttext.value = processResult;
		frm.subject.value = sSubject;
		frm.action = submitURL;
		frm.target = "lphpopup";

		if (confirm(sConfirm + "\n" + frm.contenttext.value)) {
			popUp("blank.html");
			//alert("test ok!");
			frm.submit();
		}

	}
}

