//var httpObject = null;
var include_path;
var divHeight;
var divWidth;
var divTop;
var divLeft;
var loadingDivName = 'am_loading_div';
var httpObject;

function setIncludePath(path){
	include_path = escape(path);
}

function getHTTPObject(){

	if (window.ActiveXObject)
		return  new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else
		return null;
}

function countExtraParams(params){
	total = 0;
	i = params.indexOf('=');

	while(i != -1){
		total++;
		i = params.indexOf('=', i+1);
	}
	return total;
}

function sendRequest(from, extra, type, todo, update, forceRefresh, hash){

  //display loading
  for(var i = 0; i < from.length; i++){
		displayLoading(from[i]);
	}

  //get http object
  httpObject = getHTTPObject();

  var param = "type=" + type + "&todo=" + todo + '&' + extra + "&update=" + update + '&';
  var count = 2 + countExtraParams(extra);

  //count vars
  var index = extra.indexOf('=');
  while(index != -1){
    count++;
    index = extra.indexOf('=', index+1);
  }

  for(var j = 0; j < from.length; j++){				
		
    var thisDiv = document.getElementById(from[j]);
    var inputs = thisDiv.getElementsByTagName('input');
    var selects = thisDiv.getElementsByTagName('select');
		var textareas = thisDiv.getElementsByTagName('textarea');

    for (i=0; i < inputs.length; i++) {

      if (inputs[i].type == "text" || inputs[i].type == "hidden" || inputs[i].type == "password") {
        param += inputs[i].name + "=" + escape(inputs[i].value) + "&";
        count++;
      }
      if (inputs[i].type == "checkbox") {
        if (inputs[i].checked) {
          param += inputs[i].name + "=" + escape(inputs[i].value) + "&";
        }
        else {
          param += inputs[i].name + "=&";
        }
        count++;
      }
      if (inputs[i].type == "radio") {
        if (inputs[i].checked) {
          param += inputs[i].name + "=" + escape(inputs[i].value) + "&";
          count++;
        }
      }
    }

    for (i=0; i < selects.length; i++) {
      var sel = selects[i]
      if(sel.selectedIndex >= 0)
        param += sel.name + "=" + escape(sel.options[sel.selectedIndex].value) + "&";
      count++;
    }

    for(i=0; i < textareas.length; i++){
    	param += textareas[i].name + "=" + escape(textareas[i].value) + "&";
    	count++;
    }

  }
  //set data and request
  httpObject.onreadystatechange = function(){
  	if(httpObject.readyState == 4){
		  if(httpObject.readyState == 4){
		    var respond = httpObject.responseText;

		    //check forwarding
		    poss = respond.search(/amhulio_forward_start:/);
		    pose = respond.search(/:amhulio_forward_end/);
		    if(poss != -1){
		    	url = respond.substring((poss+22), (pose));
		    	location.href = url;
		    	return;
		    }

		    if(forceRefresh){
		      document.getElementById(update).innerHTML = respond;
		    }
		    else if(respond != ''){		      		      		      
		      for (i=0; i < from.length; i++) {
		      	var thisDiv = document.getElementById(from[i]);
		      	forms = thisDiv.getElementsByTagName('form');
		      	for(j = 0; j < forms.length; j++){
		      		forms[j].reset();
		      	}
		      }		      
		      document.getElementById(update).innerHTML = respond;
		    }

		    if(hash)
		    	window.location.hash = hash;
		  }
		  hideLoading();
  	}
  }

  httpObject.open('POST', include_path+'lib/core/action.run.php', true);
  httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpObject.setRequestHeader("Content-length", count);
  httpObject.setRequestHeader("Connection","close");
  httpObject.send(param);

}


function setOutput(id, forceRefresh, hash){

  if(httpObject.readyState == 4){
    var respond = httpObject.responseText;

    //check forwarding
    poss = respond.search(/amhulio_forward_start:/);
    pose = respond.search(/:amhulio_forward_end/);
    if(poss != -1){
    	url = respond.substring((poss+22), (pose));
    	this.location.href = url;
    	return;
    }

    if(forceRefresh){
      document.getElementById(id).innerHTML = respond;
    }
    else if(respond != ''){
      document.getElementById(id).innerHTML = respond;
    }

    if(hash)
    	window.location.hash = hash;
  }
  hideLoading();
}

function doValidation(value, id){
	var httpObject = getHTTPObject();
	if(httpObject != null){
		//set data and request
	  httpObject.onreadystatechange = function(){
	  	if(httpObject.readyState == 4){
				document.getElementById('msg_'+id).innerHTML = httpObject.responseText;
			}
	  }
		httpObject.open('POST', include_path+'lib/core/action.validate.php', true);
		httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpObject.setRequestHeader("Content-length", 2);
		httpObject.setRequestHeader("Connection","close");
		httpObject.send('value='+value+'&id='+id);

	}
}

function hideLoading(){

	if(document.getElementById(loadingDivName)){
		loadingDiv = document.getElementById(loadingDivName);
		document.body.removeChild(loadingDiv);
	}

}

function displayLoading(id){

	if(document.getElementById('loading_div')){
		hideLoading();
	}
	else{
		loadingDiv = document.createElement('div');
		loadingDiv.id = loadingDivName;
		loadingDiv.style.position = "absolute";
		loadingDiv.style.left = findPosX(id) + 'px';
		loadingDiv.style.top = findPosY(id) + 'px';
		loadingDiv.style.height = findHeight(id) + 'px';
		loadingDiv.style.width = findWidth(id) + 'px';
		loadingDiv.style.backgroundImage = 'url('+include_path+'lib/core/js/loading.gif)';
		loadingDiv.style.backgroundRepeat = 'no-repeat';
		loadingDiv.style.backgroundPosition = 'center';
		document.body.appendChild(loadingDiv);
	}
}

function findWidth(id){
	return document.getElementById(id).offsetWidth;
}

function findHeight(id){
	return document.getElementById(id).offsetHeight;
}

function findPosX(id){
	obj = document.getElementById(id);
	curleft = 0;
	if(obj.offsetParent)
  	while(1){
    	curleft += obj.offsetLeft;
      if(!obj.offsetParent)
      	break;
       obj = obj.offsetParent;
     }
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(id){
	obj = document.getElementById(id);
	curtop = 0;
	if(obj.offsetParent)
		while(1){
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}



function numOnly(e, allowChars){
	pressed = (window.event) ? event.keyCode : e.which;
	c = false;
	if(allowChars)
		c = checkChars(pressed, allowChars);

	//allow common key such as back space, tab
	if(pressed >= 48 && pressed <= 57 || c || pressed == 8 || pressed == 0)
		return true;
	return false;
}

function checkChars(c, list){
	found = false;
	for(i = 0; i < list.length; i++){
		if(c == asciiValue(list[i]))
			found = true;
	}
	return found;
}

function asciiValue (c){
	c = c . charAt (0);

	var i;
	for (i = 0; i < 256; ++ i){
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;
		h = "%" + h;
		h = unescape (h);
		if (h == c)
			break;
	}
	return i;
}
