/*
COPYRIGHT 2006 Joseph B. Mcleary
Please keep this message in tact. I appreciate it.
welcome to joe's ajax lab - a lot of experiments going on here.

To use this file:

include this js when loading a page. make an ajax 
request by using the following syntax in your html:

<a href="javascript:load_request(0,'myServerFile.html','content_panel',null)">get data</a>
<a href="javascript:load_request(1,'myServerFile.php','content_panel',document.myFormName)">submit form</a>
<span id="content_panel"></span>

your link(s) should call the load_request function. take a moment 
and look at the parameters.
*/

function load_request(reqType,reqFile,dispDiv,frm,respType){



//the request loader - prep work for sending/recieving to/from a server file
//parameters: 
//reqType - 0=incoming;1=outgoing;2=outgoing;
//reqFile - server destination file (with or without a querystring)
//dispDiv - local html container to display the response
//frm - the form object if posting - null if getting
//respType - 0=stream; 1=formatted text (optional)

	// DETERMINE REQUEST TYPE - INCOMING / OUTGOING (with/without validation)
	switch(reqType)
	{
	case 0:		//get from server file
		main(reqType,reqFile,dispDiv,null,respType);
		break;
	
	case 1:		//post to server file. check elements for blanks first
		frmObj=frm;
		if(!getNameValPairs(frm)){
			alert("complete the fields please");
		}else{
		strout=new String();
		strout+=getNameValPairs(frm); //parse the form
		main(reqType,reqFile,dispDiv,strout,respType)
		}
		
		break;
	case 2:		//post to server file - no checking for blanks 
		frmObj=frm;
		if(getNameValPairs(frm)){
		strout=new String();
		strout+=getNameValPairs(frm); //parse the form
		main(reqType,reqFile,dispDiv,strout,respType)
		}
		break;
	}
}

function main(reqType,urlStr,dest,outStr,respType){ //main street

	   //this is our main object.
	   
	   //paramters:
	   //reqType - 0=get;1=post
	   //urlStr - the remote file to interact with
	   //dest - the local (client) destination html container to write the response to
	   //outStr - used for POSTING (sending). the name/value pairs of the specified form
	   //respType - direct to the proper response handler
	   
	   //properties/methods etc...
	   this.reqType=reqType;
	   this.respType=respType;
	   this.dest=dest;
	   this.url=urlStr;
	   this.stream=getStream;
	   this.disp=toScreen	//wire in the local output function. where to display the response
	   this.rnd=Math.random();	//it's stated that IE has a caching bug and appending a random number to the url eases it. here's a rand num

	   //we show a request status message/image until the response comes back
	   //this.disp("request_status_panel","requesting - one moment...");
	   //this.disp("request_status_panel","<img src='img/network_transmit.jpg'>");

	  //create an http instance from our http request function
	   callObj=new httpObj();
	   
	   //handling the in-progress response status
	   callObj.onreadystatechange = statusCheck; 
			
	   //check the current file for existence of querystring. if there is one, 
	   //use an "&" as a "final" separator when building url below. Otherwise, use
	   //a "?" since a querystring has to be created.
	   separator=new String();
	   		if(this.url.indexOf("?")>0){
	   			separator="&";
	   		}else{
	   			separator="?";
	   		}
			
	   if(this.reqType==0){	
	   // asking for a file
       		callObj.open('GET', this.url+separator+this.rnd, true); 
			data=null;
	   }else{		
	   //sending to a file
	    	callObj.open('POST', this.url+separator+this.rnd, true); 
			callObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
			data=outStr;
	   }
	   callObj.send(data); 
 }


function httpObj(){

//this is our generic http request constructor. use this 
//to create our http connector when we need it. this piece is 
//inspired and hacked from the examples at IBM, and countless other
//tutorials out there.

var httpObject = false; //assume the worst

	//now, try for an object.

   if (window.XMLHttpRequest) // try non IE 
   { 
       httpObject = new XMLHttpRequest(); 
       if (httpObject.overrideMimeType) { 
           httpObject.overrideMimeType('text/xml'); 
       } 
	   
   }else if (window.ActiveXObject) { // try IE 
       try
	   { 
          httpObject= new ActiveXObject("Msxml2.XMLHTTP"); 
       }catch (e){ 
           try	//if the above did not work
		   { 
               httpObject = new ActiveXObject("Microsoft.XMLHTTP"); 
           }catch (e){} 
       } 
   } 
   return httpObject;	
}

   
 
function statusCheck() {
	//handles the request state and passes control to the display
	//when ready

	targID=new String(dest);	//our destination html container's ID

       if (callObj.readyState == 4) { 
           if (callObj.status == 200) { 
           
			  //disp(targID,callObj.responseText);	//write to local container
			  
			  if(respType==0){
			  getStream(callObj.responseText);
			  }
			  if(respType==1){
			  disp(targID,callObj.responseText);
			  }
			  if(respType==2){
			  disp(targID,callObj.responseText);
			  }
			  if(respType==3){
			  disp(targID,callObj.responseText);
			  updateLabel("scEnabledStatus",callObj.responseText);
			  }
			  //the response is back. clear the request status message. 
			   //disp("request_status_panel","")
				
			 // or have the request status message vanish after a set interval
			 // once response is back
			 // if(reqType==1){
			 // 	setTimeout("eraseElementText(targID)",2000);
			 //  }
           } else { 
		   	   // problem
			   disp(targID,"problem connecting - status code "+callObj.status); //write to destination container
           } 
		callObj=null;
       } 

} 
   
