
var _DEBUG_ = "off";

var AJAX_ROUND     	= 0;
var AJAX_LONG 		= 1;
var AJAX_LONG_RED 	= 2;
var AJAX_CLOCK 		= 3;
var AJAX_UPLOAD 	= 4;
var AJAX_DOTS       = 5;
var BASE_URL		= 'http://www.thaifacepic.com/';


function Error( FunctionName, Err )
{
    if( _DEBUG_ == "on")
        alert( 'ERROR ' + FunctionName + ' - ' + Err );
	else
		alert('There was a problem with this request. Please try again!');
}


/* 
This function should always be called from within a try catch block.
it will throw the exception if one occurs. 

It will return a list of cbName.values that have been checked from within the FormName.
*/
function GetChecked( FormName, cbName )
{
	var total = '0';
	try
	{
		if( !document[FormName][cbName] )
		{
			return total;
		}
		var DoCheck = true;
		
		for(var i=0; i < document[FormName][cbName].length; i++)
		{
			DoCheck = false;
			if(document[FormName][cbName][i].checked)
			{
				total += ','+document[FormName][cbName][i].value;
			}
		}
		// For when there is only one element.
		if(DoCheck && document[FormName][cbName].checked)
		{
			total += ',' + document[FormName][cbName].value;
		}
	}
	catch( err )
	{
		throw( err );	
	}
	return total;

}

function ShowLoader( id, Type )
{
	if( IdExists( id ) )
	{
		switch( Type )
		{
			case AJAX_ROUND     : 
				SetDivIdHtml(id, '<img src="/images/ajax-loader-round.gif"/>' ); 
				break;	
			case AJAX_LONG 		: 
				SetDivIdHtml(id, '<img src="/images/ajax-loader.gif"/>' ); 
				break;	
			case AJAX_LONG_RED 	: 
				SetDivIdHtml(id, '<img src="/images/ajax-loader-red.gif"/>' ); 
				break;	
			case AJAX_CLOCK 	: 
				SetDivIdHtml(id, '<img src="/images/ajax-loader-clock.gif"/>' ); 
				break;	
			case AJAX_UPLOAD 	: 
				SetDivIdHtml(id, '<img src="/images/ajax-loader.gif"/>' ); 
				break;	
			case AJAX_DOTS      :
				SetDivIdHtml(id, '<img src="/images/ajax-loader-white.gif"/>' ); 
				break;
			default : 
				SetDivIdHtml(id, '<img src="/images/ajax-loader.gif"/>' ); 
				break;	
		}
			
	}
}

function LoadImg( Img )
{
	
}

function ResponseError( txt )
{
	// TYPE:MESSAGE
	// i.e
	// ERROR:OH SHIT...
	if(txt)
		if( txt.substring(0, 5) == "ERROR" )
		{
			return "Big Fat Error";			
		}	
	return '';
}

function ReloadFrame( FrameId, Page )
{
    try
    {
        if( Page == null )
	        window.frames[ FrameId ].location.reload();
	    else
            window.frames[ FrameId ].location.href = Page; 
	}
	catch( err )
    {
        Error(arguments.callee.name,  FrameId + ' - ' + Page + ':' + err );
	}
}


function IsCheckbox( Obj )
{
	if( Obj.type == "checkbox" )
		return true;
	return false;
}
function IdExists( id )
{
	if (document.layers) 
    { // NN4+
		return(document.layers[id]);
	} 
	else if (document.getElementById) 
	{ // gecko(NN6) + IE 5+
	    return (document.getElementById(id));
	} 
	else if (document.all) 
	{ // IE 4
		return(document.all[id]);
    }
	
}

function SetDivIdHtml(id, Val) 
{
	try
	{
		if (document.layers) 
    	{ // NN4+
			document.layers[id].innerHTML = Val;
	    } 
	    else if (document.getElementById) 
	    { // gecko(NN6) + IE 5+
	        document.getElementById(id).innerHTML = Val;
	    } 
	    else if (document.all) 
	    { // IE 4
			document.all[id].style.innerHTML = Val;
    	}
	}
	catch( err )
	{
		Error(arguments.callee.name, id + ' - ' + Val );
	}

}



