function goBack(value) {
	window.location = value;
}

function goHistory() {
	window.history.back();
}

function getInt(val) {
	var value = parseInt(val);
	if (isNaN(value)) {
		value = 0;
	}
	return value;
}

function getFloat(val) {
	var value = parseFloat(val);
	if (isNaN(value)) {
		value = 0;
	}
	return value;
}

function chDeci(val, deci) {
     if (val != 0) {
         deci = Math.pow(10, deci);
         val = Math.round(val*deci)/deci;
     }
     return val;
}

function roundMore(val, deci) {
     if (val != 0) {
         deci = Math.pow(10, deci);
         val = getInt(val*deci + 1)/deci;
     }
     return val;
}

function formatOutput(val) {
	if (isNaN(val)) {
		return "";
	}
	if (val == Infinity) {
		return "";
	}
	return val + "";
}

function setSelectedItem(item, value) {
	var len = item.length;
	for (var i=0; i<len; i++) {
		if (item.options[i].value == value) {
			item.selectedIndex = i;
			break;
		}
	}
}

function setCheckedItem(item, value) {
	var len = item.length;
	for (var i=0; i<len; i++) {
		if (item[i].value == value) {
			item[i].checked = true;
			break;
		}
	}
}

function photoSize() {
	var max = 250;
	var width = document.photo.width;
	var height = document.photo.height;
	var percent = 1;
	
	if (width > height) {
		if (width > max) {
			percent = max/width;
			document.photo.width = max;
			document.photo.height = height * percent;
		}
	}
	else {
		if (height > max) {
			percent = max/height;
			document.photo.width = width * percent;
			document.photo.height = max;
		}
	}
}

function viewPhoto(image) {
	var url = "../product/viewPhoto.jsp?image=" + image;
	window.open(url,'PHOTO','scrollbars=1,resizable=1');
}

function viewItem(itemNo) {
	var url = "../product/view.jsp?itemNo=" + itemNo;
	window.open(url,'VIEW_PRODUCT','scrollbars=1,resizable=1');
}

function viewPD(pdCode) {
	var url = "../prodDev/view.jsp?pdCode=" + pdCode;
	window.open(url,'VIEW_PROD_DEV','scrollbars=1,resizable=1');
}

function viewQuotation(ref) {
	var url = "../quotation/view.jsp?quoteRef=" + ref;
	window.open(url,'VIEW_QUOTE','scrollbars=1,resizable=1');
}

function viewSpecialItem(itemNo) {
	var url = "../productSpecial/view.jsp?itemNo=" + itemNo;
	window.open(url,'VIEW_PRODUCT','scrollbars=1,resizable=1');
}


function viewSupplier(supplierId) {
	var url = "../supplier/view.jsp?suppId=" + supplierId;
	window.open(url,'VIEW_SUPPLIER','scrollbars=1,resizable=1');
}

function viewInsReport(insNo) {
	var url = "../insReport/view.jsp?insNo=" + insNo;
	window.open(url,'VIEW_REP','scrollbars=1,resizable=1');
}


function attachment(dir, url) {
	var nextUrl = "../attachment/list.jsp?dir=" + dir + "&url=" + url;
	window.open(nextUrl,'ATTACHMENT','scrollbars=1,resizable=1');
}

function viewFile(url, file) {
	var nextUrl = url + "/" + file;
	window.open(nextUrl,'VIEW_ATTACHMENT','scrollbars=1,resizable=1');
}

function searchKey() {
   	if(document.form1.keyword.value == "") {
   		alert("Please enter a keyword!");
   		return;
   	}
	document.form1.submit();
}

function searchKeyword() {
   	if(document.form1.keyword.value == "") {
   		alert("Please enter a keyword!");
   		return;
   	}
	document.form1.action = "searchByKeyword.jsp";
	document.form1.submit();
}

function searchUrl() {
	document.form1.action = "searchByKeyword.jsp";
	document.form1.submit();
}

function sortBy(sort) {
	document.form1.sortBy.value = sort;

	document.form1.submit();
}

function goNextPage(pageNo) {
	document.form1.pageNo.value = pageNo;
	document.form1.action  = document.form1.listPage.value;
	document.form1.submit();
}

function checkKey() {
	key=window.event.keyCode; 
	if(key==0xD) {
		return searchKey();
	}
	return;
}

function checkCountry() {
	if (document.form1.country.value == '' || document.form1.country.value == 'N/A') {
		alert("Please choose country!");
		return false;
	}
	return true;
}

function checkChinese(str) { 
	var reg = /^[0-9a-z &*$#@~()A-Z]+$/i; 
	return reg.test(str);
} 

function hasChinese(str) {
	if (/[^\x00-\xff]/g.test(str))
		return true;
	else
		return false;
} 

function checkDigits(str) { 
	var strTemp="0123456789"; 
	var j = 0;
	
	if (str.length == 0) 
		return true;
 
	for (var i=0;i<str.length;i++) { 
		j = strTemp.indexOf(str.charAt(i)); 
		if (j == -1) 
			return false;
	} 
	
	return true;
} 

function addButton(value, click) {
	document.write("<input type=button name=b1 value=\"" + value + "\" onClick=\"" + click + "\" style=\"height:22px;border-left:1 ridge #ffffff;border-top:1 ridge #ffffff;border-bottom:1;border-right:1;border-style:ridge;FONT-SIZE: 12px\">");
}

function addButton2(name, value, click) {
	document.write("<input type=button name=\"" + name + "\" + value=\"" + value + "\" onClick=\"" + click + "\" style=\"height:22px;border-left:1 ridge #ffffff;border-top:1 ridge #ffffff;border-bottom:1;border-right:1;border-style:ridge;FONT-SIZE: 12px\">");
}

function setBgColor(id, color) {
	var element = document.getElementById(id);
	element.style.backgroundColor = color;
}

function setColor(id, color) {
	var element = document.getElementById(id);
	element.style.color = color;
}

function setReadOnly(id, tf) {
	var element = document.getElementById(id);
	element.readOnly = tf;
}

function grayId(id) {
	setBgColor(id, "#DCDCDC");
	setColor(id,"gray");
	setReadOnly(id, true);
}

function whiteId(id) {
	setBgColor(id, "");
	setColor(id,"");
	setReadOnly(id, false);
}

function displayId(id) {
	var element = document.getElementById(id);
	element.style.display = "";
}

function hideId(id) {
	var element = document.getElementById(id);
	element.style.display = "none";
}

