function init(){
	StripeRecentUpdates() ;
	setInterval("KeepTagsScrolled()", 250);
	WriteFooter() ;
	//if(bName() != 1) window.onbeforeunload = confirmExit ;
	//take of IE not hiding form submit button text
	//if(bName() == 1) document.getElementById( 'tabformsubmit' ).value = "" ;
}

function bName() {
	// return 1 for Internet Explorer
	if (navigator.appName == "Microsoft Internet Explorer"){
		return 1;
	// return 2 for Navigator
	} else if (navigator.appName == "Netscape"){
		return 2;
	// return 0 for other browsers
	} else {
		return 0;
	}
}

function StripeRecentUpdates(){
	allLi = document.getElementById( 'change_logs' ).getElementsByTagName('a') ;
	
	//stripe every other a
	zebra = 1 ;
	for(x=0;x<allLi.length;x++){
		if(zebra < 0){
			allLi[x].className = "zebra" ;	
		}
		zebra *= -1 ;
	}
	
	allOl = document.getElementById( 'change_logs' ).getElementsByTagName('ol') ;
	
	//stripe every other ol
	zebra = 1 ;
	for(x=0;x<allOl.length;x++){
		if(zebra < 0){
			allOl[x].className = "zebra" ;	
		}
		zebra *= -1 ;
	}
	
}

function uploadFile(useID,obj) {
	uploaderId = useID+obj.name;
	uploader = obj.name;
	
	document.getElementById(useID).submit();
	document.getElementById(uploaderId).className = 'noshow';
	document.getElementById( "status" ).innerHTML = '<h2 class="warning">Image is being uploaded. Please wait.</h2>' ;
}

function confirmExit(link){
	var conf = getHTTPObject() ;
	var url = "/actions/check_for_changes.php" ;
	conf.open("GET",url, true) ;
	conf.onreadystatechange = function(){
		if(conf.readyState == 4){
			if(conf.responseText){
				if(confirm('All changes made to this library will be lost if you leave this page before saving the changes from the "Commit Changes" tab. Click cancel to return to the library and save your changes. If you want to disregard these changes, click OK')){
					window.location = link ;
				}
			} else {
				window.location = link ;
			}
		}
	}
	conf.send(null) ;
}

function CheckForXNA(libID){
	//check for xna and xe in the lib
	var x = getHTTPObject() ;
	var url = "/actions/get_xna_status.php?libID=" ;
	x.open("GET",url + escape(libID), true) ;
	x.onreadystatechange = function(){
		if(x.readyState == 4){
			//if there is, ask the user to select
			if(x.responseText == "both"){
				var theSpan = document.getElementById( "xnachoice" + libID ).getElementsByTagName('span') ;
				var theLinks =  theSpan[0].getElementsByTagName('a') ;
				theSpan[0].className = "" ;
				theLinks[0].href = "edit_library.php?lib="+libID+"&loc=xna" ;
				theLinks[1].href = "edit_library.php?lib="+libID+"&loc=xe" ;
			} else if(x.responseText == "xnaonly") {		
				//else, just go edit the library
				window.location = "edit_library.php?lib="+libID+"&loc=xna" ;
			} else {
				//alert("This library is not available at this time.") ;
			}
		}
	}
	x.send(null) ;
}

function AlertUpdates(){
	var u = getHTTPObject() ;
	var url = "/actions/get_updates_detail.php" ;
	u.open("GET",url,true) ;
	u.onreadystatechange = function(){
		if(u.readyState == 4){
			alert(u.responseText) ;
		}
	}
	u.send(null) ;
}

function SelectFolderType(collection){
	var sft = getHTTPObject() ;
	var url = "/actions/checkForSubCat.php" ;
	sft.open("GET", url, true) ;
	sft.onreadystatechange = function(){
		if(sft.readyState == 4){
			if(sft.responseText == "fail"){
				AddFolder() ;
			} else {
				document.getElementById( 'foldertype' + collection ).className = 'foldertype' ;
			}
		}
	}
	sft.send(null) ;
}


function GetFolderTitle(ID){
	var t = getHTTPObject() ;
	var url = "/actions/editing_folder.php?action=edit&ID=" ;
	t.open("GET", url + escape(ID), true) ;
	t.onreadystatechange = function () {
		if (t.readyState == 4){
			//document.getElementById( 'edit_title' ).value = t.responseText ;
			document.forms[0].title.value = t.responseText ;
		}
	}
	t.send(null) ;
}

