String.prototype.trim = function() {
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
Array.prototype.merge = function(arr){
	for(i=0;i<arr.length;i++)
		this[this.length] = arr[i];
	return this;
}
Array.prototype.removeIndex = function(index){
	newvalues = [];
	for(i=0;i<this.length;i++){
		if(i != index)
			newvalues[newvalues.length] = this[i];
	}
	return newvalues;
}
function fillLanguage(id,lang,value){
    var $obj = MM_findObj(id);
    if($obj != null){
        $obj.value = replaceLanguageValue(lang,$obj.value);
    }
}
function replaceLangaugeValue(lang,source,replacestr){
    var exp = "\[" + lang + "\](.*)\[/" + lang + "\]";
    var reg = new RegExp(exp,"g");
    source = source.replace(reg,"[" + lang + "]" + replacestr + "[/]" + lang + "]");
    return source;
}
function opendetail(item_id,shop_id,lang){
	var page = "itemDetail.php?lang="+lang+"&item_id="+item_id+"&shop_id="+shop_id;
	window.open(page,'_blank','manubar=no,location=no,width=600,height=400,left=200px,top=200px');
}
function openfullwindow(url){
	width = 800;
	height = 600;
	window.open(url,'_blank','menubar=yes,resizable=yes,scrollbars=yes,location=yes,toolbar=yes,status=yes,titlebar=yes,width='+width+',height='+height+',left='+(800-width)/2+'px,top='+(600-height)/2+'px');
}
function openwindow(url){
	openwindow(url,600,400);
}
function openfullwindow(url){
	width=screen.width;
	height=screen.height;
	window.open(url,'_blank','fullscreen=true,menubar=yes,toolbar=yes,titlebar=yes,resizable=yes,scrollbars=yes,location=yes');
}
function openwindow(url,width,height){
	window.open(url,'_blank','menubar=no,resizable=yes,scrollbars=yes,location=no,width='+width+',height='+height+',left='+(800-width)/2+'px,top='+(600-height)/2+'px');
}
function openwindowfrompopup(url){
	openwindowfrompopup(url,800,600);
}
function openwindowfrompopup(url,width,height){
	window.opener.open(url,'_blank');	
}
function del(){
	return confirm('ยืนยันการลบรายการที่เลือก?');
}
function getObject(id){
	return document.getElementById(id);
}
function toggle(id){
	var obj = getObject(id);
	obj.style.display = (obj.style.display == ''? 'none':'');
}
function calculateCurrency(amount,rate,target){
	amount = parseFloat(amount);
	value = Math.floor((amount*rate)*10000)/10000
	getObject(target).innerHTML = value+" ฿";
}
function selectAllCheckBoxInForm(formid){
	var form = document.getElementById(formid);
	for (var i = 0; i < form.elements.length; i++) {
		if(form.elements[i].type == 'checkbox'){
			form.elements[i].checked = !(form.elements[i].checked);
		}
	}
}
function selectAllCheckBoxInFormWithPrefixName(formid,prefixName){
	var form = document.getElementById(formid);
	for (var i = 0; i < form.elements.length; i++) {
		if(form.elements[i].type == 'checkbox'){
			if(form.elements[i].name != '' && form.elements[i].name.indexOf(prefixName) != -1)
				form.elements[i].checked = !(form.elements[i].checked);
		}
	}
}
function appendLineValue(id,value){
	var obj = getObject(id);
	if(obj != null){
		if(obj.value == '')
			obj.value = value;
		else
			obj.value = obj.value + "\r\n" + value;
	}
}

var message="ยินดีต้อนรับค่ะ ขอให้มีความสุขกับการเลือกซื้อสินค้ากับเรานะค่ะ / Welcome to our website, we hope you will have pleasure moment while shopping here";
function checkRestrictPage(){
	return (window.location.href.indexOf('link.html')<=0 && window.location.href.indexOf('catalog.html')<=0 && window.location.href.indexOf('catalog/')<=0 && window.location.href.indexOf('control/')<=0);
}
function rightClickIE(){
	if (event.button==2 && window.location.href.indexOf('link.html')<=0){
		//alert(message);
		return false;
	}
}
function rightClickNS(e){
	if((document.layers||document.getElementById&&!document.all) && (e.which==2||e.which==3) && window.location.href.indexOf('link.html')<=0){
		//alert(message);
		return false;
	}
}
if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=rightClickNS;
}
else if (document.all&&!document.getElementById){
	document.onmousedown=rightClickIE;
}
document.oncontextmenu=new Function("if(checkRestrictPage()){alert(message);return false;}")

function display(id,value){
	var obj = null;
	if(id instanceof Array){
		for(i=0;i<id.length;i++){
			obj = getObject(id[i]);
			if(obj != null)
				obj.style.display = value;
		}
	}else{
		obj = getObject(id);
		if(obj != null)
			obj.style.display = value;
	}
}
function hideObj(id){
	display(id,'none');
}
function showObj(id){
	display(id,'');
}
function hideShow(hids,sids){
	hideObj(hids);
	showObj(sids);
}

var fillElementId = null;
var openSelectImageFrom = null;
function setSelectImageToCKEditorImageDialog(url){
    if(CKEDITOR != null && CKEDITOR.currentOpenImageDialog != null){
        CKEDITOR.currentOpenImageDialog.setValueOf('info','txtUrl',url);
        CKEDITOR.currentOpenImageDialog = null;
    }
}
function selectUrl(url){
	if(window.opener.fillElementId != null){
		window.opener.setSelectFileElementValue(url);
        window.close();
	}else if(window.opener.openSelectImageFrom != null){
        window.opener.setSelectFileValue(url);
        window.close();
    }else if(window.opener.setSelectImageToCKEditorImageDialog){
        window.opener.setSelectImageToCKEditorImageDialog(url);
        window.close();
    }
}
function openSelectFile(url,targetid){
    openSelectImageFrom = targetid;
    window.open(url,'_blank','toolbar=0,location=0,menubar=0,scrollbars=1,height=575,width=995')
}
function setSelectFileValue(value){
	document.getElementById(openSelectImageFrom).value = value;
    //$('#'+openSelectImageFrom).attr('value',value);
	openSelectImageFrom = null;
}
function setSelectFileElementValue(value){
	document.getElementById(fillElementId).value = value;
    //$('#'+openSelectImageFrom).attr('value',value);
	fillElementId = null;
}