// Global Variables
var ajaxScript = './php/ajax/ajax_backend.php';
var pass = "a897";

// Global Helpers
function switchImage(which, src) {
	$(which).src = src;
}

function isOdd(num) {
  return (num % 2);
}

function rtrim(string) {
	while (string.substring(string.length-1, string.length) == ' ') {
		string = string.substring(0,string.length-1);
	}
	return string;
}

function ltrim(string) {
	for (var i=0; i < string.length; i++) {
		if (string.substring(i, i+1) != ' ') {
			break;
		}
	}

	return string.substring(i);
}

function toggleDisplay(which) {
	if ($(which).getStyle('display') == 'block') {
		$(which).setStyle({display: 'none'});
	} else {
		$(which).setStyle({display: 'block'});
	}
}

function changeClass(which, what) {
	$(which).className = what;
}

function disableButton(which, className) {
	$(which).disabled = true;
	$(which).className = className;
}

function enableButton(which, className) {
	$(which).disabled = false;
	$(which).className = className;
}

function isset(varname){
  return (typeof(window[varname]) != 'undefined');
}

function checkIfEmpty(id, filler) {
	if ($(id)) {
		if ($F(id).blank()) {
			$F(id) = (!filler) ? " " : filler;
		}
	}
	
	return;
}

// Input Field Change
Form.Element = Object.extend(Form.Element, {
	validate: function(id, valid) {
		var good = '#999';
		var bad = '#A00';
	
		if ($(id)) {
			switch (valid) {
				case false:
					color = bad;
					width = '2px';
					break;
				case true:
				default:
					color = good;
					width = '1px';
					break;
			}
			
			$(id).setStyle({ borderWidth: width });
			$(id).setStyle({ borderColor: color });
			
		}
	},
	
	resetPlaceholders: function(id) {
		if ($(id)) {
			$(id).reset();
			el = $(id).getInputs();
			
			for (var i = 0; i < el.length; i++) {
				attr = el[i].getAttribute('placeholder');
				
				if (attr != null) {
					el[i].value = attr;
					el[i].className = 'placeholder';
				}
			}
		}
	},

	clearPlaceholders: function(id) {
		if ($(id)) {
			el = $(id).getInputs();
			
			for (var i = 0; i < el.length; i++) {
				attr = el[i].getAttribute('placeholder');
				
				if (attr != null && el[i].value == attr) {
					el[i].value = ''
				}
			}
		}
	}


});

// Escape out Unicode Crap
function escapeURI(value) {
	value = encodeURIComponent(value);
	
	value = value.replace(/'/gi, "''");
	value = value.replace(/\%22/gi, '\"');
	value = value.replace(/\%5C/gi, "\\\\");

	value = value.replace(/\%0A/gi, "\n");

	return value;
}

function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	
	return str;
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	
	return str;
}

function htmlEncode(value) {
	value = value.replace(/'/gi, "&apos;");
	value = value.replace(/"/gi, "&quot;");
	
	return value;
}

// Image Preload
var imageCache = new Array();

function cacheImage(imageList) {
	var i, img;
	
	if (typeof imageList != 'undefined') {
		for (i = 0; i < imageList.length; i++) {
			img = new Image();
			img.src = imageList[i];
			
			imageCache.push(img);
		}
	} else {
		var complete = 1;
		var total = 1;
		
		if (imageCache.length > 0) {
			total = imageCache.length;
			complete = 0;
			
			for (i = 0; i < imageCache.length; i++) {
				if (imageCache[i].complete == true) {
					complete++;
				}
			}
		}
		
		if ($('stat')) {
			$('stat').innerHTML = parseInt((complete / total), 10)*100 + " %";
		}
		
		return parseInt((complete / total), 10);
	}
}
