
function checkAll(pvalue,pBox) {
 if (pBox) {
 	var nbox=pBox.length;
	if (nbox==undefined) { 
		pBox.checked=pvalue 
		if (pBox.checked==true)MO_2(pBox)
		else MU_2(pBox)
	} else {
	    for (var j = 0; j < nbox; j++)  {
			if(pvalue)MO_2(pBox[j]);
			else MU_2(pBox[j]);
			pBox[j].checked=pvalue;
		}
	}	
  }
}
function checkIt(pall,pbox,pvalue) {   
	var ibox=pbox.length;  
    if (pvalue.checked==false) { 
		pall.checked=pvalue.checked; 
		if (ibox==undefined) { MU_2(pbox) }
		for (var j = 0; j < ibox; j++)  {
			if(pbox[j].checked==pvalue.checked){ MU_2(pbox[j])}
		}
	} else {
		MO_2(pvalue);
		for (var j = 0; j < ibox; j++)  {
			if (pbox[j].checked!=pvalue.checked){return;}
		}
		pall.checked=pvalue.checked;
	}	 
}

function checkAllGroup(pvalue,theform) {
	if (document.all||document.getElementById){
		for (i=0;i<theform.length;i++){
			if (theform.elements[i].type == "checkbox") {
				theform.elements[i].checked=pvalue;
			}
		}
	}	
}
function checkItGroup(pall,pvalue,theform) {
    if (pvalue.checked==false) { pall.checked=pvalue.checked; }
	else{
	var iAll=0;
	var iCount=0;
	(document.all||document.getElementById)
		for (i=0;i<theform.length;i++){
			if (theform.elements[i].type == "checkbox") {
				iAll=iAll+1
				if (theform.elements[i].checked==pvalue.checked)iCount=iCount+1;
			}
		}
		if ((iAll-1)==iCount) pall.checked=pvalue.checked;
	}
}

function checkValues(pbox) {
 var xID="";
 if (pbox) {
 	if (pbox.length==undefined) {
		if (pbox.checked==true) { xID=pbox.value}	
	} else
	 {
	  var nbox=pbox.length; 
 	   for (var j = 0; j < nbox; j++)  {
		  if (pbox[j].checked==true) {
		 		 if (xID=="") {	xID=pbox[j].value; } else {	xID=xID +"," +pbox[j].value;}	
			} /*Box Select */
		} /*for loop*/
	 } /*>1*/
 } 
 return xID;	
}

function DelAllImage(tb, fid, id, url) {
	if(confirm("ยืนยันการลบข้อมูล")) {
		window.location.replace("../../libs/DelAllImage.php?tb="+tb+"&fid="+fid+"&id="+id+"&url="+url);
	}
}

function DeleteRecordAndAllImage(tb, imgtb, fid, id, url) {
	if(confirm("ยืนยันการลบข้อมูล")) {
		window.location.replace("../../libs/DelRecordandimage.php?tb="+tb+"&imgtb="+imgtb+"&fid="+fid+"&id="+id+"&url="+url);
	}
}

function DeleteRecordsAndAllImages(tb, imgtb, fid, pbox, url) {
	var xValue=checkValues(pbox);
	if (xValue=="") { 
		alert("กรุณาเลือกรายการที่ต้องการลบ");
	}
	else {
		aValue = xValue.split(',');
		if (confirm("ยืนยันการลบข้อมูล " + aValue.length + " รายการ")) {
			window.location.replace("../../libs/DelRecordandimage.php?tb="+tb+"&imgtb="+imgtb+"&fid="+fid+"&id="+xValue+"&url="+url);
		}
	}
}

function DeleteSubTypeRecord(tb, tb2, imgtb, fid1, fid2, id, url) {
	if(confirm("ยืนยันการลบข้อมูล")) {
		window.location.replace("../../libs/DelSubTypeRecord.php?tb="+tb+"&tb2="+tb2+"&imgtb="+imgtb+"&fid1="+fid1+"&fid2="+fid2+"&id="+id+"&url="+url);
	}
}

