//conditions to consider for future remake of this ajax request:
// 1. ability to focus a control after the complete event fired. (see function tryfocus() for workaround

var xmlhttp = null;
var xmlhttp1 = null;
var spanid = "";


function xmlhttprequest(id, url) {
//alert("test"+url);
	spanid = id;
	xmlhttp = getxmlhttpobject();
	if (!xmlhttp) {
		alert('Your browser does not support xmlhttp object.');
		return;
	}

	xmlhttp.onreadystatechange = stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
	return 1;

}


function getxmlhttpobject() {
	var objXMLHTTP = null;
	
	if (window.ActiveXObject){
		try { 
			objXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e) {
			try {
				objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch (e) {}
		}
	} else {
		try {
			objXMLHTTP = new XMLHttpRequest();
			}
		catch (e) { objXMLHTTP = false; }
	}
	
	if (!objXMLHTTP) { alert("...giving up. Failed to create XMLHttpRequest object."); }
	
return objXMLHTTP;
}

function stateChanged() {
		
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete') {
	//	if (xmlhttp.status == 200) {
		//alert("readystate is 4");
		
			document.getElementById(spanid).style.visibility = 'visible';
			document.getElementById(spanid).innerHTML = xmlhttp.responseText; 
			tryfocus();
	}
	else {
		
		document.getElementById(spanid).innerHTML = "<br><div align='center' class='loaderstyle'> Loading <img src='images/ajax-loader.gif'></div>";
	}

}


function stateChanged3() {
		
	if (xmlhttp1.readyState == 4 || xmlhttp1.readyState == 'complete') {
	//	if (xmlhttp.status == 200) {
		//alert("readystate is 4");
		
			document.getElementById(spanid).style.visibility = 'visible';
			document.getElementById(spanid).innerHTML = xmlhttp1.responseText; 
			tryfocus();
	}
	else {
		
		document.getElementById(spanid).innerHTML = "<br><div align='center' class='loaderstyle'> Loading <img src='images/ajax-loader.gif'></div>";
	}

}

function tryfocus(){
	try {document.forms[0].elements['forumid'].focus();}
	catch(e) {}	
}


///////////////duplicate request with additional params

function xmlhttprequest2(id, url, methods, keyvalue) {
//alert("test"+url);
	spanid = id;
	xmlhttp = getxmlhttpobject();
	if (!xmlhttp) {
		alert('Your browser does not support xmlhttp object.');
		return;
	}

	xmlhttp.onreadystatechange = stateChanged;
	xmlhttp.open(methods, url, true);
	//alert('method: '+methods+' keyvalue: '+keyvalue);
	if (methods=='post') {
		//alert(methods);
		//xmlhttp.setRequestHeader('X-Referer', document.location);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        xmlhttp.setRequestHeader("Content-Length", keyvalue.length);
        xmlhttp.setRequestHeader("Connection", "close");
	    xmlhttp.send(keyvalue);
	}
	else {
		xmlhttp.send(null);
	}
	
	return 1;
	
}


function xmlhttprequest3(id, url, methods, keyvalue) {
//alert("test"+url);
	spanid = id;
	xmlhttp1 = getxmlhttpobject();
	if (!xmlhttp1) {
		alert('Your browser does not support xmlhttp object.');
		return;
	}

	xmlhttp1.onreadystatechange = stateChanged3;
	xmlhttp1.open(methods, url, true);
	//alert('method: '+methods+' keyvalue: '+keyvalue);
	if (methods=='post') {
		//alert(methods);
		//xmlhttp.setRequestHeader('X-Referer', document.location);
        xmlhttp1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        xmlhttp1.setRequestHeader("Content-Length", keyvalue.length);
        xmlhttp1.setRequestHeader("Connection", "close");
	    xmlhttp1.send(keyvalue);
	}
	else {
		xmlhttp1.send(null);
	}
	
	return 1;
	
}

