// JavaScript Document
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; 
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function checkbox_checker(objform){
	var checkbox_choices = 0;
	if ( objform.checkbox.length != undefined) {
		for (counter = 0; counter < objform.checkbox.length; counter++){
			if (objform.checkbox[counter].checked){ 
				checkbox_choices ++; 
			}
		}
	} else {
		if (objform.checkbox.checked){ 
			checkbox_choices ++;
		}
	}
	return checkbox_choices;
}

function edit_checkbox(objform){
	var checkbox_choices = 0;
	checkbox_choices=checkbox_checker(objform);
	if (checkbox_choices==0 ){
		alert("กรุณาเลือกรายการจำนวน 1 รายการที่จะแก้ไข");
		return false;
	}	
	if (checkbox_choices>1 ){
		alert('กรุณาเลือกรายการจำนวน 1 รายการเท่านั้นที่จะแก้ไข');
		return false;
	}
	return true;
}

function edit_gotourl(objform,url){
	if (edit_checkbox(objform)){
		objform.action=url;
		objform.submit();
		return true;
	}
	return false;
}
function delete_checkbox(objform){
	var checkbox_choices = 0;
	checkbox_choices=checkbox_checker(objform);
	if (checkbox_choices==0 ){
		alert("กรุณาเลือกรายการจำนวนอย่างน้อย 1 รายการที่จะลบ");
		return false;
	}	
	/*if (checkbox_choices>1 ){
		alert('กรุณาเลือกรายการจำนวน 1 รายการเท่านั้นที่จะแก้ไข');
		return false;
	}*/
	return confirm("คุณยืนยันที่จะลบรายการจำนวน "+ checkbox_choices +" รายการ ใช่หรือไม่?");
}

function delete_gotourl(objform,url){
	if (delete_checkbox(objform)){
		objform.action=url;
		objform.submit();
		return true;
	}
	return false;
}

function lov_open(url,obj_code,obj_value) {
var url_target=url+"?obj_code="+obj_code+"&obj_value="+obj_value;
var load = window.open(url_target,'','scrollbars=yes,menubar=no,height=500,width=600,resizable=yes,toolbar=no,location=no,status=no');
}
function lov_return(obj_code,obj_code_v,obj_value,obj_value_v){
	var f_code="window.opener."+obj_code+".value='"+obj_code_v+"';";
	var f_value="window.opener."+obj_value+".value='"+obj_value_v+"';";
	eval(f_code);
	eval(f_value);
	window.close();
}
function lov_return_modal(obj_code,obj_code_v,obj_value,obj_value_v){
	var f_code="window."+obj_code+".value='"+obj_code_v+"';";
	var f_value="window."+obj_value+".value='"+obj_value_v+"';";
	eval(f_code);
	eval(f_value);	
}

//add by bigon

function div_show(id)
{
     if (document.getElementById(id).style.display == 'none')
     {
          document.getElementById(id).style.display = '';
     }
}
function div_hide(id)
{
          document.getElementById(id).style.display = 'none';

}
function div_toggle(id)
{
     if (document.getElementById(id).style.display == 'none')
     {
          document.getElementById(id).style.display = '';
     }else{
		 document.getElementById(id).style.display = 'none';
	 }
}

/*
**************************************
* Event Listener Function v1.4       *
* Autor: Carlos R. L. Rodrigues      *
**************************************
*/
function safeFocus(e){
	try {
		e.focus();
		return true;
	} catch (f) {
		//alert ("can't focus"+e.name);
		return false;
	}
}
addEventListener = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEventListener = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};

enterAsTab = function(){
	for( var f, i = ( f = document.forms ).length; i; addEventListener( f[--i], "keydown", 
		function( evt ){
			if (evt.key != 13) return;
			var el, l, k = evt.key == 13, e = evt.target;
				//if( k && !/textarea|select/i.test( e.type ) && !evt.preventDefault() ){
				if( k && !/textarea/i.test( e.type ) && !evt.preventDefault() ){
					//for( l = k = (el=e.form.elements).length; el[--k] == e;);
					el = e.form.elements;
					var elCount = el.length;
					var e_index=0;
					for(var i=0; i<elCount; i++) {
						if (el[i] == e) {
							e_index = i;
							break;
						}
					}
					var setfocus = false;
					var lasttarget = null;
					for (var id=e_index+1; id < elCount; id++) {
						var target = el[id];
						if (target.disabled || (target.getAttribute("readonly")) || target.tagName=="BUTTON" /*|| (target.type.substr(0,6)=="select")*/ ) {
							continue;
						}
						if ((target.type=="button") || (target.type=="reset")) {
							if (lasttarget == null) {
								lasttarget = target;
							}
						}
						if (safeFocus(target)) {
							setfocus = true;
							break;
						}
					}
					
					if (!setfocus) {
						safeFocus(lasttarget);
					}					
				}
		} // function (evt)
		) // addEventListener(f[--i] ...
	); // for( var f, i ...
};

