function createRequestObject() {
	var req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
    
    return req ;
}

function ReturnNoDQuoteString(string){
	
	rExp = /\"/g ;
	replaceString = new String("&quot;") ;
	newString = string.replace(rExp,replaceString) ;
	
	return newString ;
	
}

var http = createRequestObject();	// We create the HTTP Object
var table = "" ;					// used later
var theDiv = "" ;					// used later













function updatedActivated() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var ID = http.responseText ;
			var curClass = document.getElementById( "sc"+ID ).className ;
			document.getElementById( "sc"+ID ).className = (curClass == 'normal')?'strong':'normal' ;
			UpdateShowcaseShowOnHome() ;
//			window.refresh() ;
		}
	}
}

function updatedShowOnHome() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var ID = http.responseText ;
			var curClass = document.getElementById( "scoh"+ID ).className ;
			document.getElementById( "scoh"+ID ).className = (curClass == 'normal')?'strong':'normal' ;
		}
	}
}

function updateDivContent(){
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			document.getElementById( theDiv ).innerHTML = http.responseText ;
		}
	}
}

function alertResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			alert(http.responseText) ;
		}
	}
}

function reloadPage() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			window.location.reload() ;
		}
	}
}

function goToPage(){
	
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			
			switch(table){
			
				case 'news'		:	window.location = "/index.php?sec=Other&sub=news" ;
									break ;
				case 'careers'	:	window.location = "/index.php?sec=Other&sub=careers" ;
									break ;
				case 'bios'		:	window.location = "/admin/edit_bios.php" ;
									break ;
				case 'showcase'	:	window.location = "/admin/edit_showcase.php" ;
									break ;
				case 'msmg'		:	window.location = "/admin/edit_msmgraphics.php" ;
									break ;
				default		:	window.location = "/admin/index.php" ;
									break ;
			
			}
	
		}
	}
	
}
















//toggle & update functions
function ShowOnHome(a_ID,table,state){
	var url = "/admin/actions/toggle_show_on_home.php?ID="+a_ID+"&table="+table+"&state="+state ; // The server-side script
	//alert(url) ;
	http.open("GET", url, true) ;
	http.onreadystatechange = updatedShowOnHome ;
	http.send(null) ;
}

function Activated(a_ID,state,table){
	var url = "/admin/actions/toggle_activated.php?table="+table+"&ID="+a_ID+"&state="+state ; // The server-side script
	//alert(url) ;
	http.open("GET", url, true) ;
	http.onreadystatechange = (table == 'showcase')?updatedActivated:reloadPage ;
	http.send(null) ;
}

function UpdateShowcaseShowOnHome(){
	var url = "/admin/actions/returnShowcaseSOH.php" ;
	http.open("GET", url, true) ;
	theDiv = "showonhome" ;
	http.onreadystatechange = updateDivContent ;
	http.send(null) ;
}

function Projectography(s_ID,b_ID){
	var url = "/admin/actions/updateProjectography.php?s_ID="+s_ID+"&b_ID="+b_ID ;
	//alert(url) ;
	http.open("GET", url, true) ;
	//http.onreadystatechange = alertResponse ;
	http.send(null) ;
}

function MSMBios(m_ID,b_ID){
	var url = "/admin/actions/updateMSMBios.php?m_ID="+m_ID+"&b_ID="+b_ID ;
	//alert(url) ;
	http.open("GET", url, true) ;
	//http.onreadystatechange = alertResponse ;
	http.send(null) ;
}

function WhodunnitAnswer(wdi_ID,bID_answer){
	var url = "/admin/actions/updateWhodunnit.php?ID="+wdi_ID+"&bID_answer="+bID_answer ;
	//alert(url) ;
	http.open("GET", url, true) ;
	//http.onreadystatechange = alertResponse ;
	http.send(null) ;
}

function UpdateLBCaption(ID){
	var sID = document.getElementById("pageID").value ;
	var caption = escape(ReturnNoDQuoteString(document.getElementById('currcaption').value)) ;
	var url = "/admin/actions/updateCaption.php?sID="+sID+"&ID="+ID+"&caption="+caption ;
	/*alert(url) ;*/
	http.open("GET", url, true) ;
	theDiv = "showcaseimgs" ;
	http.onreadystatechange = updateDivContent ;
	http.send(null) ;
	//hide the form
	document.getElementById('lightboxcaption').style.display = 'none' ;
	document.getElementById('tempformdesc').style.display = 'block' ;
}