function spanfloat(spanid,size,width,height){
//	alert("1");
globalsize = size;
globalspan = spanid;
	browser = navigator.appName;

	if(size=='big'){ 
	document.getElementById('span_global').style.width = (document.body.clientWidth - 300)+"px";
	var topx =  400;
	var leftx = document.body.clientWidth - 300;
	 }
	 
	else if(size=='mid'){ 
	document.getElementById('span_global').style.width = (document.body.clientWidth - 600)+"px";
	var topx =  400;
	var leftx = document.body.clientWidth - 600;
	 }
	 
	else if(size=='custom'){ 
	document.getElementById('span_global').style.width = width;
	var topx =  height;
	var leftx = width;
	 }
	 
	else if(size=='profile'){
	document.getElementById('span_global').style.width = 588+"px";
	var topx =  687;
	var leftx = 588;
//	alert(topx);
//	alert(leftx);
	}
	else if(size=='addfriend'){
	document.getElementById('span_global').style.width = 588+"px";
	var topx =  687;
	var leftx = 588;
//	alert(topx);
//	alert(leftx);
	}
	 
	else{
	var topx = 400; var leftx = 300;
	document.getElementById('span_global').style.width = "300px";	
	}
	 
///	document.getElementById('span_global').style.width = (document.body.clientWidth - 600)+"px";	 
//else { var topx = 400; var leftx = 300; }
document.getElementById(spanid).style.display = "block";
document.getElementById(spanid).style.visibility = "visible";
var topmargin = gettop(topx);
if(topmargin<=20) topmargin = 25;
document.getElementById(spanid).style.top = topmargin+"px";
document.getElementById(spanid).style.left = getleft(leftx)+"px";
//alert(gettop(topx));
bodyshade('bodyshade');
}

function bodyshade(spanid){
	//alert('12121');
//document.getElementById(spanid).style.bgColor = "#000000";
//alert(browser);
//	if(browser=='Netscape' || browser=='Microsoft Internet Explorer'){
	if(browser=='Netscape') document.getElementById(spanid).style.opacity = .5
	else if(browser=='Microsoft Internet Explorer') document.getElementById(spanid).style.filter = "alpha(opacity=50)";
	else if(browser=='Opera') document.getElementById(spanid).style.opacity = .5
	document.getElementById(spanid).style.top = 0;
	document.getElementById(spanid).style.left = 0;
	
	var sH = document.body.scrollHeight
	var cH = document.documentElement.scrollHeight
	if(sH < cH) document.getElementById(spanid).style.height = cH+"px";
	else document.getElementById(spanid).style.height = sH+"px";
	
	var sW = document.body.scrollWidth;
	document.getElementById(spanid).style.width = sW+"px";	
//	}
}

function bodyunshade(spanid){
//	if(browser=='Netscape' || browser=='Microsoft Internet Explorer'){
	if(browser=='Netscape') document.getElementById(spanid).style.opacity = .0
	else if(browser=='Microsoft Internet Explorer') document.getElementById(spanid).style.filter = "alpha(opacity=0)";
	else if(browser=='Opera') document.getElementById(spanid).style.opacity = .0
	document.getElementById(spanid).style.height = 0+"px";
	document.getElementById(spanid).style.width = 0+"px";
//	}
}

function closespan(spanid){
	document.getElementById(spanid).style.visibility = "hidden";
}

function gettop(objectheight){
	//var displacedH = ((screen.availHeight)/2) - (objectheight/2) ;
	var scrXY = new Array();
	var displacedH;
	scrXY = getScrollXY();
//	if(browser=='Netscape') var browserheight = document.body.clientHeight;
	if(browser=='Microsoft Internet Explorer'){
		if (document.documentElement && document.documentElement.clientHeight)
			browserheight = document.documentElement.clientHeight;
		else if (document.body)
			browserheight = document.body.clientHeight
		displacedH = (((browserheight)/2) - (objectheight/2)) + 15 + scrXY[1];
	}
	else if(browser=='Netscape'){
			displacedH = document.body.scrollTop + ((document.body.clientHeight - objectheight)/2);
//			alert(document.body.scrollTop);
	}
	else if(browser=='Opera') {
		var browserheight = document.body.clientHeight;
		displacedH = (((browserheight)/2) - (objectheight/2)) + 15 + scrXY[1];
	}
//	var displacedH = ((document.body.clientHeight-objectheight)/2)  + scrXY[1];
//	alert(browserheight+"::"+objectheight+"::"+document.body.scrollHeight);
//	var displacedH = document.body.scrollTop;
//	alert(document.body.scrollTop);
	return displacedH;
}

