function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


// สำหรับเอาค่าจาก list ใส่ input/text เพื่อให้เลือกจาก list หรือจะใส่เองใน input/text ก็ได้
function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
//    if (theValue == true || theValue == false)
//      eval("obj."+theProp+"="+theValue);
//    else 
	eval("obj."+theProp+"='"+theValue+"'");
  }
}



//Script สำหรับ ตรวจสอบค่า key ถ้าเป็น Enter ให้เลื่อนไป object ถัดไป
	//สำหรับตรวจสอบค่า key
	function checkAscii()	{
		if (event.keyCode==13)
			return true;
		else
			return false;
	}
	
	//สำหรับเรียกใช้
	function nextFocus(distination)	{
		if (checkAscii())
			distination.focus();
	}
	//distination คือ object ที่ต้องการให้ focus เมื่อกด enter




function selectdate_passBack(prdate,bfdate,afdate,formname,prefix,field_name,arraycount){

	objForm = opener.document.forms[formname];


	if( ( afdate > prdate) || ( bfdate < prdate)  ){
		alert("กำหนดวันที่ไม่ถูกต้องตามเงื่อนใข");
		}
	else{


 		if(objForm.elements[field_name].length){
         		objForm.elements[prefix+'[Date_Day]'][arraycount].value = prdate.getDate();
        		 	objForm.elements[prefix+'[Date_Month]'][arraycount].value = (prdate.getMonth()+1);
         		objForm.elements[prefix+'[Date_Year]'][arraycount].value = prdate.getFullYear();
         		objForm.elements[field_name][arraycount].value = prdate.getFullYear()+'-'+(prdate.getMonth()+1)+'-'+prdate.getDate();
			}
	   	else{
         		objForm.elements[prefix+'[Date_Day]'].value = prdate.getDate();
         		objForm.elements[prefix+'[Date_Month]'].value = (prdate.getMonth()+1);
         		objForm.elements[prefix+'[Date_Year]'].value = prdate.getFullYear();
         		objForm.elements[field_name].value =  prdate.getFullYear()+'-'+(prdate.getMonth()+1)+'-'+prdate.getDate();
       			}
		}

	}


function chkrowform(ff,formname)
	{
		dd = document.forms[formname].elements[ff.name];
		if(dd.length == 0 || dd.length == null)
			{
			if(dd == ff){return(0);}else{return(NULL);}
			}
		else
			{
			for(i=0;i<dd.length;i++)
				{if(dd[i] == ff){return(i);}}
			}	
	}