function updateLabel(divID,respText){
	rt=new String(respText);
	if(respText.indexOf("disabled") >=0)
	{
		disp(divID,"you are not stage enabled");
		document.getElementById(divID).style.color="#ff0000";
	}else{
		disp(divID,"you are stage enabled");
		document.getElementById(divID).style.color="#3CB371";
	}
}
function getNameValPairs(f){
	//for POSTING a form, this function gathers all the form elements 
	//by name and creates a name/value pair string.
	
	nvPairs=new String();
	for(var x=0;x<f.length;x++){
	
		//if(f.elements[x].value==""){ 	//no fields left blank
		//return false;					//no fields left blank
		//}								//no fields left blank
		//validation here could get tricky based on 
		//a particular requirement. might be best to 
		//do it traditionally elsewhere
	nvPairs+=f.elements[x].name+"="+escape(f.elements[x].value)+"&";
	//alert(nvPairs);
	
	}
	return nvPairs;
}

 function toScreen(t,d){	
 //this function writes to destination ("t"arget ID) container
 //with response ("d"ata)
 	targPanelObj=getElementObject(t);
	targPanelObj.innerHTML=d;
 }
 
 function eraseElementText(tid){
 //this is good when you want to momentarily show a confirmation text, 
 //then have it go away - to be ready for future confirmations
 	targPanelObj=getElementObject(tid);
	targPanelObj.innerHTML="";
 }
 
 function getElementObject(elementID){
 //return an element object by passing in the element id
 targElementID=new String(elementID);
 return document.getElementById(targElementID);
 }
 
 function getStream(rt){
	responseString=new String(rt);
	stream_job(responseString);
 }

 function stream_job(rs){
  	inString=new String(rs);
	splitResponse=inString.split("|");
	document.projFrm.trkName.value=splitResponse[1];
	document.projFrm.trkDesc.value=splitResponse[2];
	document.projFrm.trkID.value=splitResponse[0];
	
	//initialize sync flag
	document.projFrm.trkSync.checked=false;
	//now check the box if needed
	if(splitResponse[3]=="1"){
		document.projFrm.trkSync.checked=true;
	}
	
	document.projFrm.trkUpdateButton.disabled=false;
	document.projFrm.trkUpdateButton.value="Save";
	document.projFrm.delUpdateButton.disabled=false;
	document.projFrm.delUpdateButton.value="Delete";
	document.projFrm.abortButton.disabled=false;
	document.projFrm.abortButton.value="Cancel";
 }
 

function lineBoxOpen(tableWidth,cellHeight,dirOffset,cssclass){

with(document){

	write('<table width="'+tableWidth+'" align="center" cellspacing="0" cellpadding="0" border="0">');
	write('<tr>');
	write('<td style="font-size:6px" background="'+dirOffset+'/images/border/rnd_cnr_line_TL.jpg" width="9" height="7">&nbsp;</td>');
	write('<td style="font-size:6px" background="'+dirOffset+'/images/border/rnd_cnr_line_T.jpg" height="9">&nbsp;</td>');
	write('<td style="font-size:6px" background="'+dirOffset+'/images/border/rnd_cnr_line_TR.jpg" width="9" height="7">&nbsp;</td>');
	write('</tr>');
	write('<tr>');
	write('<td style="font-size:6px" background="'+dirOffset+'/images/border/rnd_cnr_line_L.jpg" width="9">&nbsp;</td>');
	write('<td height="'+cellHeight+'" valign="top" class="'+cssclass+'">');
}
}

function lineBoxClose(dirOffset){
with(document){

	write('</td>');
	write('<td style="font-size:6px" background="'+dirOffset+'/images/border/rnd_cnr_line_R.jpg" width="9">&nbsp;</td>');
	write('</tr>');
	write('<tr>');
	write('<td style="font-size:6px" background="'+dirOffset+'/images/border/rnd_cnr_line_BL.jpg" width="9" height="7">&nbsp;</td>');
	write('<td style="font-size:6px" background="'+dirOffset+'/images/border/rnd_cnr_line_B.jpg" height="9">&nbsp;</td>');
	write('<td style="font-size:6px" background="'+dirOffset+'/images/border/rnd_cnr_line_BR.jpg" width="9" height="7">&nbsp;</td>');
	write('</tr>');
	write('</table>');
}
}

function tog(eid)
{
    elementID = new String(eid);
    elObj=document.getElementById(elementID);
    
    if(elObj.style.display =="none")
    {
        elObj.style.display="";
    }else{
        elObj.style.display="none";
    }
}


function checkAreaLength(txtArea,outPanel,mxLen){
	
	targArea=new String(txtArea);
	outArea=new String(outPanel);
	var maxLen=mxLen;
	var offsetVal=0;
		lenStr=new String(document.getElementById(targArea).value);
		currLen=lenStr.length;
		offsetVal=(maxLen-currLen);
		if(currLen>=maxLen){
			offsetVal=0;
			document.getElementById(targArea).value=lenStr.substring(0,maxLen);
		}
			document.getElementById(outArea).innerHTML="("+offsetVal+")";

}

function startProgress(btnSubmitID,btnCancelID)
{
    btnS = new String(btnSubmitID);
    btnC = new String(btnCancelID);
    document.getElementById("uploadButtons").style.display=""; 
    document.getElementById(btnS).style.display="none"; 
    document.getElementById(btnC).style.display="none"; 
}

function startSend(btnSubmitID)
{
    btnS = new String(btnSubmitID);
    document.getElementById("uploadButton").style.display=""; 
    document.getElementById(btnS).style.display="none"; 

}
