// JavaScript Document โก้
function autoIframe(frameId){
	try{
		frame = document.getElementById(frameId);
		innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
		objToResize = (frame.style) ? frame.style : frame;
		objToResize.height = innerDoc.body.scrollHeight;
		objToResize.width = '100%';
	}
	catch(err){
		window.status = err.message;
	}
}

function checkurl(url) {
	document.frm.action=url;
	document.frm.submit();
}

function checkurlSubmit(url) {
	document.frm.action=url;
	document.frm.onsubmit(); 
	document.frm.submit();
}

function popup_check_approve(url, textalert) {
	question = confirm(textalert);
	if (question != "0")
	{
		document.frm.action=url;
		document.frm.submit();
	}
	
}

function popup_confirm_del(url, textalert) {
	question = confirm(textalert);
	if (question != "0")
	{
		document.location.replace(url);
	}
}

function checkurl_confirm(url, textalert) {
	question = confirm(textalert);
	if (question != "0")
	{
		window.open(url);
		window.close();
	}
	
}

function check_num(field) {
	//alert('dgdf');
	var charCode = event.keyCode;
	//if (charCode > 31 && (charCode < 48 || charCode > 57)) {
	if ((charCode != 45) && (charCode != 46) && (charCode < 48 || charCode > 57)) { //(-) && (.) && (0-9)
		alert('<b>' + field + '</b>' + ' : Please numbers only.');
		event.returnValue = false;
	}
}

/*
function checkImageFile(fileName, td) {
	var str = document.getElementById(fileName).value;
	var arrstr = str.split(".")
	if( (arrstr[arrstr.length-1].toLowerCase() == 'jpeg') || (arrstr[arrstr.length-1].toLowerCase() == 'jpg') || (arrstr[arrstr.length-1].toLowerCase() == 'gif') ) {
		return true;
	} else {
		alert('กรุณาเลือกไฟล์ (gif, jpg, jpeg) ที่เป็นรูปภาพเท่านั้น'); 
		document.getElementById(td).innerHTML = "<input name=\"" + fileName + "\" type=\"file\" id=\"" + fileName + "\" onchange=\"checkImageFile('" + fileName + "', '" + td + "'); return false;\" style=\"width:95%;\"/>";
		return false;
	}
}

function check_image(show, td_show) {
	var doc = document.frm;
	var str = document.getElementById(show).value;
	var file = document.getElementById(show);
	var td = document.getElementById(td_show);
	//var str = doc.attachfile.value;
	var lng = str.length;
	var arrstr = str.split(".")
	if( (arrstr[arrstr.length-1].toLowerCase() == 'jpg') || (arrstr[arrstr.length-1].toLowerCase() == 'gif') ) {
		return true;
	} else {
		alert('This file type is not Image, Please select Image.'); 
		td.innerHTML = "";
		td.innerHTML = "<input type='file' name='" + show + "' id='" + show + "' onchange=\"check_image('" + show + "', '" + td_show + "')\"/>";
		//document.getElementById(show).value = "";
	}
}

function check_file(show, td_show) {
	var doc = document.frm;
	var str = document.getElementById(show).value;
	var file = document.getElementById(show);
	var td = document.getElementById(td_show);
	//var str = doc.attachfile.value;
	var lng = str.length;
	var arrstr = str.split(".")
	if( arrstr[arrstr.length-1].toLowerCase() == 'pdf' ) {
		return true;
	} else if( (arrstr[arrstr.length-1].toLowerCase() == 'doc') || (arrstr[arrstr.length-1].toLowerCase() == 'docx') ) {
		return true;
	} else if( (arrstr[arrstr.length-1].toLowerCase() == 'ppt') || (arrstr[arrstr.length-1].toLowerCase() == 'pptx') ) {
		return true;
	} else if( (arrstr[arrstr.length-1].toLowerCase() == 'xls') || (arrstr[arrstr.length-1].toLowerCase() == 'xlsx') ) {
		return true;
	} else {
		alert('This file type is not Acrobat(pdf), MS Word (doc,docx), MS Excel (xls,xlsx), MS Power Point (ppt, pptx). Please select again.'); 
		td.innerHTML = "";
		td.innerHTML = "<input style='width:100%' type='file' name='" + show + "' id='" + show + "' onchange=\"check_file('" + show + "', '" + td_show + "')\"/>";
	}
}
*/
function editDateFormat(strDate, objectShow) {
	var str_thumb = strDate.value;
	var arr_thumb = str_thumb.split("/");
	var d = arr_thumb[1];
	var m = arr_thumb[0];
	var y = arr_thumb[2];
	objectShow.value = d+' / '+m+' / '+y;
}

function check_email() { 
	var at="@"; 
	var dot="."; 
	var str = document.frm.email.value;
	var lat=str.indexOf(at); 
	var lstr=str.length; 
	var ldot=str.indexOf(dot); 
	var errmessage = "";
	
	if (str.indexOf(at)==-1){ 
		errmessage = "Invalid E-mail ID."; 
	} 
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ 
		errmessage = "Invalid E-mail ID."; 
	} 
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ 
		errmessage = "Invalid E-mail ID."; 
	} 
	if (str.indexOf(at,(lat+1))!=-1){ 
		errmessage = "Invalid E-mail ID."; 
	} 
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ 
		errmessage = "Invalid E-mail ID"; 
	} 
	if (str.indexOf(dot,(lat+2))==-1){ 
		errmessage = "Invalid E-mail ID."; 
	} 
	if (str.indexOf(" ")!=-1){ 
		errmessage = "Invalid E-mail ID."; 
	} 
	
	if ( str == "" ) {
		errmessage = "E-mail can't be blank, Please key E-mail."; 
	}
	
	return errmessage;
} 

function checkLogin(url) {
	var doc = document.frm;
	var errmessage = "";
	errmessage = check_email();
	if( errmessage == "" ) {
		if( doc.pass.value == "" ) {
			errmessage = "Password can't be blank, Please key Password."; 
			alert(errmessage);
			return false;
		} else {
			checkurl(url);
		}
	} else {
		alert(errmessage);
		return false;
	}
}

function check_blank(input, input_name, url) {
	if( document.getElementById(input).value == "" ) {
		alert(input_name + " can't be Blank.");
		return false;
	} else {
		checkurlSubmit(url);
	}
}

function checkRegis(url) {
	var doc = document.frm;
	var errmessage = "";
	var errmail = "";
	
	
	if( doc.name.value == "" ) { errmessage = errmessage + "   - Name can't be blank.\n"; }
	if( doc.address.value == "" ) { errmessage = errmessage + "   - Address can't be blank.\n"; }
	errmail = check_email();
	if( errmail != "" )  { errmessage = errmessage + "   - " + errmail + "\n"; }
	if( doc.pass.value == "" ) { 
		errmessage = errmessage + "   - Password can't be blank.\n";
	} else if ( doc.pass.value != doc.pass_confirm.value ) {
		errmessage = errmessage + "   - Confirm Password can't be different Password.\n";
	}
	
	if( errmessage != "" ) {
		alert(errmessage);
		return false;
	} else {
		checkurl(url);
	}
}

function chang_big_image(image_path, width, height) {
	var doc = document.frm;
	var big_image = document.getElementById('big_image');
	
	big_image.src= image_path;
	big_image.width= width;
	big_image.height= height;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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 MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
