// global xml get begin 
var agt = navigator.userAgent.toLowerCase();
var is_mac = (agt.indexOf("mac") != -1);
var xmlgetcurr = "";
var ad_host = ""
var cookie_name = "";
var message2 = "";
var status = "";
var req;
var counter = 1;
var globaldiv = "";
var post_ajax_command = "";
var pre_ajax_command = "";
function xmlget (url, alt_url, div, message, forcerefresh) {
	return xmlpost (url, alt_url, div, message, forcerefresh, "")
}

// global xml post begin 
var orig_height = "";
var vars2 = "";                                                                                                                                                            
function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
	while (obj.offsetParent)
	{
	     curtop += obj.offsetTop
	     obj = obj.offsetParent;
	}
    }
    else if (obj.y)
	curtop += obj.y;
    return curtop;
}

function format_form_for_ajax (form) {
	var str = "";
	for (var i =0 ; i < form.length; i++) {
		if (form[i].type=='checkbox') {
			if (form[i].checked) {
				str += form[i].name + "=" + escape(form[i].value) + "&";
			} else {
				str += form[i].name + "=&";
			}
		} else {
			str += form[i].name + "=" + escape(form[i].value) + "&";
		}
	}
	str += "from_ajax=true";
	return str;
}

function xmlpostform(form, div, message, forcerefresh) {
	var str = format_form_for_ajax (form);
	xmlpost (form.action, '', div, message, forcerefresh, str);
//	alert(str);
	return false;
}

function xmlpost (url, alt_url, div, message, forcerefresh, vars) {


        if (div != "" && message != "") {
		if (change_heights) {	
			var top_height_div = div + "_top_height_div";
			var bottom_height_div = div + "_bottom_height_div";
			if (!getByID(top_height_div)) {
				getByID(div).innerHTML = "<div id='" + top_height_div + "'></div>" + getByID(div).innerHTML + "<div id='" + bottom_height_div + "'></div>";
				vars2 = vars;
        		        setTimeout('xmlpost("' + url + '", "' + alt_url + '", "' + div + '",  "' + message + '", "' + forcerefresh + '", vars2)', 0);
				return;
			} else {
				getByID(div).style.height = findPosY(getByID(bottom_height_div)) - findPosY(getByID(top_height_div));
			}	
		}
	    	getByID(div).innerHTML = message;
		vars2 = vars;
                setTimeout('xmlpost2("' + url + '", "' + alt_url + '", "' + div + '", "' + forcerefresh + '", vars2)', 0);
        } else {
               	xmlpost2(url, alt_url, div, forcerefresh, vars);
        }
}
function xmlpost2(url, alt_url, div, forcerefresh, vars) {
	if (pre_ajax_command) {
		eval(pre_ajax_command);
	}
//	var pattern = new RegExp ("&", 'gi');
//	alert(vars.replace(pattern, "\n"));
	if (xmlgetcurr != "") {
		vars2 = vars;
             	setTimeout('xmlpost2("' + url + '", "' + alt_url + '", "' + div + '", "' + forcerefresh + '", vars2)', 1);
		return; 
        } else {
        if (forcerefresh) {
                document.cookie = cookie_name + "=1; domain=." + ad_host + "; path=/";
//                document.cookie = cookie_name + "; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=." + ad_host + "; path=/";
        }	
	if (div == "") {
		xmlgetcurr = counter++;
	} else {
		xmlgetcurr = div;
	}
        if (window.ActiveXObject  && !is_mac) {
                req = new window.ActiveXObject("Msxml2.XMLHTTP");
        } else if (window.XMLHttpRequest) {
                req = new XMLHttpRequest();
        } else {
             if (alt_url != "") {
                location.replace(alt_url)
              }
        xmlgetcurr = "";
        status = "";
         return;
        }
    globaldiv=div;
	if (vars == "") {
		req.open("GET", url, true);
	} else {
    		req.open("POST", url, true);
	}
    req.send(vars);

	checkstate();
//	alert(req.responseText);

    req.onreadystatechange=checkstate;

	
    return;
	}	
}

function checkstate () {
	div = globaldiv;
	stats = getByID("output");
	if (stats) {
		stats.innerHTML = req.readyState;
	}
	if (req.readyState == 4) {

		xmlgetcurr = "";		
		 if (div != "") {
			 var x = req.responseText;
		//	     alert(x);
		 	getByID(div).innerHTML = x;
			
        	}

	        status = req.status;
		if (post_ajax_command) {
			eval(post_ajax_command);
		}
		setTimeout(from_response_javascript, 0);
	    return req.status;
	} else if (req.readyState == 0) {
		return "";
	} else {
		return "";
	}

}

function from_response_javascript() {
	var obj = getByID('post_ajax');
	if (obj) {
		if (obj.innerHTML != "") {
			alert(obj.innerHTML);
			eval(obj.innerHTML);
			obj.innerHTML == "";
			obj.id = '';
		}
	}
	
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
}
// global xml get end 