function DeleteFolder(ID){
	var url = "/actions/editing_folder.php?action=delete&ID=" ;
	http.open("GET", url + escape(ID), true) ;
	http.onreadystatechange = function(){
		if(http.readyState == 1){
			document.getElementById( 'status' ).innerHTML = '<h2 class="warning">Working&hellip;</h2>' ;
		} else if(http.readyState == 4){
			document.getElementById( 'status' ).innerHTML = '<h2>Folder Deleted Successfully!</h2>' ;
			window.setTimeout("document.getElementById( 'status' ).innerHTML = ''",3500) ;
			document.getElementById('content').innerHTML = http.responseText ;
			GetEditCount();
		}	
	}
	http.send(null) ;
}

function GetLinkData(ID){
	var d = getHTTPObject() ;
	var url = "/actions/editing_link.php?action=edit&ID=" ;
	d.open("GET", url + escape(ID), true) ;
	d.onreadystatechange = function() {
		if (d.readyState == 4){
			var a_result = d.responseText.split("|") ;
			document.forms[0].title.value = a_result[0] ;
			document.forms[0].link.value = a_result[1] ;
			document.getElementById( 'linktest' ).href = a_result[1] ;
			document.forms[1].slug.value = a_result[2] ;
			if(a_result[3] == "true"){
				document.getElementById( 'hasslideout_yes' ).checked = 'checked' ;
				document.getElementById( 'hasslideout_no' ).checked = '' ;
				document.getElementById( 'slideout_inputs' ).className = '' ;
				document.forms[0].slideout_title.value = a_result[4] ;
				document.forms[0].description.value = a_result[5] ;
				if(a_result[6] == "internal"){
					document.getElementById( 'usage_internal' ).checked = 'checked' ;
					document.getElementById( 'usage_external' ).checked = '' ;
				} else {
					document.getElementById( 'usage_internal' ).checked = '' ;
					document.getElementById( 'usage_external' ).checked = 'checked' ;
				}
				if(a_result.length > 6){
					document.getElementById( 'slideout_thumbnail' ).className = 'hasthumb' ;
					document.getElementById( 'thumbnail_image' ).src = "lib-ref/"+a_result[2]+".jpg" ;
					document.getElementById( 'thmb_img_form' ).className = '' ;
				} else {
					document.getElementById( 'slideout_thumbnail' ).className = 'nothumb' ;	
				}
			} else {
				document.getElementById( 'hasslideout_yes' ).checked = '' ;
				document.getElementById( 'hasslideout_no' ).checked = 'checked' ;
				document.getElementById( 'slideout_inputs' ).className = 'noshow' ;
				document.getElementById( 'edit_slideout_title').value = '' ;
				document.getElementById( 'edit_slideout_desc').value = '' ;
				document.getElementById( 'usage_internal' ).checked = '' ;
				document.getElementById( 'usage_external' ).checked = '' ;
			}
		}
	}
	d.send(null) ;
}

function DeleteLink(ID){
	var url = "/actions/editing_link.php?action=delete&ID=" ;
	http.open("GET", url + escape(ID), true) ;
	http.onreadystatechange = function(){
		if(http.readyState == 1){
			document.getElementById( 'status' ).innerHTML = '<h2 class="warning">Working&hellip;</h2>' ;
		} else if(http.readyState == 4){
			document.getElementById( 'status' ).innerHTML = '<h2>Link Deleted Successfully!</h2>' ;
			window.setTimeout("document.getElementById( 'status' ).innerHTML = ''",3500) ;
			document.getElementById('content').innerHTML = http.responseText ;
			GetEditCount();
		}	
	}
	http.send(null) ;
}

function GetEditCount(){
	var ec = getHTTPObject() ;
	var url = "/actions/get_num_edits.php" ;
	ec.open("GET", url, true) ;
	ec.onreadystatechange = function(){
		if (ec.readyState == 4){
			allEditStrings = document.getElementsByClassName( 'num_edits' ) ;
			for(x=0;x<allEditStrings.length;x++){
				allEditStrings[x].firstChild.nodeValue = ec.responseText ;
			}
		}
	}
	ec.send(null) ;
}

function WriteFooter(type,ID,index){
	//if you're clicking on a link or subcat that is set to delete the 'add link' button must be shut off

	var f = getHTTPObject() ;
	var url = "/actions/update_current_menu_pos.php?type="+type+"&index="+index+"&value=" ;
	f.open("GET", url + escape(ID), true) ;
	f.onreadystatechange = function () {
		if (f.readyState == 4){
			document.getElementById( 'breadcrumbs' ).innerHTML = f.responseText ;
		}
	}
	f.send(null) ;
}

function AddFolder(){
	var url = "/actions/addFolder.php" ;
	http.open("GET", url, true) ;
	http.onreadystatechange = function(){
		if(http.readyState == 1){
			document.getElementById( 'status' ).innerHTML = '<h2 class="warning">Working&hellip;</h2>' ;
		} else if(http.readyState == 4){
			document.getElementById( 'status' ).innerHTML = '<h2>Folder Added Successfully!</h2>' ;
			window.setTimeout("document.getElementById( 'status' ).innerHTML = ''",3500) ;
			var theID = http.responseText ;
			UpdatePage(true) ;
			EditFolder(theID) ;
		}	
	}
	http.send(null) ;
}

