// ******************************************************************
//  Function : Javascript
// ******************************************************************
// Developed By : อลงกรณ์ คงดีได้  (Accords)
// E-mail :  thaiunionmax@hotmail.com
// License : 14/2/2550
// ******************************************************************

//*******************************************************************
//  Disable Copy Text
//*******************************************************************
function disableselect(e){
	return false
}

function reEnable(){
	return true
}
//if IE4+
document.onselectstart=new Function ("return false")

//*******************************************************************
//  Hide Status
//*******************************************************************
function hidestatus(){
	window.status=''
	return true
}

if (document.layers)
document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)
document.onmouseover=hidestatus
document.onmouseout=hidestatus

//*******************************************************************
//  Disable Click Right
//*******************************************************************
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

//******************************************************************************************************
// Eexplanation :
// Input Value  : String(form.name.value)
//******************************************************************************************************
function InValidChar( String )
{
	var NotPermitChar = "`~!@#$%^&*()-=+\\|[]{};:'\",./<>?";

	for(var i = 0; i < String.length; i++){
		for (var j = 0; j < NotPermitChar.length; j++){
			if( String.charAt(i) == NotPermitChar.charAt(j) ){
				return true;
			}
		}
	}
	return false;
}

//*******************************************************************
//  return false
//*******************************************************************
function CheckName(str)
{
	var strName = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*().-_+=<,>?':;[]{}";
	var len = str.length;
	
	for(i=0;i<len;i++) 
	{
		if(strName.indexOf(str.substring(i,i+1)) > 0) return true;
	}
	return false;	
}

//*******************************************************************
//  Swf
//*******************************************************************
function swfShow(src,width,height){
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+width+'" height="'+height+'"><param name="wmode" value="transparent"/><param name="movie" value="'+src+'"><param name="quality" value="high"><param name="menu" value="false"><embed src="'+src+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" wmode="transparent"></embed></object>');
}

function swfShowNoBg(src,width,height){
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+width+'" height="'+height+'"><param name="movie" value="'+src+'"><param name="quality" value="high"><param name="menu" value="false"><embed src="'+src+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'"></embed></object>');
}

function iframeSwitch()
{
   //find flash content container and grab contents 
   //(should only be object and embed tags)
   var container = $("#flash_content");
   var c = container.html();

   //get the size from the object element
   var object = $("object",container);
   var x = object.width();
   var y = object.height();

   //create the iframe element, hidden for now
   var frame = $.IFRAME({ 
      id:'graph_content_frame',
      frameborder:'0',
      marginwidth:'0',
      marginheight:'0',
      scrolling:'no'
   });
   $(frame).css('display','none');
   
   //attach the iframe to the document and remove the 
   //old flash container element
   container.after(frame).remove();
   
   //set the size, open the iframe, and insert
   //the flash content. 
   $("#graph_content_frame")
   .css('border','none')
   .height(y+'px')
   .width(x+'px')
   .css('display','block')
   .each(function(){
      this.contentDocument.open();
      this.contentDocument.write(c);
      this.contentDocument.close();
   });
}

//*******************************************************************
//  Title Bar
//*******************************************************************
/*
var tt = 0;
var FMess = new Array("ข้อความที่ 1","ข้อความที่ 2","ข้อความที่ 3");

function FTitle()
{
document.title = FMess[tt]
tt = (tt < (FMess.length -1)) ? tt + 1 : 0;
setTimeout('FTitle()',700);
}
FTitle();
*/

//*******************************************************************
//  Popup windows
//*******************************************************************
function popup(url,width,height){
	window.open(url,"vconline","toolbar=0,scrollbars=1,location=0,status=0,resizable=0,width="+width+",height="+height);
}

function popup2(url,width,height){
	window.open(url,"vconline","toolbar=0,scrollbars=0,location=0,status=0,resizable=0,width="+width+",height="+height);
}

function newWindow(mypage,myname,w,h,features) {
	var win = null;
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus();
}

//*******************************************************************
//  Cookie
//*******************************************************************
function getCookieVal (offset) {     
    var endstr = document.cookie.indexOf (";", offset);     
    if (endstr == -1) endstr = document.cookie.length;     
    return unescape(document.cookie.substring(offset, endstr));   
}   
  
// เรียกใช้งาน Cookie   
function GetCookie (name) {     
    var arg = name + "=";     
    var alen = arg.length;     
    var clen = document.cookie.length;     
    var i = 0;     
    while (i < clen) {       
        var j = i + alen;       
        if (document.cookie.substring(i, j) == arg)         
        return getCookieVal (j);       
        i = document.cookie.indexOf(" ", i) + 1;       
        if (i == 0) break;      
    }     
    return null;   
}   
  
// สร้าง Cookie   
function SetCookie (name, value) {     
    var argv = SetCookie.arguments;     
    var argc = SetCookie.arguments.length;     
    var expires = (argc > 2) ? argv[2] : null;  //alert(expires);   
    var path = (argc > 3) ? argv[3] : null;     
    var domain = (argc > 4) ? argv[4] : null;     
    var secure = (argc > 5) ? argv[5] : false;     
    document.cookie = name + "=" + escape (value) +    
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +    
        ((path == null) ? "" : ("; path=" + path)) +     
        ((domain == null) ? "" : ("; domain=" + domain)) +       
        ((secure == true) ? "; secure" : "");   
}   
  
// ลบ Cookie   
function DeleteCookie(name) {     
    var exp = new Date();
    var cval = GetCookie (name);
	exp.setTime (exp.getTime() - 1);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();   
} 

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";

	document.cookie = name+"="+value+expires+"; path=/";
	//	alert('Add Item '+ name + ' Complete.');
	window.location = "shopping_cart.php";
}

