var itIsRetail = false;
var reNum = /^[0-9]*$/i; // Regex number only
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function isLegal(str, pattern) {
	// Returns TRUE if str match the given pattern
	var re = pattern;
	return re.test(str);
}
function isEmail(str)
{
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var reEmail =  /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return reEmail.test(str);
}

function checkinput(form)
{
	if(trim(form.title.value).length==0) {
		hboxAlert(form.title);
		return false ;
	}
	if(trim(form.contact_name_s.value).length==0) {
		// alert("Please select your contact name.");
		// form.contact_name_s.focus();
		hboxAlert(form.contact_name_s);
		return false ;
	}
	if(trim(form.contact_name_g.value).length==0) {
		// lert("Please insert your contact name.");
		// form.contact_name_g.focus();
		hboxAlert(form.contact_name_g);
		return false ;
	}
	if(trim(form.com_name.value).length==0) {
		hboxAlert(form.com_name);
		return false ;
	}
	if(trim(form.business.value).length==0) {
		hboxAlert(form.business);
		return false ;
	}
	if(trim(form.job.value).length==0) {
		hboxAlert(form.job);
		return false ;
	}
	if(trim(form.address1.value).length==0) {
		hboxAlert(form.address1);
		return false ;
	}
	if(form.country.value.length==0) {
		hboxAlert(form.country);
		return false ;
	}
	if(trim(form.tel_c.value).length==0) {
		// alert("Please input the country code of your company telephone no.");
		// form.tel_c.focus();
		hboxAlert(form.tel_c);
		return false;
	} else if (!isLegal(trim(form.tel_c.value), reNum)) {
		hboxAlert(form.tel_c);
		return false;
	}
	if  (trim(form.tel_a.value).length>0 && !isLegal(trim(form.tel_a.value), reNum)) {
		hboxAlert(form.tel_a);
		return false;
	}
	if(trim(form.tel.value).length==0) {
		// alert("Please input your company telephone no.");
		// form.tel.focus();
		hboxAlert(form.tel);
		return false ;
	} else if (!isLegal(trim(form.tel.value), reNum)) {
		hboxAlert(form.tel);
		return false;
	}
	if(trim(form.fax_c.value).length==0) {
		hboxAlert(form.fax_c);
		return false;
	} else if (!isLegal(trim(form.fax_c.value), reNum)) {
		hboxAlert(form.fax_c);
		return false;
	}
	if  (trim(form.fax_a.value).length>0 && !isLegal(trim(form.fax_a.value), reNum)) {
		hboxAlert(form.fax_a);
		return false;
	}
	if(trim(form.fax.value).length==0) {
		hboxAlert(form.fax);
		return false;
	} else if (!isLegal(trim(form.fax.value), reNum)) {
		hboxAlert(form.fax);
		return false;
	}
	if(form.email.value.length==0) {
		// alert("Please insert your email address.");
		// form.email.focus();
		hboxAlert(form.email);
		return false;
	}else{
		if (!isEmail(trim(form.email.value))) {
			// alert("Your email address format is not correct.");
			// form.email.focus();
			hboxAlert(form.email);
			return false;
		}
	}
	form.submit();
}
function clickOthersInBusinessNature(obj) {
	// return true or false if user click "others" in Business Nature
	if (obj) {
		for (var i=0;i<obj.length;i++) {
			if (obj[i].value=="1OT" && obj[i].checked) {
				return true;
			}
		}
	}
	return false;
}