function GetDivIdHtml(id) 
{
	var Val = "ERROR";
	if (document.layers) 
    { // NN4+
		Val = document.layers[id].innerHTML;
    } 
    else if (document.getElementById) 
    { // gecko(NN6) + IE 5+
        Val = document.getElementById(id).innerHTML;
    } 
    else if (document.all) 
    { // IE 4
		Val = document.all[id].style.innerHTML;
    }
	return Val;

}

function SetDivIdBgColor(id, Val ) 
{
    try
    {
        if (document.layers) 
        { // NN4+
            document.layers[id].style.backgroundColor = Val;
        } 
        else if (document.getElementById) 
        { // gecko(NN6) + IE 5+
            document.getElementById(id).style.backgroundColor = Val;
        } 
        else if (document.all) 
        { // IE 4
            document.all[id].style.backgroundColor = Val;
        }
	}
	catch( err )
    {
        Error( arguments.callee.name, 'id=' + id + ', Val= ' + Val + ' : ' + err );	
	}
	
}



function SetDivIdVal(id, Val) 
{
    try
    {
        if (document.layers) 
        { // NN4+
            document.layers[id].value = Val;
        } 
        else if (document.getElementById) 
        { // gecko(NN6) + IE 5+
            document.getElementById(id).value= Val;
        } 
        else if (document.all) 
        { // IE 4
            document.all[id].style.value = Val;
        }
	}
	catch( err )
    {
        Error( arguments.callee.name, ' id=' + id + ', Val= ' + Val + ' : ' + err );	
	}
	
}

function GetDivIdObj(id)
{
	try
	{
		var Obj = null;
		if (document.layers) 
    	{ // NN4+
			Obj = document.layers[id];
    	} 
    	else if (document.getElementById) 
    	{ // gecko(NN6) + IE 5+
        	Obj = document.getElementById(id);
    	} 
   	 	else if (document.all) 
    	{ // IE 4
			Obj = document.all[id];
    	}
	}
	catch( err )
	{
		  Error( arguments.callee.name, ' id=' + id + ' ' + err ); 
	}
	return Obj;
	
}

function GetDivIdVal(id) 
{
	try
	{
		var Val = "ERROR";
		if (document.layers) 
    	{ // NN4+
			Val = document.layers[id].value;
    	} 
    	else if (document.getElementById) 
    	{ // gecko(NN6) + IE 5+
        	Val = document.getElementById(id).value;
    	} 
   	 	else if (document.all) 
    	{ // IE 4
			Val = document.all[id].style.value;
    	}
	}
	catch( err )
	{
		  Error( arguments.callee.name, ' id=' + id + ' ' + err ); 
	}
	return Val;
}



function HideAllDivs()
{
	try
	{
		if (!document.getElementsByTagName) 
		{
			alert('Your browser doesn\'t support features of this page.  Please consider updating you browser..');
			return false;
		}
		var divs = document.getElementsByTagName("div");
		for(var i=0; i < divs.length; i++)
		{
			var div = divs[i];
			var id  = div.id;

			if ( id.substring(0, 5) == "panel")
			{
				div.style.display = "none";
			}
		}
	}
	catch( err )
	{
		 Error( arguments.callee.name, err ); 
	}
	return true;

}




