// Main function object fade
nereidFadeObjects = new Object();
nereidFadeTimers = new Object();

function nereidFade(object, destOp, rate, delta){
if (!document.all)
return
    if (object != "[object]"){  //do this so I can take a string too
        setTimeout("nereidFade("+object+","+destOp+","+rate+","+delta+")",0);
        return;
    }
        
    clearTimeout(nereidFadeTimers[object.sourceIndex]);
    
    diff = destOp-object.filters.alpha.opacity;
    direction = 1;
    if (object.filters.alpha.opacity > destOp){
        direction = -1;
    }
    delta=Math.min(direction*diff,delta);
    object.filters.alpha.opacity+=direction*delta;

    if (object.filters.alpha.opacity != destOp){
        nereidFadeObjects[object.sourceIndex]=object;
        nereidFadeTimers[object.sourceIndex]=setTimeout("nereidFade(nereidFadeObjects["+object.sourceIndex+"],"+destOp+","+rate+","+delta+")",rate);
    }
}

// Menu fade
function FadeOn(obj){ nereidFade(obj,0,0,50) }
function FadeOut(obj){ nereidFade(obj,100,0,50) }

// Banner rotate
function Banner_rotate(Btype,adBanner,adLink) {
  var currentAd = eval("currentAd"+Btype);
  if (currentAd == eval("imgs"+Btype+".length")) { currentAd = 0; }
  nereidFade(eval(adBanner),0,0,50); 
  eval(adBanner).src=eval("imgs"+Btype+"[currentAd]");
  eval(adBanner).alt=eval("alt"+Btype+"[currentAd]");
  eval(adLink).href=eval("lnks"+Btype+"[currentAd]");
  eval(adLink).target=eval("target"+Btype+"[currentAd]");
  nereidFade(eval(adBanner),100,100,5);
  eval("currentAd"+Btype+"= currentAd+1" );  
}

/* Modified to support Opera */ 
function bookmarksite(title,url){ 
  if (window.sidebar) // firefox 
    window.sidebar.addPanel(title, url, ""); 
  else if(window.opera && window.print){ // opera 
    var elem = document.createElement('a'); 
    elem.setAttribute('href',url); 
    elem.setAttribute('title',title); 
    elem.setAttribute('rel','sidebar'); 
    elem.click(); 
  } 
  else if(document.all)// ie 
    window.external.AddFavorite(url, title); 
} 

//  Hide & Show layer Function
var timer;
function HideShow(layer,status){
  if (status=="show"){ state='visible'; timer=0; }else{ state='hidden'; timer=200; }
  setTimeout(eval("document.all."+layer+".style.visibility = state"), timer);
}

//##  Check input numberic only   ##
function Filter_Numberic() {
  var keycode = window.event.keyCode;
  if( keycode >= 37 && keycode <= 40 ) return true;  // arrow left, up, right, down  
  if( keycode >= 48 && keycode <= 57 ) return true;  // key 0-9
  if( keycode >= 96 && keycode <= 105 ) return true;  // numpad 0-9
  if( keycode == 110 || keycode == 190  ) return true;  // dot
  if( keycode == 8  ) return true;  // backspace
  if( keycode == 9 ) return true;  // tab
  if( keycode == 13 ) return true;  // enter
  if( keycode == 45 ||  keycode == 46 || keycode == 35 || keycode == 36) return true;  // insert, del, end, home
  return false;
}

function Filter_Integer() {
  var keycode = window.event.keyCode;
  if( keycode >= 37 && keycode <= 40 ) return true;  // arrow left, up, right, down  
  if( keycode >= 48 && keycode <= 57 ) return true;  // key 0-9
  if( keycode >= 96 && keycode <= 105 ) return true;  // numpad 0-9
//  if( keycode == 110 || keycode == 190  ) return true;  // dot
  if( keycode == 8  ) return true;  // backspace
  if( keycode == 9 ) return true;  // tab
  if( keycode == 13 ) return true;  // enter
  if( keycode == 45 ||  keycode == 46 || keycode == 35 || keycode == 36) return true;  // insert, del, end, home
  return false;
}