function getleft(objectwidth){
	//var displacedH = ((screen.availHeight)/2) - (objectheight/2) ;
//	var scrXY = new Array();
//	scrXY = getScrollXY();
		
	var displacedW = (((document.body.clientWidth)/2) - (objectwidth/2));
	return displacedW;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
// return [ scrOfX, scrOfY ];
 var scr = new Array(scrOfX,scrOfY);
 return scr;
}

function valuecheck(){
	
	if(document.searcha.status1[0].checked == true ||  document.searcha.status1[1].checked == true) return true;
	else return false;


}

function showme(id,count){
	
	elem = document.getElementById('td'+id);
	//alert(id+'--'+count);
	for(var i=0;i<count;i++){
	
		if(i != id){
			document.getElementById('td'+i).style.display = 'none';		
			document.getElementById('tdhead'+i).style.backgroundColor = '#cccccc';
			document.getElementById('tdhead'+i).style.color = '#990000';
		}else{
			elem.style.display = 'block';	
			document.getElementById('tdhead'+id).style.backgroundColor = '#ff0000';	
			document.getElementById('tdhead'+i).style.color = '#ffffff';
		}
		
	}
	
	
}


function showonlyone(boxes,len,thechosenone) {

       //var newboxes = document.getElementsByName(boxes);
	  // var newboxeshead = document.getElementsByName(boxes+"head");
	 
       for(var x=1; x<len; x++) { 
	   		if(document.getElementById(boxes+x) != null){
               if (boxes+x == thechosenone) {
		
                   document.getElementById(boxes+x).style.display = 'block';
				   document.getElementById(boxes+"head"+x).style.backgroundColor = '#ff0000';
				   document.getElementById(boxes+"head"+x).style.color = '#ffffff';
               }
               else {
				   
                   document.getElementById(boxes+x).style.display = 'none';
				   document.getElementById(boxes+"head"+x).style.backgroundColor = '#cccccc';
				   document.getElementById(boxes+"head"+x).style.color = '#990000';
               }
			}
       }
}



/* ---------------------------------------------------- MULTI AJAX   ----------------------------------------------------*/


var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS
var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE


function xhrRequest(type) {
if (!type) {
type = 'html';
}


// xhrsend IS THE xi POSITION THAT GETS PASSED BACK
// INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
// IN CASE A FREE RESOURCE ISN'T FOUND IN THE LOOP
var xhrsend = xi.length;

// GO THROUGH AVAILABLE xi VALUES
for (var i=0; i<xi.length; i++) {


// IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
if (xi[i] == 1) {
xi[i] = 0;
xhrsend = i;
break;
}
}


// SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
xi[xhrsend] = 0;


// SET UP THE REQUEST
if (window.ActiveXObject) {
try {
xhr[xhrsend] = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr[xhrsend] = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
} else if (window.XMLHttpRequest) {
xhr[xhrsend] = new XMLHttpRequest();
if (xhr[xhrsend].overrideMimeType) {
xhr[xhrsend].overrideMimeType('text/' + type);
}
}
return (xhrsend);
}


function fcn(id, url, methods, keyvalue) {

var xhri = xhrRequest('html');
xhr[xhri].open(methods, url, true);
xhr[xhri].onreadystatechange = function() {
	if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
	//alert(xhr[xhri].responseText);
	
	
	document.getElementById(id).style.visibility = 'visible';
	document.getElementById(id).innerHTML = xhr[xhri].responseText; 
	
	xi[xhri] = 1;
	xhr[xhri] = null;
	}else{
		document.getElementById(id).innerHTML = "<br><div align='center' class='loaderstyle'> <img src='images/ajax-loader.gif'></div>";
	}
};
	
	
	if (methods=='post') {
		//alert(methods);
		//xmlhttp.setRequestHeader('X-Referer', document.location);
		
       xhr[xhri].setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
       xhr[xhri].setRequestHeader("Content-Length", keyvalue.length);
       xhr[xhri].setRequestHeader("Connection", "close");
	   xhr[xhri].send(keyvalue);
	}
	else {
		xhr[xhri].send(null);
	}
	
	//xhr[xhri].send(null);
}

