// JavaScript Document - made by Quangmd@g3ws.com
MyStatusActive = 0;

function getobj(name) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		return document.getElementById(name)
	}
	else {	
		if (document.layers) { // Netscape 4
			return document(name)
		}
		else { // IE 4
			return document.all(name)
		}
	}
}

function setObjHTML(obj, val) {
	tmp = getobj(obj);
	
	if (tmp.innerHTML)
		tmp.innerHTML = val;
	else 
		tmp.value = val;
	
}

function setObjText(obj, val) {
	tmp = getobj(obj);
	
	if (tmp.innerText)
		tmp.innerText = val;
	else if (tmp.textContent)
		tmp.textContent = val;
	else
		tmp.text = val;
	
}

function mystatus(st) {
	if (MyStatusActive==1) {
		var tmp = getobj('mystatusbar');
		mystatusbar.innerHTML = st + '<br>' + mystatusbar.innerHTML;
	}
}

function GetXMLHttpObject()
{
      var oXMLHttp = null;

      try
      {
            oXMLHttp = new ActiveXObject("MSXML2.XMLHTTP");
      }
      catch (E)
      {
            try
            {
                  oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E)
            {
              oXMLHttp = null;
            }
      }

      if ((oXMLHttp == null) && (typeof(XMLHttpRequest) != 'undefined'))
      {
            oXMLHttp = new XMLHttpRequest();
      }

      return oXMLHttp;
}

function getAjaxVal(sURL, objID, valname,nocache)
{
		//NO CACHE
		if (sURL.indexOf('?')==-1) {
			sURL = sURL + '?';
		}
		if (nocache != true)
			sURL = sURL + '&nocache=' + Math.random();
		
		if (valname==null)
			valname = 'value';

	var obj;
	obj = getobj(objID);
	
	eval('obj.' + valname + ' = "please wait ..."');
	
	var oXMLHttp = GetXMLHttpObject();      

      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {
						obj = getobj(objID);
						
						if (obj!=null) {
							//some time IE crash if using innerHTML - Unknown runtime error
							oXMLHttp.responseText.replace('"','\"');
							eval('obj.' + valname + ' = oXMLHttp.responseText;');

						} else {
							mystatus('No HTML target box ' + objID);
						}
						
						mystatus('Done fetching value into ' + objID);	
                  }
            }			
	  
	  oXMLHttp.open("GET", sURL, true);
	  
      oXMLHttp.send(null);
	  mystatus('Using document : ' + sURL);
	  
	  return oXMLHttp;
	  
	  return false;
}

function RefreshContent(sDivId) {
	tmp = getobj(sDivId);
	
	if (tmp.dynamicURL) {
		LoadContent(tmp.dynamicURL, sDivId);
	}
}

function LoadContent(sURL, sDivId, zeroWH, nowaittingbox, nodisplay)
{
		//NO CACHE
		if (sURL.indexOf('?')==-1) {
			sURL = sURL + '?';
		}
		
		//unmark if we want to prevent caching
		//sURL = sURL + '&nocache=' + Math.random();
	
			tmp = getobj(sDivId);
			//tmp.title = tmp.title + ' (Ajax Loaded ...)'; 
			tmp.dynamicURL = '';
			tmp.dynamicURL = sURL;		
	
      var oXMLHttp = GetXMLHttpObject();	  

      oXMLHttp.open("GET", sURL, true);

      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {
					  	mystatus('Ready state for transfering result');
						obj = getobj(sDivId);
						
						if (obj!=null) {
							//some time IE crash if using innerHTML - Unknown runtime error
							obj.innerHTML = oXMLHttp.responseText;

							mystatus('store URL of Div (' + sDivId + ')- ajax holder ' + obj.dynamicURL);
							
							//change the window title
							changetitle();
						} else {
							mystatus('No HTML target box ' + sDivId);
						}
						
						if (!nodisplay) {
							hideitembyname(sDivId);
							showitembyname(sDivId);
						}
						if (zeroWH) {
							divclose(sDivId);
						}
						mystatus('Done fetching document into ' + sDivId);	
                  }
            }			
	  
	  if (!nowaittingbox) {
		  //default is show the waitting box
		  	obj = getobj(sDivId);
			if (obj!=null) {						
				mystatus('make waitting animation');
				obj.innerHTML = '<img src="/images/loading.gif" />';
			} else {
				mystatus('No waiting box for ' + sDivId);
			}			
	  }

      oXMLHttp.send(null);
	  mystatus('Using document : ' + sURL);
}