//##  Send var to get method  ##
function SetPage(obj_name,obj_value){
  var PageNo = obj_name+"=";
  var url = new String( window.location );
  var newurl = url.substring(url.lastIndexOf("/")+1,url.length);

  if (newurl.indexOf("#")>0)
  {    
	var exitS = "N";
    var startS = newurl.indexOf("#")-1;
	var charS = newurl.substring(0,newurl.indexOf("#"));
	do {
      if (newurl.charAt(startS+1)=="?" || newurl.charAt(startS+1)=="&" || startS+1==newurl.length) {exitS="Y";} 
	  startS++;
	} while (exitS=="N");
	charS += newurl.substring(startS,newurl.length);
	newurl = charS;
  } 

  if (url.indexOf("?")>0){
    if (newurl.indexOf(PageNo)<0){
	  newurl += "&"+PageNo+obj_value;
	}else{
	  var getdata = "";
	  var arraydata = newurl.substring(newurl.indexOf("?")+1,newurl.length).split( "&" );
      for( i=0; i<arraydata.length; i++ ){ 
	    if (arraydata[i].indexOf(PageNo)<0){
	      getdata += "&"+arraydata[i];
		}else{
		  getdata += "&"+PageNo+obj_value
		}
      }	  
	  getdata = getdata.substring(1,getdata.length);
	  newurl = newurl.substring(0,newurl.indexOf("?")+1)+getdata;	  
	}	
  }else{
	newurl += "?"+PageNo+obj_value;
  }
  location.href=newurl;  
}

//  Paging
function GoToPage(pageno,order){
  var PageNo = "";
  if(order==undefined){PageNo = "pageno=";}else{PageNo = "pageno"+order+"=";}
  var url = new String( window.location );
  var newurl = url.substring(url.lastIndexOf("/")+1,url.length);

  if (newurl.indexOf("#")>0)
  {    
	var exitS = "N";
    var startS = newurl.indexOf("#")-1;
	var charS = newurl.substring(0,newurl.indexOf("#"));
	do {
      if (newurl.charAt(startS+1)=="?" || newurl.charAt(startS+1)=="&" || startS+1==newurl.length) {exitS="Y";} 
	  startS++;
	} while (exitS=="N");
	charS += newurl.substring(startS,newurl.length);
	newurl = charS;
  } 

  if (url.indexOf("?")>0){
    if (newurl.indexOf(PageNo)<0){
	  newurl += "&"+PageNo+pageno;
	}else{
	  var getdata = "";
	  var arraydata = newurl.substring(newurl.indexOf("?")+1,newurl.length).split( "&" );
      for( i=0; i<arraydata.length; i++ ){ 
	    if (arraydata[i].indexOf(PageNo)<0){
	      getdata += "&"+arraydata[i];
		}else{
		  getdata += "&"+PageNo+pageno
		}
      }	  
	  getdata = getdata.substring(1,getdata.length);
	  newurl = newurl.substring(0,newurl.indexOf("?")+1)+getdata;	  
	}	
  }else{
	newurl += "?"+PageNo+pageno;
  }
  location.href=newurl;    
}

//##  Popup window & Dialog   ##
function PopupWindow(url,w,h){
  var Cwidth = parseFloat(screen.availWidth/2)-(w/2);
  var Cheight = parseFloat(screen.availHeight/2)-(h/2);
  return  window.open(url,'','width='+w+',height='+h+',top='+Cheight+',left='+Cwidth+',scrollbars=yes');
}

function PopupDialog(url,w,h){
  return  window.showModalDialog(url,window,'dialogWidth:'+w+'px;dialogHeight:'+h+'px;status:0;help:no;');
}

function Wpop(url,img,w,h){
  var PopupMode = "1"; // 0: Popup window, 1: Dialog window
  if (window.navigator.appVersion.indexOf("MSIE")==-1) {PopupMode="0";} // if browser is firefox or not IE

  if(PopupMode=="0") {
    if(img=="yes"){url="ShowImgPopup.asp?im="+url;w=parseInt(w)+16; h=parseInt(h)+4;} 
    PopupWindow(url,w,h);
  }else{
    if(img=="yes"){url="ShowImgPopup.asp?im="+url;w=parseInt(w)+6; h=parseInt(h)+28;} 
    PopupDialog(url,w,h);    
  }
}

function OpenerReloadAndClose(msg){
  if (opener) {opener.location.reload();}
  else {var xWin=window.dialogArguments; xWin.location.replace(xWin.location);}
  if (msg != '') {alert(msg);}
  self.close();
}

