function submitForm(who) {
	var error = false;
	var myForm = document.getElementById(who);
	
	if (who == 'newsletterSubmitForm') {
		if (((myForm.first_name.value == "") || (myForm.first_name.value == "First Name")) && !error) {
			error = true;
			myForm.first_name.focus();
			alert("Please input your First Name!");
		}
		if (((myForm.last_name.value == "")  || (myForm.first_name.value == "Last Name")) && !error) {
			error = true;
			myForm.last_name.focus();
			alert("Please input your Last Name!");
		}
	} else {
		if ((myForm.name.value == "") && !error) {
			error = true;
			myForm.name.focus();
			alert("Please input your Name!");
		}
		if ((myForm.company.value == "") && !error) {
			error = true;
			myForm.company.focus();
			alert("Please input your Company!");
		}
		if ((myForm.request.value == "") && !error) {
			error = true;
			myForm.request.focus();
			alert("Please input your Request!");
		}
	}
	if (!verifyEmail(myForm.email.value) && !error) {
		error = true;
		myForm.email.focus();
		alert("Please input your vaild Email!");
	}
	
	if (!error) {
		myForm.submit();
	}
}

function verifyEmail(address) {
	if (trim(address) != "") {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return reg.test(address);
	} else {
		return false;
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// POP DIV Function
function getPageSize() {
	var d = document;
	var w = window;
	var iebody = (d.compatMode && d.compatMode != 'BackCompat') ? d.documentElement : d.body;	
	var b = d.body;

	var xScroll = (w.innerWidth && w.scrollMaxX) ? w.innerWidth + w.scrollMaxX : Math.max(b.scrollWidth, b.offsetWidth),
		yScroll = (w.innerHeight && w.scrollMaxY) ? w.innerHeight + w.scrollMaxY : Math.max(b.scrollHeight, b.offsetHeight),
		pageWidth = (document.all && !window.opera) ? iebody.scrollWidth : (d.documentElement.clientWidth || self.innerWidth),
      	pageHeight = (document.all && !window.opera) ? Math.max(iebody.scrollHeight, iebody.clientHeight) : (d.documentElement.clientHeight || self.innerHeight);

	var width = (document.all && !window.opera) ? iebody.clientWidth : (d.documentElement.clientWidth || self.innerWidth),
		height = (document.all && !window.opera) ? iebody.clientHeight : self.innerHeight;
			
	return {
		pageWidth: Math.min(pageWidth, xScroll),
		pageHeight: Math.max(pageHeight, yScroll),
		width: width,
		height: height,		
		scrollLeft: (document.all && !window.opera) ? iebody.scrollLeft : pageXOffset,
		scrollTop: (document.all && !window.opera) ? iebody.scrollTop : pageYOffset
	}
}

function addEventListener(el, event, func) {
	try {
		el.addEventListener(event, func, false);
	} catch (e) {
		try {
			el.detachEvent('on'+ event, func);
			el.attachEvent('on'+ event, func);
		} catch (e) {
			el['on'+ event] = func;
		}
	} 
}

function hideDiv(who) {
	if (who) {
		document.getElementById(who).style.display = 'none';
	}
}

function showDiv(who) {
	if (who) {
		document.getElementById(who).style.display = 'block';
	}
}

function setOverlaySize() {
	var page = getPageSize();
	document.getElementById('overlay').style.width = page.pageWidth + 'px';
	document.getElementById('overlay').style.height = page.pageHeight + 'px';
}

function openOverlay() {
	showDiv('overlay');
}

function closeOverlay() {
	hideDiv('overlay');
}

function setDivPos(who) {
	document.getElementById('overlay').style.width = 1;
	document.getElementById('overlay').style.height = 1;

	var page = getPageSize();
	if (document.getElementById(who)) {
		document.getElementById(who).style.left = (page.pageWidth / 2) - (parseInt(document.getElementById(who).style.width) / 2) + 'px';
		document.getElementById(who).style.top = page.scrollTop + 20 + 'px';
	}
	setOverlaySize();	
}

function openPopUpDiv(who, type) {
	document.getElementById('popup_area_content').innerHTML = '';
	if (!type) {
		type = 'product';
	}
	openOverlay();
	setDivPos(who);
	showDiv(who)
	setDivPos(who);
	
	var page = getPageSize();
	if (type == "product") {
		if (page.scrollTop < 400) {
			document.getElementById(who).style.top = '400px';
		} else {
			document.getElementById(who).style.top = page.scrollTop + 20 + 'px';
		}
	} else {
		if (page.scrollTop < 600) {
			document.getElementById(who).style.top = '600px';
		} else {
			document.getElementById(who).style.top = page.scrollTop + 20 + 'px';
		}
	}
	window.onresize = function () {
		var page = getPageSize();
		var to_move = (page.pageWidth / 2) - (parseInt(document.getElementById(who).style.width) / 2);
		if (to_move <= 187) {
			document.getElementById(who).style.left = '187px';
		} else {
			document.getElementById(who).style.left = to_move + 'px';
		}
		setOverlaySize();	
	};
}

function closePopUpDiv(who) {
	closeOverlay();
	hideDiv(who)
}