//onKeyUp=""
function buildQueryString(theFormName) {
	theForm = document.forms[theFormName];
	var qs = ''
	for (e=0;e<theForm.elements.length;e++) {
		if (theForm.elements[e].name!='') {
			qs+=(qs=='')?'?':'&'
			qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value)
			}
		}
	return qs
}

function callToServer(URL) {
	if (!document.createElement) {return true};
	
	var temp = URL.split("?")[0];
	var atemp = temp.split("/");
	temp = atemp[atemp.length-1];
	re=/[^a-z,^A-Z,^0-9]/g;
	var iFrameName = temp.replace(re,"");
	var iFrameObj=undefined;
	iFrameObj = document.getElementById(iFrameName);
	
	if (iFrameObj == null) {
		iFrameObj = document.createElement("iframe");
		iFrameObj.id=iFrameName;
		iFrameObj.style.display="none";
		document.body.appendChild(iFrameObj);
	} else {
		//alert("iFrame "+iFrameName+ " ready Existing.");
	}
	
	iFrameObj.src=URL;
	
	return true;		
}

var IFrameObj; // our IFrame object
function _callToServer(URL) {
	if (!document.createElement) {return true};
		
	var IFrameDoc;
	//var URL = 'server.html' + buildQueryString(theFormName);
	if (!IFrameObj && document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// callToServer() is called
		try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			
			if (document.frames) {
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['RSIFrame'];
			}
		} catch(exception) {
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='<iframe id="RSIFrame" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('callToServer("'+theFormName+'")',10);
		return false;
	}
	
	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return true;
	}
	
	IFrameDoc.location.replace(URL);
	return false;
}
	
function handleResponse() {
	var theFormDiv = document.getElementById('theFormDiv');
	theFormDiv.style.display = 'none';
	var responseMessage = document.getElementById('responseMessage');
	var message = '<p>Your message has been sent! Thank you.<\/p>'
	message += '<p><a href="#" onclick="showForm(); return false;">Send another message<\/a><\/p>'
	responseMessage.innerHTML = message;
	responseMessage.style.display = 'block';
}

function showForm() {
	var theFormDiv = document.getElementById('theFormDiv');
	theFormDiv.style.display = 'block';
	document.forms['emailForm'].reset();
	var responseMessage = document.getElementById('responseMessage');
	responseMessage.style.display = 'none';
}

function processState(st) {
	alert(st)
	}
function isvalidlength(obj,len,objerr){
	var objC=document.getElementById(obj);
	var objerrC=document.getElementById(objerr);
	if (len!='')
	{
		if (objC.value.length!=len)
		{
			if (objC.value.length>0){ objC.focus();}
			objerrC.innerText='***กรุณาระบุข้อมูลให้ถูกต้อง***';
			return false;
		}
	}
		return true;
}

function isValidDateDDMM(dateStr) {
	var vdate = 1;
// Checks for the following valid date formats:
	//ddmm
//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
var datePat = /^(\d{2})(\d{2})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
	//alert("Date is not in a valid format.")
	vdate=0;
	return vdate;
}
month = matchArray[1]; // parse date into variables
//day = matchArray[1];
year = matchArray[2];

if (month < 1 || month > 12) { // check month range
//alert("Month must be between 1 and 12.");
vdate=0;
return vdate;
}
if (day < 1 || day > 31) {
//alert("Day must be between 1 and 31.");
vdate=0;
return vdate;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//alert("Month "+month+" doesn't have 31 days!")
vdate=0;
return vdate;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
//alert("February " + year + " doesn't have " + day + " days!");
vdate=0;
return vdate;
   }
}

if(year.length != 4){
vdate=0;
return vdate;
}
if(day.length != 2){
vdate=0;
return vdate;
}
if(month.length != 2){
vdate=0;
return vdate;
}

return vdate;  // date is valid
}
