// JavaScript Document
Event.observe(window, 'load', init, false);
var allFuncs = new Object();

// Used to set everything up
function init(){
	/*
	// Focus on the Search Box
	if (document.search)
		document.search.keywords.focus();
	
	// Make Nifty Corners
	if(NiftyCheck()){
		Rounded("div.formError","#FFFFFF","#E7C57B");
		Rounded("div.formSuccess","#FFFFFF","#D1E1E3");
	}
	*/
	
	// If there is a list of departments
	if ($('classified_images')){
		var url = 'manager/inc/connector/classified_image.php';
		
		// Get all the links in the department list
		imgs=$('classified_images').getElementsByTagName('p');
 		// Loop through all of them
 		for(i=0;i<imgs.length;i++){
 			// Check to see if they are links to go and edit the dept
 			if(left(imgs[i].id, 5) == "desc_"){
 				// Make the links editable in place
 				EditInPlace.makeEditable( {
					type: 'textarea',
					id: imgs[i].id,
					save_url: url
				});
				// Remove their original href
				//imgs[i].href = 'javascript:void(0);';
 			}
 		}
	}
		
	// Degradable AJAX
	disableActions();
}

function DisplayCaptcha(){
	$('recaptcha').setStyle({display: 'block'});
	$('recaptcha_response_field').focus();
}
function HideCaptcha(){
	$('recaptcha').setStyle({display: 'none'});
}
function JSCaptcha(){
	$('noJSCaptcha').setStyle({display: 'none'});
	$('JSCaptcha').setStyle({display: 'block'});
}

// Helper function to get the elements by class
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/**
* Delete the Classified Images
**/
function ClassifiedImageDelete(image_id) {
	// Call the script to add the user to the database
	var pars = 'action=DeleteImage&image_id=' + escape(image_id);
	var url = 'inc/connector/classified_image.php';
	var myAjax = new Ajax.Updater('classified_images', url, {method: 'post',parameters: pars, onComplete: init});
	
	// Return that everything went okay
    return true;
}

/**
* Automated Degradable AJAX.
* Will replace all special classes with OnClick Events
*/
function disableActions () { 
    var links = document.getElementsByTagName("a"); 
    for (i=0; i<links.length; i++){ 
        var part= links[i];
		var action = getAction(part.className);
		if (action != ""){ 
			part.onclick = allFuncs[getAction(part.className)];
			part.href = "javascript:void(0);";
		}	
    }
}
/**
* Shortcut to get the Javascript function out of the class name
*/
function getAction(name) {
    allNames = name.split(" ");
    for(x = 0; x < allNames.length; x++)
        if(left(allNames[x], 4) == "func"){
			return right(allNames[x], allNames[x].length - 4);
		}
    return "";
 }
 
 
/**
* Copy of vbscripts left function
*/
function left(str, n){
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}
/**
* Copy of vbscripts right function
*/
function right(str, n){
	if (n <= 0)
	   return "";
	else if (n > String(str).length)
	   return str;
	else {
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
}

/**
* Used to show status of AJAX reqests at work
*/
var myGlobalHandlers = {
	onCreate: function(){
		Element.show('systemWorking');
	},

	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.hide('systemWorking');
		}
	}
};
Ajax.Responders.register(myGlobalHandlers);

/**
* List of all the Dynamic Functions
*/
allFuncs["ReportBug"] = function() {
	report_bug = window.open("report_bug.php","","width=500,height=600,scrollbars,");
}

/**
* Accessing GET variables
*/
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  // Taken out you don't need this unless your doing testing 
 //alert('Query Variable ' + variable + ' not found');
 return false;
}

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );
    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }
    return str;
}

function MakeRounded(){
	// Make Nifty Corners
	if(NiftyCheck()){
		Rounded("div.formError","#FFFFFF","#E7C57B");
		Rounded("div.formSuccess","#FFFFFF","#D1E1E3");
	}
	
	return true;	
}

function PrintPage(){
	window.print(); 	
}