var defaultEmptyOK = false ;
var whitespace = " \t\n\r" ;
var iEmail = "อีเมล์ไม่ถูกต้อง, กรุณากรอกอีเมล์ใหม่" ;

//============================== Begin Function checkString ===============================
function checkString(theField, sMsg, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    //alert(theField);
    if (checkString.arguments.length < 3) { emptyOK = defaultEmptyOK ; }
    if ((emptyOK == true) && (isEmpty(theField.value))) { return true ; }
    if (isWhitespace(theField.value)) 
	{
		warnEmpty(theField, sMsg) ;
       return false;
    }
    else 
	{
		return true;
	}
}
//============================== End Function checkString ===============================

//================================== Function Warn Empty Value ===============================
function warnEmpty(theField, sMsg) 
{
	   alert (sMsg);
	   var chkObj = typeof(tabTh) ;
	   if (chkObj == 'undefined')
	   {
	   		theField.focus();
	   }
	   else
	   {
	   		 var chkObj2 = typeof(f_Type) ;
			 if (chkObj2 == 'undefined')
			 {
				var fieldName = theField.name;
			 	if (fieldName.indexOf("TH") != -1 || fieldName.indexOf("tname") != -1 || fieldName.indexOf("th") != -1  || fieldName.indexOf("announce_id") != -1 )
				{
					changeTab(tabTh,tabEn,btnTabTh,btnTabEn,'th');
					theField.focus();
				}
				else
				{
					changeTab(tabTh,tabEn,btnTabTh,btnTabEn,'en');
					theField.focus();	
			    }
			}
			else
			{
				var fieldName = theField.name;
			 	if (fieldName.indexOf("TH") != -1 || fieldName.indexOf("tname") != -1 || fieldName.indexOf("th") != -1  || fieldName.indexOf("announce_id") != -1 )
				{
					changeTab(tabTh,tabEn,btnTabTh,btnTabEn,'th');
					theField.focus();
				}
				else
				{
					changeTab(tabTh,tabEn,btnTabTh,btnTabEn,'en');
					theField.focus();	
			    }
			}
	   }
	   
	    return;
}
//================================ End Function  Warn Empty Value =============================
function isWhitespace(s)
{  
	 var i;
	
	 // Is s empty?
    if (isEmpty(s)) { return true; }

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) { return false ; }
    }

    // All characters are whitespace.
    return true;
}
//=================================== End Function  isWhitespace ==============================
//================================ Function Validate Check Email ================================
function checkEmail(theField, sMsg, emptyOK)
{
		var A=0;
		var B=0;
		var P=999;
		
		 if (checkEmail.arguments.length == 1) 
		 { 
	 		emptyOK = defaultEmptyOK ;
	 	}
		
		if (((emptyOK == false) && isEmpty(theField.value)) || isWhitespace(theField.value))
		{
			warnEmpty(theField, sMsg) ;
			return false ;
		}
		
		if (theField.value.length > 0)
		{	
			for (i=0;i<theField.value.length;i++)
			{
			//	alert(document.addform.temail.value.charCodeAt(i));
				if(theField.value.substr(i,1)==" ")				//อีเมล์เป็นช่องว่าง spacebar
				{
					warnInvalid (theField, iEmail) ;
					return false;
				}// end if

				if(i==0 && theField.value.substr(i,1)=="@")				//อีเมล์ตัวแรกขึ้นต้นด้วย @
				{
					warnInvalid (theField, iEmail) ;
					return false;
				}// end if

				if(i==0  && theField.value.substr(i,1)==".")				//อีเมล์ตัวแรกขึ้นต้นด้วย .
				{
					warnInvalid (theField, iEmail) ;
					return false;
				}// end if
				
				if(theField.value.substr(i,1)=="@")
				{
					A=A+1;
					if(theField.value.substr(i+1,1)=="." || theField.value.substr(i-1,1)==".")			// อีเมล์ที่มี . อยู่ติดกับ @
					{
						warnInvalid (theField, iEmail) ;
						return false;
					}// end if
				}// end if

				if(theField.value.substr(i,1)==".")
				{
					B=B+1;
				}// end if
				
				//alert(document.addform.temail.value.charCodeAt(i));
				if ((theField.value.charCodeAt(i)<48 || theField.value.charCodeAt(i)>57) && (theField.value.charCodeAt(i)<65 || theField.value.charCodeAt(i)>90)&&(theField.value.charCodeAt(i)<97 || theField.value.charCodeAt(i)>122))
				{		
					if (theField.value.charCodeAt(i) !=64 &&  theField.value.charCodeAt(i) !=46 && theField.value.charCodeAt(i) !=95 && theField.value.charCodeAt(i) !=45)
					{
						//alert(document.addform.temail.value.charCodeAt(i));
						warnInvalid (theField, iEmail) ;
						return false;
					}// end if
				}// end if

		  }// end for

			if ((A>1) || (A<1 || B<1))
			{
				warnInvalid (theField, iEmail) ;
				return false;
				
			}// end if
			return true;
		}// end if
}
//=============================  End Function Validate Check Email ===============================
//================================ Begin Function Warn Invalid Value ============================
function warnInvalid(theField, sMsg)
{   
	alert(sMsg);
	theField.focus();
    theField.select();
    return ;
}
//================================ End Function Warn Invalid Value ===============================

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0));
}
//================================ End Function Validate Data Empty ============================
function isEmail (s)
{   
	if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
// =======================================================================================
function NewWin(art_id) {  //dev
	//alert('test123');
	var url = "print_content.asp?art_id="+art_id;
  newWin=window.open(url,'regwin','width=595,height=407,toolbar=0,scrollBars=1,resizable=0,left=195,top=113')
}
function NewWinToFriend(art_id) {  //dev
	//alert('test123');
	var url = "emailto_friend.asp?art_id="+art_id;
  newWin=window.open(url,'regwin_email','width=400,height=250,toolbar=0,scrollBars=1,resizable=0,left=195,top=113')
}

function Validate_Data_frmRemove(objForm,objChkbox)
{

	var dForm = document.forms[objForm.name] ;
	var errEmptyDeleteBox = "กรุณาเลือกรายการที่ต้องการ Remove" ;
	var msgConfirmDelete = "ต้องการจะ Remove จริงหรือไม่?" ;
	var delBoxName = objChkbox ;
	

	if (!checkStatusCheckbox( objForm, delBoxName, errEmptyDeleteBox ))
	{ 
		return false ; 
	}
	else
	{
		if( confirm(msgConfirmDelete) == true )
		{
			return true ;
		}
		else
		{
			return false ; 
		}
	}
		
}
//================================ End Function Validate Check Form Delete Content =============================
function ContentDetail(strFileName) {  //dev
	//alert('test123');
	var url = strFileName;
  newWin=window.open(url,'regwinDetail','width=595,height=407,toolbar=0,scrollBars=1,resizable=0,left=195,top=113')
}

function OrderPrint() {  //dev
	//alert('test123');
	//var url = strFileName;
  newWin=window.open('order_print.php','order_print','width=595,height=407,toolbar=0,scrollBars=1,resizable=0,left=195,top=113')
}

//=====================================================================================================
function displayImage( objImg ) 
{
	if ( objImg.style.display == 'none' )
	{
		objImg.style.display = '' ;
	}
}
//=====================================================================================================

//=====================================================================================================
function swapMultiImage( form, fileField, objImg, intMaxWidthImage ) 
{
	var myImage ;
	var dForm = document.forms[form.name] ;
	myImage = new Image() ;
	myImage.src = fileField.value ;
	//alert("PreView ==> "+objImg.width);
	//alert("New Pic ==> "+myImage.width);
	objImg.width = myImage.width
	objImg.src = myImage.src ;
	if (myImage.width < intMaxWidthImage) {
		intMaxWidthImage = myImage.width
	} 
	resizeImage( objImg, intMaxWidthImage )  ;
	//return true ;
}
//=====================================================================================================

//=====================================================================================================
function resizeImage( objImg , intMaxWidthImage ) 
{
	var imageWidth ;
	imageWidth = objImg.width ;

	//alert( objImg.name + "old ==> " + objImg.width);
	if ( resizeImage.arguments.length < 2 ) { intMaxWidthImage = 200 ; }
	
	if( imageWidth == 0 ){ objImg.width = intMaxWidthImage; }
	i = 0;
	total = 1000000 ;
	while ( imageWidth == -1 && i < total  )
	{
		imageWidth = objImg.width;
		i++;
	}

	if ( intMaxWidthImage < imageWidth )
	{
		objImg.width = intMaxWidthImage ;
	}
	
	return true ;
}
//=====================================================================================================
function callPopupWindows(formTextVal) 
{		
		var strTextVal = eval("document."+formTextVal+".value;");
		var	popup = window.open('../html_editor/html_editor.asp?formTextVal='+formTextVal,'popup','width=650,height=420,resizable=yes,status=yes');
		//alert(self.parent.frames.length);
		//if( self.parent.frames.length != 0 )
		//{ 
		//	eval("parent.leftFrame.document.formA.mycontent.value = document."+formTextVal+".value;") ;
		//	parent.leftFrame.document.formA.myback.value = formTextVal ;
		//} 
		//else 
		//{ 
		//eval("window.opener.document.saveform.content.value = "+strTextVal+";");
		//alert(popup);
		/*for (poperty in document) {
			document.write('name = >'+poperty+' value ==>'+document.poperty+'<hr>')
		}  */
		//window.opener.document.iView.boby.innerHTML = strTextVal;
		//window.opener.document.saveform.content.value = strTextVal ;
		//}
		//alert(strTextVal);
		//alert(window.document.saveform);
		//writeFile('doc.htm',strTextVal);
}
//=====================================================================================================

function writeFile(fileName, data) {
    // The following calls require Object Signing
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalPropertyRead");
    var userHome = java.lang.System.getProperty("user.home");
    var fileSeparator = java.lang.System.getProperty("file.separator");
    var fileName = (fileName || 'myFile.txt');
    
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess");
    var raf = new java.io.RandomAccessFile(fileName, "rw");
    raf.seek(raf.length());
    raf.writeUTF(Date() + "::" + data + "\n");
    raf.close();
    java.lang.System.out.println(Date() + "::" + data + "\n");
    //-->
}

