var domainName="http://yeuthethao.com/dudoan";
var theHttpRequestObject; 
var obj; 
var back_url;
var timeLoad=5;
// JavaScript Document
/*
Usage: This function used to detect what browser visitor using
Return:
	IE : Browser is Internet Explorer
	Mozilla : Browser is Mozilla Firefox , Netscape
*/
function DetectBrowser()
{
	if (window.XMLHttpRequest)//window.navigator.appName.toLowerCase().indexOf("netscape") > -1
	{
		return "Mozilla"; //For Firefox, Safari, Opera
	}
	else if(window.ActiveXObject)
	{
		return "IE";
	}
	else{
		//Error for an old browser
		alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
		}
}
/*
Usage: create requrest object
Return: Request object
*/
function getHttpRequestObject()
{
	var theRequestObject;
	var theBrowser;
	
	theBrowser = DetectBrowser();
	
	if (theBrowser == "IE")
	{
		theRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		theRequestObject = new XMLHttpRequest();
	}
	
	return theRequestObject;
}

function setRequestHeader(theMethod , theURL, theArgs)
{
	
	if (theMethod == "POST")
	{
		//alert(theArgs);
		theHttpRequestObject.setRequestHeader("Method", "POST " + theURL + " HTTP/1.1");
		theHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		theHttpRequestObject.setRequestHeader("Content-length", theURL.length);
	}
}

function doRequest(theMethod, theURL, theArgs, theHandledFunction)
{
	theHttpRequestObject = getHttpRequestObject();
	theURL=theURL+"&isLoadAJax";
	theHttpRequestObject.open(theMethod, theURL, true);	
	//setRequestHeader(theMethod, theURL);	
	setRequestHeader(theMethod, encodeURI(theURL), theArgs);	
	theHttpRequestObject.onreadystatechange = theHandledFunction;	
	theHttpRequestObject.send(theArgs);
}    

/* 
+theMethod : POST or GET
+theURL : url need get information
+theArgs : null if GET or id0=7 &id1=..
+theHandledFunction : function set return value
+obj: La 1 HTML TAG de set noi dung khi goi Ajax
EX :
+ doRequest('GET','<?php echo PATH_CAFE?>' + 'home/get_contact_ajax/' + this.value, null, showFriends);	
+ doRequest('POST','<?php echo PATH_CAFE?>' + 'home/update_blast_ajax', getBlastPostArgs(), saveBlast);
note: add action in array (file url_option.php):
$arrAction	= array("image","get_contact_ajax","get_city_ajax","update_blast_ajax");
*/

//---------------------------------------------------------------------------------------------------//
// Ham nay dung cho 1 so truong hop gan vao the div don gian
// Muc dich la khong phai viet lai ham ShowContentAjax
// obj: La 1 DIV TAG de set noi dung khi goi Ajax

function doNewRequest(theMethod, theURL, theArgs, obj1, back_url1)
{
	//alert(theURL);
	//alert(back_url);	
	timeLoad=timeLoad+1;
	back_url = back_url1; 
	obj = obj1;
	trVal=2;//Math.floor(Math.random()*11);
theURL=theURL+"&isLoadAJax="+timeLoad;
theURL=theURL.replace("right&isLoadAJax="+timeLoad,"right");

//alert(theURL);
//alert(timeLoad);
	theHttpRequestObject = getHttpRequestObject();
	theHttpRequestObject.open(theMethod, theURL, true);	
	setRequestHeader(theMethod,theURL, theArgs);	
	theHttpRequestObject.onreadystatechange = ShowContentAjax;	
	theHttpRequestObject.send(theArgs);
}    
function doNewRequest2(theMethod, theURL, theArgs, obj1, back_url1)
{
	//alert(theURL);
	//alert(back_url);	
	theURL=theURL;
	back_url = back_url1; 
	obj = obj1;

	theHttpRequestObject = getHttpRequestObject();
	theHttpRequestObject.open(theMethod, theURL, true);	
	setRequestHeader(theMethod,theURL, theArgs);	
	theHttpRequestObject.onreadystatechange = ShowContentAjax2;	
	theHttpRequestObject.send(theArgs);
}    