function DeleteSubTypeRecords(tb, tb2, imgtb, fid1, fid2, pbox, url) {
	var xValue=checkValues(pbox);
	if (xValue=="") { 
		alert("กรุณาเลือกรายการที่ต้องการลบ");
	}
	else {
		aValue = xValue.split(',');
		if (confirm("ยืนยันการลบข้อมูล " + aValue.length + " รายการ")) {
			window.location.replace("../../libs/DelSubTypeRecord.php?tb="+tb+"&tb2="+tb2+"&imgtb="+imgtb+"&fid1="+fid1+"&fid2="+fid2+"&id="+xValue+"&url="+url);
		}
	}
}

function DeleteTypeProduct(id, url) {
	if(confirm("ยืนยันการลบข้อมูล")) {
		window.location.replace("../../libs/DelTypeProduct.php?id="+id+"&url="+url);
	}
}

function DeleteTypeProducts(pbox, url) {
	var xValue=checkValues(pbox);
	if (xValue=="") { 
		alert("กรุณาเลือกรายการที่ต้องการลบ");
	}
	else {
		aValue = xValue.split(',');
		if (confirm("ยืนยันการลบข้อมูล " + aValue.length + " รายการ")) {
			window.location.replace("../../libs/DelTypeProduct.php?id="+xValue+"&url="+url);
		}
	}
}

function delRecordAndSubAndImage(tb, tb1, id, url, fid) {
	if(confirm("ยืนยันการลบข้อมูล")) {
		window.location.replace("../../libs/delData.php?tb="+tb+"&tb1="+tb1+"&id="+id+"&url="+url+"&fid="+fid+"&fimg=true");
	}
}

function delRecordsAndSubsAndImages(tb, tb1, pbox, url, fid) {
	var xValue=checkValues(pbox);
	if (xValue=="") { 
		alert("กรุณาเลือกรายการที่ต้องการลบ");
	}
	else {
		aValue = xValue.split(',');
		if (confirm("ยืนยันการลบข้อมูล " + aValue.length + " รายการ")) {
			window.location.replace("../../libs/delData.php?tb="+tb+"&tb1="+tb1+"&id="+xValue+"&url="+url+"&fid="+fid+"&fimg=true"); 
		}
	}
}

function delRecordAndSub(tb, tb1, id, url, fid) {
	if(confirm("ยืนยันการลบข้อมูล")) {
			window.location.replace("../../libs/delData.php?tb="+tb+"&tb1="+tb1+"&id="+id+"&url="+url+"&fid="+fid+"&img=true&img2=true"); 
	}
}
function delRecordsAndSubs(tb, tb1, pbox, url, fid) {
	var xValue=checkValues(pbox);
	if (xValue=="") { 
		alert("กรุณาเลือกรายการที่ต้องการลบ");
	}
	else {
		aValue = xValue.split(',');
		if (confirm("ยืนยันการลบข้อมูล " + aValue.length + " รายการ")) {
			window.location.replace("../../libs/delData.php?tb="+tb+"&tb1="+tb1+"&id="+xValue+"&url="+url+"&fid="+fid+"&img=true&img2=true"); 
		}
	}
}

function delRecord(tb, id, url) {
	if(confirm("ยืนยันการลบข้อมูล")) {
		window.location.replace("../../libs/delData.php?tb="+tb+"&id="+id+"&url="+url);
	}
}
function delRecords(tb, pbox, url) {
	var xValue=checkValues(pbox);
	if (xValue=="") { 
		alert("กรุณาเลือกรายการที่ต้องการลบ");
	}
	else {
		aValue = xValue.split(',');
		if (confirm("ยืนยันการลบข้อมูล " + aValue.length + " รายการ")) {
			window.location.replace("../../libs/delData.php?tb="+tb+"&id="+xValue+"&url="+url); 
		}
	}
}

function delRecordAndImage(tb, id, url) {
	if(confirm("ยืนยันการลบข้อมูล")) {
		window.location.replace("../../libs/delData.php?tb="+tb+"&id="+id+"&url="+url+"&img=true");
	}
}
function delRecordsAndImages(tb, pbox, url) {
	var xValue=checkValues(pbox);
	if (xValue=="") { 
		alert("กรุณาเลือกรายการที่ต้องการลบ");
	}
	else {
		aValue = xValue.split(',');
		if (confirm("ยืนยันการลบข้อมูล " + aValue.length + " รายการ")) {
			window.location.replace("../../libs/delData.php?tb="+tb+"&id="+xValue+"&url="+url+"&img=true"); 
		}
	}
}