function validateCheckboxs(obj) {
	// validate checkboxs
	var mycount1 = 0;
	for (var i=0; i<obj.length; i++)
	{
		temp=obj[i].checked;
		if (temp==1) {
			mycount1++;
			break;
		}
	}
	if (mycount1 <= 0) {
		if (obj.length>0)
			hboxAlert(obj[0]);
		return false;
	}
	return true;
}
function validateAnsweredShopNo(obj) {	
	// validate if user is retailer, then did they answer another question
	var noOfShopsValue = new Array("RE1", "RE2");
	var isTrue = false;
	for (var i=0; i<obj.length; i++)
	{
		for (var j=0; j<noOfShopsValue.length; j++) {
			if (obj[i].checked && obj[i].value==noOfShopsValue[j]) { 
				isTrue = true;
				break;
			}
		}
		if (isTrue)
			break;
	}
	return isTrue;
}
function validateIntCountry(obj) {	
	// validate checkboxs (int_country)
	var sf = 0;
	var st = 0;
	var temp = "";
	for (var i=0; i<obj.length; i++)
	{
	 	if (obj[i].checked)
			temp=obj[i].value;
		if (temp.indexOf("SF-")>=0) {
			sf = 1;
		} else if (temp.indexOf("ST-")>=0) {
			st= 1;
		}
	}
	if (sf==1 && st==1)
		return true;
	if (sf==0)
		hboxAlert(obj[0]);
	else if (st==0)
		hboxAlert(obj[6]);
	return false;
}
function selectToDisplay(obj, target, val) { 
	// This function is applicable for checkbox or radio button ONLY
	if (document.getElementById(target)) {
		if (obj.value == val && obj.checked) {
			document.getElementById(target).style.display = "inline";
		//	document.getElementById(target).style.visibility = "visible";
			obj.form[target].focus();
		}
		else
		//	document.getElementById(target).style.visibility = "hidden";
			document.getElementById(target).style.display = "none";
	}
}
function showPrice(obj) {
	// show the radio buttons of price price/pc after user click the checkboxs of product(FA)
	var divName = obj.value;
	if (divName!="" && document.getElementById(divName)) {
		if (obj.checked) {
			document.getElementById(divName).style.visibility = "visible";
			getBoxValue(obj);
		} else {
			document.getElementById(divName).style.visibility = "hidden";
			clearBoxValue(obj);
		}
	}
}
function getBoxValue(obj) {
	// retrieve the value from the highlighted radio button and fill it in the hidden box
	// please be reminded that 
	// the checkbox name is "pdt_fa_"CODE (e.g. pdt_fa_BX)
	// and the radio button name is CODE (e.g. BX)
	var objParent = obj.parentNode;
	var objHiddenBox = getInputBoxByName("pdt_fa", objParent.id);
	var objForm = obj.form;
	var radioChecked; // if user did not click any radio button, then fill in the checkbox value
	if (obj.name!="" && objForm && objHiddenBox) {
		var objRadio = obj.name.replace('pdt_fa_','');
		if (objRadio!="") {
			var objRadioList = eval("objForm."+objRadio);
			for (var i=0;i<objRadioList.length;i++) {
				if (objRadioList[i].checked) {
					objHiddenBox.value = objRadioList[i].value;
					radioChecked = true;
					break;
				}
			}
			if (!radioChecked)
				objHiddenBox.value = obj.value;
		}
	}
}
function clearBoxValue(obj) {
	// clear the hidden box value named "pdt_fa" in product (FA)
	var parent = obj.parentNode;
	var hiddenBox = getInputBoxByName("pdt_fa", parent.id);
	if (hiddenBox)
		hiddenBox.value = "";
}
function getInputBoxByName(targetName, areaId) {
	// get and return the specified target input box by name within specific node id
	if (targetName!="" && document.getElementById(areaId))
	{
		var o = document.getElementById(areaId);
		for (var i=0;i<o.childNodes.length;i++) {
			if (o.childNodes[i].name==targetName) {
				// only execute when hiddenBox was found
				return o.childNodes[i];
				break;
			}
		}
	}
	return false;
}
function organizeVal(obj, div_id) {
	// organize the product(FA) value to product code:product price (e.g.BI:1-10)
	// and fill this value into relevant hidden box named "pdt_fa"
	if (obj && obj.value!="" && document.getElementById(div_id)) {
		var o = getInputBoxByName("pdt_fa", div_id);
		if (o)
			o.value = obj.value;
	}
}
function resetForm(obj) {
	// reset form warning
	if (confirm("All data will be lost, are you sure?"))
		if (obj)
			obj.reset();
}
function showOthers(selectObj, boxId) {
	// show textbox when user select others for job position
	var obj = document.getElementById(boxId);
	var inputbox = eval("selectObj.form."+boxId);
	if (obj) {
		if (selectObj.value=="TH") {
			obj.className = "position_others_on";
			if (inputbox)
				inputbox.focus();
		}
		else
			obj.className = "position_others_off";
	}
}
// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

var retailCode=new Array("RB", "RS", "RG", "RZ", "RGT", "RV", "RX", "RU", "RY", "RSV", "RSG", "RW");
function isRetail(obj) {
	if (obj) {
		var isRetail_local = false;
		var f = obj.form;
		var n = obj.name;
		var nodelist = f[n];
		if (nodelist.length>0) {
			for (var i=0;i<nodelist.length;i++) {
				for (var j=0;j<retailCode.length;j++) {
					if (nodelist[i].checked && nodelist[i].value==retailCode[j]) {
						// is retail
						isRetail_local = true;
						break;
					}
				}
				if (isRetail_local)
					break;
			}
			showRetailStar( f, isRetail_local );
		}
	}
}
function showRetailStar(form, show) {
	var obj = document.getElementById("retailQ");
	if (obj) {
		if (show) {
			obj.className = "isRetailOn";
			itIsRetail = true;
		}
		else {
			obj.className = "hide";
			itIsRetail = false;
		}
	}
}
function printpage() {
	window.print();
}
var acceptTextAreaValue;
function maxTextAreaLength(obj, name, maxLength) {
	if (obj) {
		var numObj; // the form object showing the num of char left
		numObj = eval("obj.form."+name);
		if (obj.value!=null && obj.value.length>0) {
			if (obj.value.length>255) {
				// exceed the limit
				obj.value = acceptTextAreaValue;
			} else {
				acceptTextAreaValue = obj.value;
				numObj.value = 255-obj.value.length;
			}
		};
	}
}