function PostLoadContent(sURL, sParams, sDivId, zeroWH, nowaittingbox, nodisplay)
{

			tmp = getobj(sDivId);
			//tmp.title = tmp.title + ' (Ajax Loaded ...)'; 
			tmp.dynamicURL = '';
			tmp.dynamicURL = sURL;		
	
      var oXMLHttp = GetXMLHttpObject();	  

      oXMLHttp.open("POST", sURL, true);

      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {
					  	mystatus('Ready state for transfering result');
						obj = getobj(sDivId);
						
						if (obj!=null) {
							//some time IE crash if using innerHTML - Unknown runtime error
							obj.innerHTML = oXMLHttp.responseText;

							mystatus('store URL of Div (' + sDivId + ')- ajax holder ' + obj.dynamicURL);
							
							//change the window title
							changetitle();
						} else {
							mystatus('No HTML target box ' + sDivId);
						}
						
						if (!nodisplay) {
							hideitembyname(sDivId);
							showitembyname(sDivId);
						}
						if (zeroWH) {
							divclose(sDivId);
						}
						mystatus('Done fetching document into ' + sDivId);	
                  }
            }			
	  
	  if (!nowaittingbox) {
		  //default is show the waitting box
		  	obj = getobj(sDivId);
			if (obj!=null) {						
				mystatus('make waitting animation');
				obj.innerHTML = '<img src="/images/loading.gif" />';
			} else {
				mystatus('No waiting box for ' + sDivId);
			}			
	  }

		//Send the proper header infomation along with the request
		oXMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		oXMLHttp.setRequestHeader("Content-length", sParams.length);
		oXMLHttp.setRequestHeader("Connection", "close");
	
		oXMLHttp.send(sParams);

	  mystatus('Using document : ' + sURL);
}

function create_request_string(theform) 
{ 
	var reqStr = ""; 
	
	for(i=0; i < theform.elements.length; i++) 
	{ 
		isFormObject = false; 
		
		switch (theform.elements[i].tagName) 
		{ 
		case "INPUT": 
		
		switch (theform.elements[i].type) 
		{ 
			case "text": 
			case "hidden": 
				reqStr += theform.elements[i].name + "=" + encodeURIComponent(theform.elements[i].value); 
				isFormObject = true; 
			break; 
		
			case "checkbox": 
			if (theform.elements[i].checked) 
			{ 
				reqStr += theform.elements[i].name + "=" + theform.elements[i].value; 
			}else{ 
				reqStr += theform.elements[i].name + "="; 
			} 
			isFormObject = true; 
			break; 
		
			case "radio": 
			if (theform.elements[i].checked) 
			{ 
				reqStr += theform.elements[i].name + "=" + theform.elements[i].value; 
				isFormObject = true; 
			} 
		} 
		break; 
		
		case "TEXTAREA": 
		
			reqStr += theform.elements[i].name + "=" + encodeURIComponent(theform.elements[i].value); 
			isFormObject = true; 
			break; 
		
		case "SELECT": 
			var sel = theform.elements[i]; 
			reqStr += sel.name + "=" + sel.options[sel.selectedIndex].value; 
			isFormObject = true; 
			break; 
		} 
		
		if ((isFormObject) && ((i+1)!= theform.elements.length)) 
		{ 
			reqStr += "&"; 
		} 
	
	} 
	
	return reqStr; 
}

//make POST becom GET
function buildQueryString(theFormName) {
  //theForm = document.forms[theFormName]; 

  theForm = getobj(theFormName);
  //use the new function !!
  return create_request_string(theForm);
  
}

function checkForm(theFormName,alertobj,func) {
	returntf = true;
	theForm = getobj(theFormName);
	if (alertobj == null) {
		if (typeof(showHelpDiv)=='function') {
			helpfunc = true;
			//alert('have function !');
		} else {
			alertobj = '';
		}
	} else if (typeof(alertobj)=='string') {
		alertobj = getobj(alertobj);	
	}
	
	if (theForm==null)
		return true;
	
  	for (e=0;e<theForm.elements.length- 1;e++) {			
		if (theForm.elements[e].required=='yes') {
			  if ((theForm.elements[e].type == 'checkbox' && !theForm.elements[e].checked) || (theForm.elements[e].value == '')) {
				  if (theForm.elements[e].title=='') {
						theForm.elements[e].title = theForm.elements[e].name;  
						prefix = 'One or more fields need to be set ! [] ';
				  } else {
					  	prefix = '';
				  }
				  
				  if (helpfunc) { 
					  theForm.elements[e].focus();
					  showHelpDiv(prefix + theForm.elements[e].title);					  
				  } else if (alertobj!='') {
					  alertobj.innerText = prefix + theForm.elements[e].title;
				  } else {
					  alert(prefix + theForm.elements[e].name);
					  //for tab only
					  if (theForm.elements[e].ontab)
						  expandtab(theForm.elements[e].ontab, theForm.elements[e].tabid)					  					  
				  }
				  
				  theForm.elements[e].focus();

				  returntf = false;
				  break;
			  }
		}
    }

	return returntf;
}

function SubmitMyForm(url, formname, holderid) {
	if (typeof(tinyMCE) != 'undefined') {
		tinyMCE.triggerSave();
	}
  
	if (!checkForm(formname)) {
	  return false;
	}

	formURI = buildQueryString(formname);
	if (formURI=='') {
		return false;
	}

	LoadContent(url + formURI,holderid);
}

function SubmitMyForm2(formname, holderid,freset) {
	if (typeof(tinyMCE) != 'undefined') {
		tinyMCE.triggerSave();
	}

	if (!checkForm(formname)) {
	  return false;
	}

	formURI = buildQueryString(formname);
	if (formURI=='') {
		return false;
	}
	
	//alert(getobj(formname).action + '::' + formURI);
	PostLoadContent(getobj(formname).action,formURI,holderid);
	
	//getobj(formname).reset();
}