function AddSubCat(){
	var url = "/actions/addSubCat.php" ;
	http.open("GET", url, true) ;
	http.onreadystatechange = function () {
		if(http.readyState == 1){
			document.getElementById( 'status' ).innerHTML = '<h2 class="warning">Working&hellip;</h2>' ;
		} else if(http.readyState == 4){
			document.getElementById( 'status' ).innerHTML = '<h2>Subcategory Added Successfully!</h2>' ;
			window.setTimeout("document.getElementById( 'status' ).innerHTML = ''",3500) ;
			var theID = http.responseText ;
			UpdatePage(true) ;
			EditFolder(theID) ;
		}
	}
	http.send(null) ;
}

function AddLink(){
	var url = "/actions/addLink.php" ;
	http.open("GET", url, true) ;
	http.onreadystatechange = function(){
		if(http.readyState == 1){
			document.getElementById( 'status' ).innerHTML = '<h2 class="warning">Working&hellip;</h2>' ;
		} else if(http.readyState == 4){
			document.getElementById( 'status' ).innerHTML = '<h2>Link Added Successfully!</h2>' ;
			window.setTimeout("document.getElementById( 'status' ).innerHTML = ''",3500) ;
			var theID = http.responseText ;
			UpdatePage(true) ;
			EditLink(theID) ;
		}	
	}
	http.send(null) ;
}

function UpdatePage(NoFormSwitch){
	var h = getHTTPObject() ;
	var url = "/actions/updatePage.php" ;
	h.open("GET", url, true) ;
	h.onreadystatechange = function (){
		if(h.readyState == 4){
			if(!NoFormSwitch) SwapToEditWarning() ;
			GetEditCount();
			document.getElementById('content').innerHTML = h.responseText ;
		}
	}
	h.send(null) ;
}

function UpdateUserList(){
	var uul = getHTTPObject() ;
	var url = "/actions/updateUsers.php" ;
	uul.open("GET", url, true) ;
	uul.onreadystatechange = function (){
		if(uul.readyState == 4){
			//document.getElementById('edit_user_table').innerHTML = uul.responseText ;
			window.location.reload( false );
		}
	}
	uul.send(null) ;
}








function KeepTagsScrolled(){
	var tabDiv = new getObj('tabs');
	var topMargin = (bName() == 1)?20:100 ;
	if(getScrollY() > 200){
		tabDivTop = topMargin + getScrollY() ;
		tabDiv.style.top = tabDivTop + "px" ;
	} else if(getScrollY() == 0){
		tabDiv.style.top = topMargin + "px" ;
	}
	//alert("You're at " + getScrollY() + " pixels. The 'tabs' div should be at "+tabDivTop);

}

function getObj(name){
	if (document.getElementById){
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all){
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers){
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function getScrollY(){
  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 ];
  return scrOfY ;
}

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
	var classes = elem[i].className;
	if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
}

function ajaxform(thisform,formhandler)
{
    //General Purpose Ajax form submitter.
    //Written by Carl(bag) @ Thybag.co.uk

    // Set up data variable
    var formdata = "";

    // Set up Ajax request variable
    try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}

    // Loop through form fields
    for (i=0; i < thisform.length; i++)
    {
         //Build Send String
         if(thisform.elements[i].type == "text"){ //Handle Textbox's
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }else if(thisform.elements[i].type == "textarea"){ //Handle textareas
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }else if(thisform.elements[i].type == "checkbox"){ //Handle checkbox's
                 formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].checked + "&";
         }else if(thisform.elements[i].type == "radio"){ //Handle Radio buttons
                  if(thisform.elements[i].checked==true){
                     formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].value + "&";
                  }
         }else{
                  //finally, this should theoretically this is a select box.
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }
    }

    //Send Ajax Request
    xmlhttp.onreadystatechange = function(){
   //Check page is completed and there were no problems.
   if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
          //What to do once the form is submitted - to inform the user.
          alert("Your form has been successfully submitted");

   }
    }
    //Make connection
    xmlhttp.open("POST", formhandler);
    //Set Headers
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //Send data
    xmlhttp.send(formdata);
    //stops form from submitting normally
    return false;
}


function getHTTPObject(){
	var xmlhttp; 
	/*@cc_on @if (@_jscript_version >= 5) try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @else xmlhttp = false; @end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) { xmlhttp = false; } 
	}
	
	return xmlhttp;
} 

var http = getHTTPObject(); // We create the HTTP Object