//
function ToggleDiv(id, Val) 
{

	try
	{
		if( !IdExists( id ) )
		{
			throw( ' Id : ' + id + ' doesnt exist' );
			
		}
	
		
		if (document.layers) 
		{ // NN4+
			if (Val == 'h') 
			{
				document.layers[id].visibility = "hide";
				document.layers[id].display = "none";
			} 
			else 
			{
				document.layers[id].visibility = "show";
				document.layers[id].display = "block";
			}
		} 
		else if (document.getElementById) 
		{ // gecko(NN6) + IE 5+

			var obj = document.getElementById(id);
		   
			if (Val == 'h') 
			{
				obj.style.visibility = "hidden";
				obj.style.display = "none";
			} 
			else 
			{
				obj.style.visibility = "visible";
				obj.style.display = "block";
			}
			
			
		} 
		else if (document.all) 
		{ // IE 4
			if (Val == 'h') 
			{
				document.all[id].style.visibility = "hidden";
				document.all[id].style.display = "none";
			} 
			else 
			{
				document.all[id].style.visibility = "visible";
				document.all[id].style.display = "block";
			}
		}
	}
	catch( err )
	{
		 Error( arguments.callee.name, err ); 	
	}
}
function ajaxObject(url, callbackFunction) 
{  
	try
	{
		var that=this;        
		this.updating = false;  
		this.abort = function() 
		{    
			if (that.updating) 
			{      
				that.updating=false;      
				that.AJAX.abort();      
				that.AJAX=null;    
			} 
		}  
		this.update = function(passData,postMethod) 
		{     
			if (that.updating) 
			{ 
				return false; 
			}    
			that.AJAX = null;                              
			if (window.XMLHttpRequest) 
			{                    
				that.AJAX=new XMLHttpRequest();                  
			} 
			else 
			{                                        
				that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");    
			}                                                 
			if (that.AJAX==null) 
			{                                   
				throw( 'AJAX is null' );
			} 
			else
			{
				that.AJAX.onreadystatechange = function() 
				{          
					if (that.AJAX.readyState==4) 
					{                       
						that.updating=false;
						that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
						that.AJAX=null; 
					}
				}
				that.updating = new Date();
				if (/post/i.test(postMethod)) 
				{
					var uri=urlCall+'?'+that.updating.getTime();
					that.AJAX.open("POST", uri, true);
					that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					that.AJAX.setRequestHeader("Content-Length", passData.length);
					that.AJAX.send(passData);      
				} 
				else 
				{
					var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());         
					that.AJAX.open("GET", uri, true);                                     
					that.AJAX.send(null);                                               
				}
				return true;                                                 
			}                                                                             
	   }  
	   var urlCall = url;          
	   this.callback = callbackFunction || function () { };
	}
	catch( err )
	{
		throw( err );
	}
}


function HideLoading( id )
{
	if( IdExists( id ) )
	{
		GetDivIdObj( id ).style.display = '';
		
	}
	if( IdExists( id + 'Loading' ) )
	{
		GetDivIdObj( id+'Loading'  ).style.display = 'none';
	}
}

function ShowLoading( id )
{
	if( IdExists( id ) )
	{
		GetDivIdObj( id ).style.display = 'none';
	}
	if( IdExists( id + 'Loading' ) )
	{
		GetDivIdObj( id+'Loading' ).style.display = '';	
	}
}


function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function OpenWindow( mylink, scrollBar )
{
    // Center on screen
	try
	{
		var w = 1024;
		var h = 2000;
    	if (screen)
   	 	{
			if( w > screen.width )
				w = screen.width;
			if( h > screen.height )
				h = screen.height;
        	y = (screen.availHeight - h)/2;
        	x = (screen.availWidth - w)/2;

    	}
  		var w=window.open(mylink
					  	,null
					  	,"height="+h+",width="+w+",status=yes,toolbar=no,menubar=no,location=no,scrollbars="+scrollBar+",resizable=yes");
		w.focus();
	}
	catch( err )
	{
		Error( arguments.callee.name,  mylink );
	}
	
    return false;
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) 
{
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) 
	{
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) 
	{
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) 
		{
			radioObj[i].checked = true;
		}
	}
}

	
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) 
	{
		if(radioObj[i].checked) 
		{
			return radioObj[i].value;
		}
	}
	return "";
}


function LangBar()
{
	if( GetDivIdObj( 'dropDownLang' ).style.display == 'block' )
		ToggleDiv( 'dropDownLang', 'h' );
	else
		ToggleDiv( 'dropDownLang', 's' );
		
		
	return false;		
}

 