//delete function
function DeleteLBImg(ID){
	if(confirm("Are you sure you want to delete this? This cannot be undone. Seriously...")){
		
		var table = 'showcase_images' ;
		var sID = document.getElementById("pageID").value ;
		var url = "/admin/actions/deleteNode.php?sID="+sID+"&ID="+ID+"&table="+table ;
		//alert(url) ;
		http.open("GET", url, true) ;
		theDiv = "showcaseimgs" ;
		http.onreadystatechange = updateDivContent ;
		http.send(null) ;
		//hide the form
		document.getElementById('lightboxcaption').style.display = 'none' ;
		document.getElementById('tempformdesc').style.display = 'block' ;
		
	}
}

function updateMSMCaption(ID){
	var caption = escape(ReturnNoDQuoteString(document.getElementById('currcaption').value)) ;
	var url = "/admin/actions/updateMSMCaption.php?ID="+ID+"&caption="+caption ;
	/*alert(url) ;*/
	http.open("GET", url, true) ;
	http.onreadystatechange = reloadPage ;
	http.send(null) ;
}

//delete function
function deleteMSMGraphic(ID){
	if(confirm("Are you sure you want to delete this? This cannot be undone. Seriously...")){
		
		table = "msmg" ;
		var url = "/admin/actions/deleteNode.php?ID="+ID+"&table=msm_graphics" ;
		//alert(url) ;
		http.open("GET", url, true) ;
		http.onreadystatechange = goToPage ;
		http.send(null) ;		
	}
}











//navigation functions
function JumpToBio(a_ID){
	window.location = "/admin/edit_single_bio.php?key="+a_ID ;
}

function JumpToShowcase(a_ID){
	window.location = "/admin/edit_single_showcase.php?key="+a_ID ;
}









//delete function
function DeleteNode(ID,tbl){
	if(confirm("Are you sure you want to delete this? This cannot be undone. Seriously...")){
		
		table = tbl ;
		var url = "/admin/actions/deleteNode.php?ID="+ID+"&table="+tbl ;
		//alert(url) ;
		http.open("GET", url, true) ;
		http.onreadystatechange = (arguments.length == 3)?goToPage:reloadPage ;
		http.send(null) ;
		
	}
}









//upload stuff
function uploadFile(useID,obj) {
	//var uploadDir = obj.value;
	uploaderId = useID+obj.name;
	uploader = obj.name;
	
	//uploadDirectory = document.getElementById('formName'+obj.name).uploadDirectory.value ;
	document.getElementById(useID).submit();
	document.getElementById(uploaderId).innerHTML = '<blink><strong class="red">File is Uploading, Please Wait</strong></blink>';
	//traceUpload(uploadDir, obj.name);	
}























function adjustRows(textarea){
	textarea.rows = textarea.scrollHeight / 18 ;
	if(document.getElementById( "feedback" )) document.getElementById( "feedback" ).innerHTML = textarea.scrollHeight+" : "+textarea.clientHeight+" :: "+textarea.rows ;
}









//form validation
function clearDefault(element,defaultString){
	if(element.value == defaultString) element.value = '' ;
}

function checkDate(element){	
	splitDate = element.value.split("/") ;
	
	if(splitDate.length != 3 || splitDate[0].length != 2 || splitDate[1].length != 2 || splitDate[2].length != 4){
		alert("Please enter date in correct format: MM/DD/YYYY") ;
		element.value = '' ;
		element.focus() ;
	}
}

function updateHeading(string){
	string = (string == '')?'<blink class="red">Enter a name</blink>':string ;
	document.getElementById( "pageTitle" ).innerHTML = string ;	
}

function updateCurrentLightbox(imgID,numImg){
	
	for(var x = 0; x<numImg; x++){
		
		document.getElementById( "lb"+x ).className = "" ;
		
	}
	
	//select the image
	document.getElementById( imgID ).className = "current" ;
	
	//get and display the info
	document.getElementById('lbID').value = document.getElementById( imgID ).name ;
	document.getElementById('currcaption').value = document.getElementById( imgID ).title ;
	
	//show the form
	document.getElementById('lightboxcaption').style.display = 'block' ;
	document.getElementById('tempformdesc').style.display = 'none' ;
	
	//set the values for the buttons
	document.getElementById('lbSave').href = "Javascript:UpdateLBCaption('"+document.getElementById( imgID ).name+"');" ;
	document.getElementById('lbDrop').href = "Javascript:DeleteLBImg('"+document.getElementById( imgID ).name+"');" ;
	//
	
}