function updDate(fi_name,n)
	{
	
	//objForm = document.forms[0];
    objForm = f;
	if(objForm.elements[fi_name].length){
		dd = objForm.elements['tmp_'+fi_name+'[Date_Day]'][n].value;
		mm = objForm.elements['tmp_'+fi_name+'[Date_Month]'][n].value;
		yy = objForm.elements['tmp_'+fi_name+'[Date_Year]'][n].value;
		
		}
	else{
		dd = objForm.elements['tmp_'+fi_name+'[Date_Day]'].value;
		mm = objForm.elements['tmp_'+fi_name+'[Date_Month]'].value;
		yy = objForm.elements['tmp_'+fi_name+'[Date_Year]'].value;
		}



	prdate = new Date(yy,mm,dd);
	//if(( afdate > prdate) || ( bfdate < prdate) )
	if(0)
		{

		if(objForm.elements[fi_name].length){
			objForm.elements['tmp_'+fi_name+'[Date_Day]'][n].value = dd;
			objForm.elements['tmp_'+fi_name+'[Date_Month]'][n].value = mm;
			objForm.elements['tmp_'+fi_name+'[Date_Year]'][n].value = yy;
			}
		else{
			objForm.elements['tmp_'+fi_name+'[Date_Day]'].value = dd;
			objForm.elements['tmp_'+fi_name+'[Date_Month]'].value = mm;
			objForm.elements['tmp_'+fi_name+'[Date_Year]'].value = yy;
			}


		alert("กำหนดวันที่ไม่ถูกต้องตามเงื่อนใข");
		}
		else
		{
			if(dd != "" && mm != "" &&  yy != "")
				{
				if(objForm.elements[fi_name].length){
					objForm.elements[fi_name][n].value = yy+'-'+mm+'-'+dd;
					}
				else{
					objForm.elements[fi_name].value = yy+'-'+mm+'-'+dd;
					}
				}
			else
				{objForm.elements[fi_name].value ="";}
			
		}
				
	}

 function updTime2(fi_name,ff,formname){

	objTime = new Date();	
	objForm = document.forms[formname];
	arrcount = chkrowform(ff,formname);
	if(objForm.elements[fi_name].length){
		hh_Index = objForm.elements['tmp_'+fi_name+'[time_hh]'][arrcount].selectedIndex;
		hh = objForm.elements['tmp_'+fi_name+'[time_hh]'][arrcount].options[hh_Index].text;
		mm_Index = objForm.elements['tmp_'+fi_name+'[time_mm]'][arrcount].selectedIndex;
		mm = objForm.elements['tmp_'+fi_name+'[time_mm]'][arrcount].options[mm_Index].text;	
		}
	else{
		hh_Index = objForm.elements['tmp_'+fi_name+'[time_hh]'].selectedIndex;
		hh = objForm.elements['tmp_'+fi_name+'[time_hh]'].options[hh_Index].text;
		mm_Index = objForm.elements['tmp_'+fi_name+'[time_mm]'].selectedIndex;
		mm = objForm.elements['tmp_'+fi_name+'[time_mm]'].options[mm_Index].text;
		}
	if(hh.value >= 24 || hh.value < 0 ){
		alert("Hour Error");
		hh = "";
		mm = "";
		objForm.elements[fi_name].value = "";
		}
	else if(mm.value >= 60 || hh.value < 0 ){
		alert("Minute Error");
		hh = "";
		mm = "";
		objForm.elements[fi_name].value = "";
		}
	else if(mm != '' && hh != '')
		{		

		if(objForm.elements[fi_name].length){
			objForm.elements[fi_name][arrcount].value = hh+':'+mm;
			}
		else{
			objForm.elements[fi_name].value = hh+':'+mm;
			}
		}
	} // End Function 



 function updTime(fi_name,ff,formname)
		  	{
		  	objTime = new Date();	
			objForm = document.forms[formname];
			arrcount = chkrowform(ff,formname);
			if(objForm.elements[fi_name].length){
				hh = objForm.elements['tmp_'+fi_name+'[time_hh]'][arrcount];
				mm = objForm.elements['tmp_'+fi_name+'[time_mm]'][arrcount];
				}
			else{
				hh = objForm.elements['tmp_'+fi_name+'[time_hh]'];
				mm = objForm.elements['tmp_'+fi_name+'[time_mm]'];
				}

			//hh = objForm.elements['tmp_'+fi_name+'[time_hh]'];
			//mm = objForm.elements['tmp_'+fi_name+'[time_mm]'];
			
			//alert(hh.value+'|'+mm.value);
			if(hh.value >= 24 || hh.value < 0 )
				{
				alert("Hour Error");
				hh.value = "";
				mm.value = "";
				objForm.elements[fi_name].value = "";
				}
			else if(mm.value >= 60 || hh.value < 0 )
				{
				alert("Minute Error");
				hh.value = "";
				mm.value = "";
				objForm.elements[fi_name].value = "";
				}
			else if(mm.value != '' && hh.value != '')
				{		
				objTime.setHours(hh.value);
				objTime.setMinutes(mm.value);
				str_hh =objTime.getHours();
				str_hh2 = str_hh.toString();
				if(str_hh2.length == 1){str_hh2 ='0'+str_hh2;}
				str_mm =objTime.getMinutes();
				str_mm2  = str_mm.toString();
				if(str_mm2.length == 1){str_mm2 ='0'+str_mm2;}
				//alert(str_hh2+':'+str_mm2);
				//objForm.elements[fi_name].value = objTime.getHours()+':'+objTime.getMinutes();
				if(objForm.elements[fi_name].length){
					//objForm.elements[fi_name][arrcount].value = objTime.getHours()+':'+objTime.getMinutes();
					//objForm.elements['tmp_'+fi_name+'[time_hh]'][arrcount].value=objTime.getHours();
					//objForm.elements['tmp_'+fi_name+'[time_mm]'][arrcount].value=objTime.getMinutes();

					objForm.elements[fi_name][arrcount].value = str_hh2+':'+str_mm2;
					objForm.elements['tmp_'+fi_name+'[time_hh]'][arrcount].value=str_hh2;
					objForm.elements['tmp_'+fi_name+'[time_mm]'][arrcount].value=str_mm2;
					}
				else{
					//objForm.elements[fi_name].value = objTime.getHours()+':'+objTime.getMinutes();
					//objForm.elements['tmp_'+fi_name+'[time_hh]'].value=objTime.getHours();
					//objForm.elements['tmp_'+fi_name+'[time_mm]'].value=objTime.getMinutes();				

					objForm.elements[fi_name].value = str_hh2+':'+str_mm2;
					objForm.elements['tmp_'+fi_name+'[time_hh]'].value=str_hh2;
					objForm.elements['tmp_'+fi_name+'[time_mm]'].value=str_mm2;		
					}


				}
			}



function setEnableTime(fi_name,ff){

	objForm = document.forms[0];
	arrcount = chkrowform(ff);
	
	if(objForm.elements[fi_name].length){

		objForm.elements['tmp_'+fi_name+'[time_hh]'][arrcount].disabled = !(ff.checked);
		objForm.elements['tmp_'+fi_name+'[time_mm]'][arrcount].disabled = !(ff.checked);
		}
	else{
		objForm.elements['tmp_'+fi_name+'[time_hh]'].disabled = !(ff.checked);
		objForm.elements['tmp_'+fi_name+'[time_mm]'].disabled = !(ff.checked);	
		}
	}