function delImage(tb, id, url, field) {
	if(confirm("ยืนยันการลบข้อมูล")) {
		window.location.replace("../../libs/delImage.php?tb="+tb+"&id="+id+"&url="+url+"&field="+field);
	}
}

function oneValues(pbox) {
 var xID="";
 if (pbox) {
 	if (pbox.length==undefined) {
		if (pbox.checked==true) { xID=pbox.value}	
	} else
	 {
	  var nbox=pbox.length; 
	  var iCount=0;
 	   for (var j = 0; j < nbox; j++)  {
		  if (pbox[j].checked==true) {xID=pbox[j].value; iCount = iCount+1;} /*Box Select */
		} /*for loop*/
	 } /*>1*/
 } 
 if (iCount>1) { xID = "-1"};
 return xID;	
}


function moveTo(pform,pbox,purl,iselect,pNoselected) {
 if (iselect > 0) {
  var MsgID=checkValues(pbox);
  if (MsgID=="") {alert(pNoselected); }
   else {
    	  pform.action=purl;  pform.submit();
	   }
  }
}

//Numeric Format
function numFormat(elem, lead, sep, Min,Max)
{
	if (elem.value == '') 
	{
		elem.value = format('0', lead, sep);
		return true;
	}	
	var value = parseInt(cleanNumber(elem.value), 10);
	if (lead == '') 
	{
		if (Min > value)
		{
			alert('You have exceeded the range for the interior size.\nPlease check your information and try again.');
			value = Min;
		}		
		if (value > Max) 
		{
			alert('You have exceeded the range for the interior size.\nPlease check your information and try again.');
			value = Max;
		}
	}
	if (isNaN(value)) 
	{
		alert('You have entered an incorrect character on this field. \nPlease check your information and try again.');
		elem.value = format(Min, lead, sep);
		elem.focus();
		return false;
	}
	elem.value = format(value, lead, sep);
	return true;
}

function format(value, lead, sep)
{
	var strValue = new String(value);
	var len = strValue.length;
	var n;
	var strRet = '';
	var ctChar = 3 - (len%3);
	if (ctChar == 3) ctChar =0;
	for (n=0; len > n; n++) {
		if (ctChar == 3) {
			strRet += sep;
			ctChar = 0;
		}
		ctChar++;
		strRet += strValue.substring(n,n+1)		
	}
	if (lead == '%') 
	{
		return strRet + lead;
	}
	else 
	{
		return lead + strRet;
	}
}

function replace(szBuf, szFind, szReplace, lStart)
{
	var lFind = 0;
	if (!lStart) lStart = 0;
	
	while (lFind != -1) {
		lFind = szBuf.indexOf(szFind, lStart);

		if (lFind != -1) {
			szBuf = szBuf.substring(0,lFind) + szReplace + szBuf.substring(lFind + szFind.length);
			lStart = lFind + szReplace.length;
		}
	}
	return szBuf;
}

function cleanNumber(strNum)
{
	if (!strNum) return strNum;
	strNum = replace(strNum, '$', '', 0);
	strNum = replace(strNum, ',', '', 0);
	strNum = replace(strNum, '%', '', 0);	
	return strNum;
}

function onIntRangeChange(pElem,Min,Max) { 
	var v =cleanNumber(pElem.value);
	var bInvalid = false;
	if (isNaN(Number(v))) {
		bInvalid = true;}	
		numFormat(pElem, '', '',Min,Max);
	return(true);
}