function createCookie2(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";

	document.cookie = name+"="+value+expires+"; path=/";
	//	alert('Add Item '+ name + ' Complete.');
	window.location = "makeproduct.php";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function eraseCookie2(name , url) {
	createCookie(name,"",-1);
	window.location = url;
}

//*******************************************************************
//  Input Text
//*******************************************************************
function banKey(evt , style)
{
	var allowedEng //อนุญาตให้คีย์อังกฤษ
	var allowedThai //อนุญาตให้คีย์ไทย
	var allowedNum //อนุญาตให้คีย์ตัวเลข

	if(style == "str_num"){
		allowedEng = true; //อนุญาตให้คีย์อังกฤษ
		allowedThai = false; //อนุญาตให้คีย์ไทย
		allowedNum = true; //อนุญาตให้คีย์ตัวเลข
	}else if(style == "str"){
		allowedEng = true; //อนุญาตให้คีย์อังกฤษ
		allowedThai = false; //อนุญาตให้คีย์ไทย
		allowedNum = false; //อนุญาตให้คีย์ตัวเลข
	}else if(style == "str2_num"){
		allowedEng = false; //อนุญาตให้คีย์อังกฤษ
		allowedThai = true; //อนุญาตให้คีย์ไทย
		allowedNum = true; //อนุญาตให้คีย์ตัวเลข
	}else if(style == "str2"){
		allowedEng = false; //อนุญาตให้คีย์อังกฤษ
		allowedThai = true; //อนุญาตให้คีย์ไทย
		allowedNum = false; //อนุญาตให้คีย์ตัวเลข
	}else if(style == "num"){
		allowedEng = false; //อนุญาตให้คีย์อังกฤษ
		allowedThai = false; //อนุญาตให้คีย์ไทย
		allowedNum = true; //อนุญาตให้คีย์ตัวเลข
	}

	var k = event.keyCode;

	/* เช็คตัวเลข 0-9 */
	if (k>=48 && k<=57) { return allowedNum; }

	/* เช็คคีย์อังกฤษ a-z, A-Z */
	if ((k>=65 && k<=90) || (k>=97 && k<=122)) { return allowedEng; }

	/* เช็คคีย์ไทย ทั้งแบบ non-unicode และ unicode */
	if ((k>=161 && k<=255) || (k>=3585 && k<=3675)) { return allowedThai; }
}

/*
var allowedEng = true; //อนุญาตให้คีย์อังกฤษ
var allowedThai = false; //อนุญาตให้คีย์ไทย
var allowedNum = true; //อนุญาตให้คีย์ตัวเลข
คือ หากจะไม่อนุญาติให้ใส่อะไรอันไหนเข้ามาก็เซต เปน false นะครับ ถ้าอณุญาติอันไหนก็ใส่ true ซะ ในกรณีนี้ที่เซตไว้คือ ให้ใส่ ภาษาอังกฤษ กับ ตัวเลขได้เท่านั้น ภาษาไทยจะพิมม่ายได้ค้าบมาดูตัวอย่างฟอร์มกันครับ
<form id="form1" name="form1" method="post" action="">
  <input name="text" type="text" onkeypress="return bannedKey(event)" />
  <input type="submit" name="Submit" value="Submit" />
</form>
*/

//*******************************************************************
//  Resize images
//*******************************************************************
function autosize(SENDER , MAXWIDTH , MAXHEIGHT)
{
	W = SENDER.width;
	H = SENDER.height;
	ratio = W/H;
	ratioC = MAXWIDTH/MAXHEIGHT;

	if(W>MAXWIDTH || H>MAXHEIGHT)
	{
		if(ratio>ratioC) { SENDER.width = MAXWIDTH; } else { SENDER.height = MAXHEIGHT; }
			SENDER.style.cursor = 'hand';
			SENDER.onclick = function(){window.open(SENDER.src);
		}
	}
	//<img src="aboutusPage_24.gif" onload="autosize(this , 200 , 300)">
}

//*******************************************************************
//  Other
//*******************************************************************
function calen(){
	open('calen.htm' ,'calen' ,'height=190 , width=200 , top=250 , left=250 , status=0, menubar=0 ,scrollbar=0');
}

function closeMessage(){
	messageObj.close();	
}

//*******************************************************************
//  Form field Limiter
//*******************************************************************
var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
	if (window.event&&event.srcElement.value.length>=maxlength)
		return false
	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
		var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
		if (pressedkey.test(String.fromCharCode(e.which)))
			e.stopPropagation()
	}
}

function countlimit(maxlength,e,placeholder){
	var theform=eval(placeholder)
	var lengthleft=maxlength-theform.value.length
	var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
	if (window.event||e.target&&e.target==eval(placeholder)){
	if (lengthleft<0)
	theform.value=theform.value.substring(0,maxlength)
	placeholderobj.innerHTML=lengthleft
	}
}

function displaylimit(thename, theid, thelimit){
	var theform=theid!=""? document.getElementById(theid) : thename
	var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> characters remaining on your input limit'
	if (document.all||ns6)
		document.write(limit_text)
	if (document.all){
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
		eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
	} else if (ns6){
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
	}
}

/*<form name="sampleform">

<input type="text" name="george" size=20><br>
<script>
displaylimit("document.sampleform.george","",5)
</script>
<p>z
<textarea name="john" id="johndoe" cols=25 rows=15></textarea><br>
<script>
displaylimit("","johndoe",10)
</script>

</form>*/