function  setDisableTime(fi_name,arrcount){

	objForm = document.forms[0];
	//arrcount = chkrowform(ff);
	if(objForm.elements[fi_name].length){
		arrcount = (objForm.elements[fi_name].length) -1 ;
		objForm.elements['tmp_'+fi_name+'[time_hh]'][arrcount].disabled = true;
		objForm.elements['tmp_'+fi_name+'[time_mm]'][arrcount].disabled = true;
		}
	else{
		objForm.elements['tmp_'+fi_name+'[time_hh]'].disabled = true;
		objForm.elements['tmp_'+fi_name+'[time_mm]'].disabled = true;	

		}
	}



function autotab(original,destination,formname){
	arrcount=chkrowform(original,formname);
	objForm = document.forms[formname];
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength")){
		if(objForm.elements[original.name].length){destination[arrcount].focus();}
		else{destination.focus();}
		}
	}	

	function findchkboxval(Sname,Tname){
		a = MM_findObj(Sname);
		c = MM_findObj(Tname);
		b = '';
		j = 0;
		if(a.length == 0 || a.length == null){
			if(a.checked){b +=a.value;}
			
			}
		else{
			for(i=0;i<a.length;i++){
				if(a[i].checked){
					if(j!=0){b+=',';}
					b +=a[i].value;
					j++;
					}
				}
			}
		c.value = b;
		}
function mtrack(e) {

   var text = 'Coordinates: ';
   if (document.layers) {

	mousex = e.pageX;
	mousey = e.pageY;
   } else {
  

   if(checkIt('msie')){

	mousex = event.x;
	mousey = event.y;
	}
	
	else{

	mousex = e.clientX;
	mousey = e.clientY;
	}


   }
   
}
/**Check brower */
function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function rowcontrollrender(rowrun,aa1){
//d2=MM_findObj(d0);
d3 = "<a id='uploadins_"+rowrun+"'   href=javascript:uploadinsertrow("+aa1['name']+");     >";
if( aa1['imagerowadd']!=null){
d3+="<img src=\""+ aa1['imagerowadd']+"\"  border=0  title=\"เพิ่มแถว\">";
}
else{
d3+="<b>+</b>";
}
d3 +="</a>&nbsp;";
d3+="<a id='uploaddel_"+rowrun+"'    href=javascript:uploaddelrow('"+aa1['sourceid']+"_"+rowrun+"',"+aa1['name']+");    >";
if( aa1['imagerowdel']!=null){
d3+="<img src=\""+ aa1['imagerowdel']+"\"    border=0 title=\"ลบแถว\">";
}
else{
d3 +="<b>-</b>";
}

d3 +="</a>";
//d2.innerHTML = d3;
return(d3);
}


function uploaddelrow(d0,aa1){
	  d2=MM_findObj(d0);
	  delRow = d2.parentNode;
	  datarowcount = 0;
	   for (var i=0; i<delRow.childNodes.length; i++) {
	   if(delRow.childNodes[i].name == aa1['sourceid']+'datarow'){datarowcount++}
	   }
	  for (var i=0; i<delRow.childNodes.length; i++) {
	      if(d0 == delRow.childNodes[i].id  && datarowcount >1){
		  		d2.parentNode.removeChild(delRow.childNodes[i]);
				}
	  }
}


function uploadinsertrow(aa1){
	d0 = aa1['sourceid'];
    var orgid = new Array();
    var rowrun = Math.round(Math.random() * 100000);
    var  ma1=MM_findObj(d0);
	ma1.style.visibility = "hidden";
	ma1.style.position = "absolute";
	//rowcontrollrender(d1);
    var theNewParagraph = document.createElement('DIV');
	 theNewParagraph.id = d0+'_'+rowrun ;
	 theNewParagraph.name = d0+'datarow' ;
	 //alert(theNewParagraph.style.visibility);
	mainTB = document.createElement('TABLE');
	mainTB.cellSpaceing = 0;
	mainTB.cellPadding = 0;
	mainTB.border = 0;
	mainTBODY = document.createElement('TBODY');
	mainTR = document.createElement('TR');
	mainTD1 = document.createElement('TD');
	mainTD1.innerHTML =  ma1.innerHTML;
	mainTD2 = document.createElement('TD');
	mainTD2.innerHTML = rowcontrollrender(rowrun,aa1);
	mainTR.appendChild(mainTD1);
	mainTR.appendChild(mainTD2);
	mainTBODY.appendChild(mainTR);
	mainTB.appendChild(mainTBODY);
	theNewParagraph.appendChild(mainTB);
	 ma0= MM_findObj(aa1['parseareaid']);
	 //alert(theNewParagraph.innerHTML);
    ma0.appendChild(theNewParagraph);

}

function chkrowform(ff,formname)
{	
   dd = document.forms[formname];
   //dd = document.forms[0].elements[ff.name];
	if (dd.length == 0 || dd.length == null)
	{	if (dd == ff) { return(0); }
		else { return(NULL); }
	}
	else
	{	for (i = 0; i < dd.length; i++)
		{	if (dd[i] == ff) { return(i); } }
	}	
}


function setdropdown(fm,em,ee)
{	dd = document.forms[0].elements[fm];
	for (i=0; i < dd[em].options.length; i++)
	{	if (dd[em].options[i].value == ee)
		{	//alert(dd[em].options[i].value);
			dd[em].selectedIndex = i;
		}
	}
}