//ฟังก์ชั่นเกี่ยวกับวันที่
function DateBoxValid(fDay, fMonth, fYear, fRequest)	{
  //return 0 If  Data is in Currect Format and the value is possible.
  var DateField =  fMonth.options[fMonth.selectedIndex].value + "/" + fDay.options[fDay.selectedIndex].value + "/"+  fYear.options[fYear.selectedIndex].value;
  var dateFormat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
  var matchArray = DateField.match(dateFormat); 
  var ErrMessage="";
    if (matchArray == null) {
		  if ( fRequest==true) { ErrMessage=" empty Date value, ";}  
     }
	  else //Check data Value
       {	 
	    day = matchArray[3];
    	month = matchArray[1]; 
		year = matchArray[4];
		if (day < 1 || day > 31) {
			ErrMessage=ErrMessage+" Day must be between 1 and 31, ";}
	  	if (month < 1 || month > 12) {
			ErrMessage=ErrMessage+ " Month must be between 1 and 12, ";}
	  	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
			ErrMessage=ErrMessage+" - Month "+month+" doesn't have 31 days!, ";}
	  	if (month == 2) {
	    	 var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
         	  if (day>29 || (day==29 && !isleap)) {
                 ErrMessage=ErrMessage+" - February " + year + " doesn't have " + day + " days!, "; }
	       } 		   
	 }//end else null
	 if ((ErrMessage.length>0) ||  (DateField.length ==0)) {
	    if (DateField.length==0) { 
		  return " must have value.";
		 } else { 
			  return ErrMessage;
			}  
	  }  else
	    {

			return "";
	  }
}//End function

function checkBoxs(pbox) {
 var xID="";
 if (pbox) {
	  var nbox=pbox.length; 
 	   for (var j = 0; j < nbox; j++)  {
		  if (pbox[j].checked==true) {
		 		 if (xID=="") {	xID=pbox[j].value; } else {	xID=xID +"," +pbox[j].value;}	
			} /*Box Select */
		} /*for loop*/
 } 
 return xID;	
}
function MO(e)
{
if (!e)
var e=window.event;
var S=e.srcElement;
while (S.tagName!="TR")
{S=S.parentElement;}
if (S.className!="R") { S.className="TR1"; }
}
function MU(e)
{
if (!e)
var e=window.event;
var S=e.srcElement;
while (S.tagName!="TR")
{S=S.parentElement;}
if (S.className!="R") { S.className="P2"; }
}
function MO_2(m)
{
while (m.tagName!="TR")
{m=m.parentElement;}
m.className="R";
}
function MU_2(m)
{
while (m.tagName!="TR")
{m=m.parentElement;}
m.className="P";
}
function on(name){
  if (browserOK) {
     for (i = 0; i < objCount; i++) {
      if (document.images[pics[i][2]] != null)
        if (name != pics[i][2]) { 
          document.images[pics[i][2]].src = pics[i][0].src;
        } else {
           document.images[pics[i][2]].src = pics[i][1].src;
        }
    }
  }
}

function off(){
  if (browserOK) {
     for (i = 0; i < objCount; i++) {
      if (document.images[pics[i][2]] != null) 
        document.images[pics[i][2]].src = pics[i][0].src;
    }
  }
}
function Menu_c()  // clicked menu
{
	for (i=0;i<document.all.length;i++)
		if (document.all[i].className=="mtc") {
			document.all[i].className = "";
	}
	if (!e)
	var e=window.event;
	var S=e.srcElement;
	while (S.tagName!="TR") {
		S=S.parentElement;
	}
	S.className="mtc";
}

function Menu_focus()  // clicked menu
{
	if (!e)
	var e=window.event;
	alert(e.id)
	var S=e.srcElement;
	while (S.tagName!="TD") {
	//	S=S.parentElement;
	S.className="mtc";
	}
}
 
function Menu_t(e)  // on mouse over
{
if (!e)
var e=window.event;
var S=e.srcElement;
while (S.tagName!="TR")
{S=S.parentElement;}
if (S.className!="mto" & S.className!="mtc") { S.className="mto"; }
}
function Menu_o(e)  // on mouse out
{
if (!e)
var e=window.event;
var S=e.srcElement;
while (S.tagName!="TR")
{S=S.parentElement;}
if (S.className!="mtr" & S.className!="mtc") { S.className="mtr"; }
}
function openPreview(sf)
{
LeftPosition = (screen.width) ? (screen.width-800)/2 : 0;
TopPosition = (screen.height) ? (screen.height-600)/6 : 0;
pw = window.open(sf, 'preview', 'toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=800,height=600,left='+LeftPosition+', top='+TopPosition+'');
pw.focus();
}