function fcn2(id, url, methods, keyvalue) {

var xhri = xhrRequest('html');
xhr[xhri].open(methods, url, true);
xhr[xhri].onreadystatechange = function() {
	if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
	//alert(xhr[xhri].responseText);
	
	
	document.getElementById(id).style.visibility = 'visible';
	document.getElementById(id).innerHTML = xhr[xhri].responseText; 
	
	alert(xhr[xhri].responseText);
	
	xi[xhri] = 1;
	xhr[xhri] = null;
	}else{
		document.getElementById(id).innerHTML = "<br><div align='center' class='loaderstyle'> <img src='images/ajax-loader.gif'></div>";
	}
};
	
	
	if (methods=='post') {
		//alert(methods);
		//xmlhttp.setRequestHeader('X-Referer', document.location);
		
       xhr[xhri].setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
       xhr[xhri].setRequestHeader("Content-Length", keyvalue.length);
       xhr[xhri].setRequestHeader("Connection", "close");
	   xhr[xhri].send(keyvalue);
	}
	else {
		xhr[xhri].send(null);
	}
	
	//xhr[xhri].send(null);
}


function checkProofVal(){
	var form = document.formreg;
	if(form.proof[0].checked == true){
		showProofForm();
	}else if(form.proof[1].checked == true){
		hideProofForm();
	}	
}

function showProofForm(){
	for(var x=0;x<10;x++){
		document.getElementById('proof' + x).style.display = '';	
	}
}

function hideProofForm(){
	for(var x=0;x<10;x++){
		document.getElementById('proof' + x).style.display = 'none';	
	}
}

function showthis(type,id,total,uid, byid){
	
	for(var x=1;x<(total);x++){		
		if(x==id){
			var param = '?type=' + type + '&id=' + id + '&uid=' + uid + '&byid=' + byid;
			document.getElementById('ajaxspan_'+x).style.display = 'block';
			fcn('ajaxspan_' + x, 'ajax/comments.php' + param, 'get');	
		}else{ 
		
			if(document.getElementById('comment'+x) != null && document.getElementById('comment'+x).value != ""){
				document.getElementById('ajaxspan_'+x).style.display = 'block';
			}else{
				document.getElementById('ajaxspan_'+x).style.display = 'none';
			}
		}	
	}
}

function showthis2(type,id,total,uid,byid){
	
	for(var x=1;x<(total);x++){		
		if(x==id){
			var param = '?type=' + type + '&id=' + id + '&uid=' + uid+ '&byid=' + byid;
			document.getElementById('ajaxspan_'+x).style.display = 'block';
			fcn2('ajaxspan_' + x, 'ajax/comments2.php' + param, 'get');	
		}else{ 
		
			if(document.getElementById('comment'+x) != null && document.getElementById('comment'+x).value != ""){
				document.getElementById('ajaxspan_'+x).style.display = 'block';
			}else{
				document.getElementById('ajaxspan_'+x).style.display = 'none';
			}
		}	
	}
}

function sendcomment(id,uid,byid,comment){
	if(comment != ""){
		var param = 'uid=' + uid + '&byid=' + byid + '&com=' + comment;
		fcn('ajaxspan_' + id, 'ajax/add_comment.php', 'post', param);
		fcn('ajaxcoms_' + id, 'ajax/comment_list.php', 'post', param);
		fcn('ajax_totals', 'ajax/totals.php', 'post', param);	
	}
}

function sendcomment2(id,uid,byid,comment){
	if(comment != ""){
		var param = 'uid=' + uid + '&byid=' + byid + '&com=' + comment;
		fcn('ajaxspan_' + id, 'ajax/add_comment.php', 'post', param);
		fcn('ajax_bigcoms', 'ajax/comment_list2.php', 'post', param);
		fcn('ajax_totals', 'ajax/totals.php', 'post', param);
	}
}

function sendboost(type,id,uid,param2){
	document.getElementById('ajaxspan_'+ id).style.display = 'block';
	var param = 'type=' + type + '&id=' + id + '&uid=' + uid + param2;
	fcn('ajaxspan_' + id, 'ajax/comments.php', 'post', param);	
	fcn('ajax_totals', 'ajax/totals.php', 'post', param);	
}

function getdrugloc(value,returnval){
	var param = 'drugstore=' + value + '&returnval=' + returnval;
	fcn('span_drugloc', 'ajax/druglocation.php', 'post', param);	
}