function ShowContentAjax() 
{
	if (theHttpRequestObject.readyState==4) 
	{
		if (theHttpRequestObject.status == 200) 
		{		
			if(obj!=null) 
			{		
				//alert(theHttpRequestObject.responseText);
				if(obj!=null) obj.innerHTML=theHttpRequestObject.responseText;
				if(obj.id=="div_message") 
				{
					var msg = obj.innerHTML;
					alert(Trim(msg));
				}
			
				/////////////
				$(function() {
  $('.nyroModal').nyroModal();
 // alert("nyroModal");
});
				$(document).ready(function(){
// first locate all of the select tags on the page and hide them
$("select.changeMe"+timeLoad).css('display','none');
//now, for each select box, run this function
var option = $("select.changeMe"+timeLoad);

   option.each(function(key,vals){
	var link =  $(this).attr('link').split('____');
	//if 1=link 2 = ajax
	var action = 1;
	if(link[1]==2) action=2;
	
	var curSel = $(this);
	// get the CSS width from the original select box
	var gddWidth = $(curSel).css('width');
	var gddWidthL = gddWidth.slice(0,-2);
	var gddWidth2 = gddWidthL - 28;
	var gddWidth3 = gddWidthL - 16;
	// build the new div structure
	var gddTop = '<div style="width:' + gddWidthL + 'px" class="selectME" tabindex="0"><div class="cornerstop"><div><div></div></div></div><div class="middle"><div><div><div>';
	//get the default selected option
	var whatSelected = $(curSel).children('option:selected').text();
	//write the default
	var gddFirst = '<div class="first"><span class="selectME gselected" style="width:'+ gddWidth2 +  'px;">'+ whatSelected +'</span><span id="arrowImg"></span><div class="clears"></div></div><ul class="selectME">';
	// create a new array of div options from the original's options
	var addItems = new Array();     
	if(action==2)
	var link_action_page = link[0];
	else 
	var link_action_page = link[0];
	var numOption=0;
	$(curSel).children('option').each( function() {           
		var text = $(this).text(); 
		var selVal = $(this).attr('value'); 
		var group = $(this).attr('group'); 
		var new_link = link_action_page.replace("%s%",selVal);
		 new_link = new_link.replace("%s%",selVal); new_link = new_link.replace("%s%",selVal); new_link = new_link.replace("%s%",selVal);
		 
		var before =  '<li style="width:' + gddWidthL + 'px;"><a href="javascript:void(1)" onclick="javascript:'+new_link+'" rel="' + selVal + '" tabindex="0"  style="width:' + gddWidth3 + 'px;">';
		if(group!= undefined) 
			var before =  '<li style="width:' + gddWidthL + 'px;" ><a   href="javascript:void(0)"   tabindex="0"  style="width:' + gddWidth3 + 'px;" >';

		var after = '</a></li>';           
		addItems.push(before + text + after);
		numOption=numOption+1;
	});
	if(numOption>10) //link_action_page.replace("%s%",selVal);
		gddFirst=gddFirst.replace("<ul class=\"selectME\">","<ul class=\"selectME\" style=\"height:200px;\">");
	//hide the default from the list of options 
	var removeFirst = addItems.shift();
	// create the end of the div selectbox and close everything off
	var gddBottom ='</ul></div></div></div></div><div class="cornersbottom"><div><div></div></div></div></div>'
	//write everything after each selectbox
	var GDD = gddTop + gddFirst + addItems.join('') + gddBottom;
	$(curSel).after(GDD);
	//this var selects the div select box directly after each of the origials
	var nGDD = $(curSel).next('div.selectME');
	
	$(nGDD).find('li:first').addClass("first");
	
	$(nGDD).find('li:last').addClass('last');
	//handle the on click functions - push results back to old text box
	$(nGDD).click( function(e) {
		 var myTarA = $(e.target).attr('rel');
		 var myTarT = $(e.target).text();
		 var myhrefA = $(e.target).attr('href');
		 if(myhrefA!='undefined') eval(myhrefA);
		 
		 var myTar = $(e.target);
		 //if closed, then open
		 if( $(nGDD).find('li').css('display') == 'none')
			{
					//this next line closes any other selectboxes that might be open
					$('div.selectME').find('li').css('display','none');
					$(nGDD).find('li').css('display','block');
					
					//if user clicks off of the div select box, then shut the whole thing down
					$(document.window || 'body').click( function(f) {
							var myTar2 = $(f.target);
							if (myTar2 !== nGDD) {$(nGDD).find('li').css('display','none');}
					});
							return false;
			}
			else
			{      
					if (myTarA == null){
						$(nGDD).find('li').css('display','none');
								return false;
							}
							else {
								//set the value of the old select box
								$(curSel).val(myTarA);
								//set the text of the new one
								 $(nGDD).find('span.gselected').text(myTarT);
								 $(nGDD).find('li').css('display','none');
								 return false;
							}
			}
	//handle the tab index functions
	 }).focus( function(e) {        
	 	        

		 $(nGDD).find('li:first').addClass('currentDD');
		 $(nGDD).find('li:last').addClass('lastDD');
		 function checkKey(e){
			//on keypress handle functions
			function moveDown() {
				var current = $(nGDD).find('.currentDD:first');
				var next = $(nGDD).find('.currentDD').next();
				if ($(current).is('.lastDD')){
				return false;
				} else {
					$(next).addClass('currentDD');
					$(current).removeClass('currentDD');
				}
			}
			function moveUp() {
				var current = $(nGDD).find('.currentDD:first');
				var prev = $(nGDD).find('.currentDD').prev();
				if ($(current).is('.first')){
				return false;
				} else {
					$(prev).addClass('currentDD');
					$(current).removeClass('currentDD');
				}
			}
			var curText = $(nGDD).find('.currentDD:first').text();
			var curVal = $(nGDD).find('.currentDD:first a').attr('rel');
		   switch (e.keyCode) {
				case 40:
					$(curSel).val(curVal);
					$(nGDD).find('span.gselected').text(curText);
					moveDown();
					return false;
					break;
				case 38:
					$(curSel).val(curVal);
					$(nGDD).find('span.gselected').text(curText);
					moveUp();
					return false;
					break;
				case 13:
					$(nGDD).find('li').css('display','none');
					}     
		}
		$(document).keydown(checkKey);	
	}).blur( function() {
			$(document).unbind('keydown');
	});
});
});
				/////////

			}
			
			if(back_url!=null) document.location.href = back_url;
		}
	}
	else
		if(obj!=null) 
		{	
			obj.innerHTML = '<p align=center><img src="'+ domainName +'/img/spinner.gif" alt="" border="0" align="absmiddle"> Loading...</p>';		
		}
	
}	
function ShowContentAjax2() 
{
	if (theHttpRequestObject.readyState==4) 
	{
		if (theHttpRequestObject.status == 200) 
		{		
			if(obj!=null) 
			{		
				//alert(theHttpRequestObject.responseText);
				if(obj!=null) obj.innerHTML=theHttpRequestObject.responseText;
				if(obj.id=="div_message") 
				{
					var msg = obj.innerHTML;
					alert(Trim(msg));
				}
			}
			$(function() {
  $('.nyroModal').nyroModal();
 // alert("nyroModal");
});
			if(back_url!=null) document.location.href = back_url;
		}
	}
	else
		if(obj!=null) 
		{	
			//obj.innerHTML = '<p align=center><img src="images/spinner.gif" alt="" border="0" align="absmiddle"> Loading...</p>';		
		}		
}	

//-----------------------------------------------------------------------------------------------------//


