/*
(C) copyright 2009 by jeff clemmer, chris sehenuk, and detour sites.

all rights reserved.

http://detoursites.com/
*/

function ddCommand(url, command, fields, callback)
{
    var param="";
    
    if (fields==null) {
    
        param="v="+encodeURI(command)+
                "&sid="+Math.random();
                
        ddMakePostRequest(url, param, callback);
        
    } else {
    
        //alert(fields.length);
        
        if (fields.length > 0) {
        
            //alert(document.getElementById(fields[0]).type);
            
            var ajaxfields=""; //""+fields[0]+"="+document.getElementById(fields[0]).value;
        
            //alert(fields.length);
            
            var curStr="";
            var first=true;
            for (var t=0;t<fields.length;t++){
                
                //alert(t+"-"+fields[t]);
                
                //alert();
                
                if (document.getElementById(fields[t]).type=="checkbox") {
                
                    if (document.getElementById(fields[t]).checked)
                    
                        curStr="on";
                        
                    else
                    
                        curStr="off";
                        
                } else {
                
                    curStr=document.getElementById(fields[t]).value;
                    
                    if (curStr.indexOf("`")!=-1) {
                    
                        alert("what the fuck are you trying to do?");
                        return;
                    
                    }
                    
                }
                
                if (first==true) {
                
                    ajaxfields=ajaxfields+fields[t]+"="+curStr;
                    first=false;
                    
                } else
                
                    ajaxfields=ajaxfields+"`"+fields[t]+"="+curStr;
                
            }
            
            param="v="+encodeURI(command)+
                    "&f="+encodeURI(ajaxfields)+
                    "&sid="+Math.random();
                    
            //alert(param);
                    
            //alert("command-fields");
            ddMakePostRequest(url, param, callback);
                    
        } else {
        
            alert("Sorry, there was a server error...");
        
        }
    
    }

}

//specfields allows you to just pass in a string, that will be appended to all the regular fields.  this is so you can have non standard fields, like on the detour site with the graphic check boxes.  the getbyid can't get these sorts fields...

//this one you have to push in fields...
function ddCommandExt(url, command, fields, specfields, callback)
{
    var param="";
    
	//alert(fields.length);
	
	if (fields.length > 0) {
	
		//alert(document.getElementById(fields[0]).type);
		
		var ajaxfields=""; //""+fields[0]+"="+document.getElementById(fields[0]).value;
	
		//alert(fields.length);
		
		var curStr="";
		var first=true;
		
		//alert(fields.length);
		for (var t=0;t<fields.length;t++){
			
			//alert(t+"-"+fields[t]);
			
			//alert();
			
			if (document.getElementById(fields[t]).type=="checkbox") {
			
				if (document.getElementById(fields[t]).checked)
				
					curStr="on";
					
				else
				
					curStr="off";
					
			} else {
			
				curStr=document.getElementById(fields[t]).value;
				
				if (curStr.indexOf("`")!=-1) {
				
					alert("what the fuck are you trying to do?");
					return;
				
				}
				
			}
			
			if (first==true) {
			
				ajaxfields=ajaxfields+fields[t]+"="+curStr;
				first=false;
				
			} else
			
				ajaxfields=ajaxfields+"`"+fields[t]+"="+curStr;
			
		}
		
		param="v="+encodeURI(command)+
				"&f="+encodeURI(ajaxfields);
				
		if (specfields.length > 0)
			param=param+specfields;
			
		param=param+"&sid="+Math.random();
				
		//alert(param);
				
		//alert("command-fields");
		ddMakePostRequest(url, param, callback);
				
	} else {
	
		alert("no fields specified");
	
	}

}


//this function is option, and provides a generic way to update an
//element
function ddCommandDone(id)
{
    var response="";
    if (xhttp.readyState==4 || xhttp.readyState=="complete"){
        
        response=xhttp.responseText;

        //alert(response);
        
        var cBlock=response.split("(~)");
        
        if (cBlock[0]=="ok") {
        
            document.getElementById(id).innerHTML=cBlock[1];
            
        } else {
        
            if (response.length==0)
                alert("there was a server error.  empty reply.  please try again.");
            else {
            
                if (response.match("( ! )")!=null) {
                    
                    document.getElementById(id).innerHTML="<div style=\"background:darkred; color:white; padding:10px;\">"+response+"</div>";
                    
                    return;
                    
                }
                
                if (cBlock[0]=="debug") {
                
                    alert(response);
                    document.getElementById("debug").innerHTML="<div style=\"background:darkred; color:white; padding:10px;\"><pre>"+response+"</pre></div>";
                    
                    return;
                
                }    
                    
                alert("server error:\n\n"+response);
                
            }
            
        }
        
    }
    
}

function ddMakePostRequest(url, param, callback)
{
    
    xhttp = false;
    if (window.XMLHttpRequest) 
    { // Mozilla, Safari,...
        xhttp = new XMLHttpRequest();
        if (xhttp.overrideMimeType) 
        {
            // set type accordingly to anticipated content type
            //http_request.overrideMimeType("text/xml");
            xhttp.overrideMimeType("text/html");
        }
    } 
    else if (window.ActiveXObject) 
    { // IE
        try 
        {
            xhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e)
        {
            try 
            {
                xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) 
            {}
        }
    }
    if (!xhttp) 
    {
        alert("Cannot create XMLHTTP instance");
        return false;
    }
    
    //alert (param);
    
    xhttp.onreadystatechange = callback;//bodychangeready;
    xhttp.open("POST", url, true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.setRequestHeader("Content-length", param.length);
    xhttp.setRequestHeader("Connection", "close");
    xhttp.send(param);
    
}