// ##  Validate email  ##
function chkemail(o1){
  //var obj = document.all(o1);
  var obj = o1;
  if(obj.value=="") return true;
  var apos=obj.value.indexOf("@");
  var dotpos=obj.value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2) {
    alert("You enter email format incorrect.");
	obj.focus(); return false;
  } else {return false;}
}

// ##  Validate form ##
function ValidateForm(f,Field,Display){
  for (i=0;i<Field.length;i++){
    if (f[eval("Field[i]")].value=="") {alert("กรุณากรอก " +Display[i]+ " ด้วยค่ะ"); f[eval("Field[i]")].focus(); return false;}
  }
  return true;				     
}

function PrintTag(elementId){
var printContent = document.getElementById(elementId).innerHTML;
 var windowUrl = 'about:blank';
 var uniqueName = new Date();
 var windowName = 'Print' + uniqueName.getTime();
 var printWindow = window.open(windowUrl, windowName, 'left=0,top=0,width=10,height=10');

 printWindow.document.write("<link href='css/text.css' rel='stylesheet' type='text/css'>");
 printWindow.document.write(printContent);
 printWindow.document.close();
 printWindow.focus();
 printWindow.print();
 printWindow.close();
}

function DDSelect(obj,chk){	
  for(i=0;i<obj.length;i++){
    if (obj[i].value==chk) { obj[i].selected=true; break; }
  }
}

//##  Move Check box data  ##
function CheckDatabox(show,show_data){
  show_data.value = ""
  for(i=0;i<show.length;i++){
    if (show[i].checked){show_data.value+=show[i].value+","}
  }
}

function LimitDatabox(obj,limit,msg){
  var count_checked = 0;
  for(i=0;i<obj.length;i++){
    if (obj[i].checked) {count_checked++}
  }
  if(count_checked>limit){alert(msg); return false;}else{return true;}
}

// ##  Show hide table ##
function ShowTable(data){
  data.style.visibility='visible';
  data.style.position='static';
}
function HideTable(data){
  data.style.visibility='hidden';
  data.style.position='absolute';
}

// ## Popup float ##
  // Pop up float
  document.writeln('<div id="FLayer2" style="visibility:hidden;position:absolute; filter:alpha(opacity=50); width:100%; height:100%; z-index:1; background-color: #000000; layer-background-color: #000000; border: 1px none #000000;left:0;right:0;"></div>');
  document.writeln('<div id="FLayer1" style="visibility:hidden;position:absolute; width:100px; height:30px; z-index:2; background-color: #000000; layer-background-color: #000000; border: 1px none #000000; "> <table width="100" height="30" border="0" cellspacing="1" cellpadding="2" bgcolor="#000000"><tr><td bgcolor="#FFFFFF" align="center"  onclick="FloatImg_close();"><img name="Lim" alt=""> <br><div align="right"><a href=#><font color=#112755 size=1>Close</font>&nbsp;<img src="images/misc/close.gif"  hspace="2" border=0 align="absmiddle" ></a></div></td></tr></table></div>');

function FloatImg(im,alt){
  document.all.Lim.src=im;  
  if(alt == undefined || alt==null || alt=="") {alt="Click to close."} document.all.Lim.alt=alt;
  HideShow('FLayer2','show');  HideShow('FLayer1','show');
  document.all.FLayer1.style.left=parseFloat(screen.availWidth/2)-(document.all.Lim.width/2);
  var Top = parseFloat(screen.availHeight/2)-(document.all.Lim.height/2);
  if(Top>250){Top=250}
  document.all.FLayer1.style.top=Top;
}
function FloatImg_close(){
  HideShow('FLayer2','hide');  HideShow('FLayer1','hide');
}

// ## Image thumbnail ##
function resize(which, max) {	
  var elem = which; //document.getElementById(which);
  if (elem == undefined || elem == null) return false;
  if (max == undefined) max = 100;
  if (elem.width > elem.height) {
    if (elem.width > max) elem.width = max;
  } else {
    if (elem.height > max) elem.height = max;
  }
}

// Switch lang
function GoogleTranslate(lang){
  if(lang=="th") {top.location.href="http://www.foodietaste.com/"}
  else{
    var url = new String( window.location );
    url = url.replace("?","%3F");
    url = url.replace("=","%3D");
    top.location.href="http://translate.google.co.th/translate?hl=th&sl=th&tl="+lang+"&u="+url;
  }
}

