//function to remove specific classname
function removeClass(string, classname) {
	var classes = string.split(' '), ret = '';

	for(var i=0;i<classes.length;i++) {
		if(classes[i] != classname) {
			ret += ' ' + classes[i];
		}
	}
	
	return ret;
}

function addStyle(style){
	document.write("<style>"+style+"<\/style>")
}

//add a classname
function addClass(string, classname) {
	if (string == '') {
		return classname;
	} else {
		return string + ' ' + classname;
	}
}

function toggleClass(string, classname) {
	var classes = string.split(' '), ret = '';
	var classin = false;
	for(var i=0;i<classes.length;i++) {
		if(classes[i] == classname) {
			classin = true;
			break;
		}
	}
	
	return classin?removeClass(string, classname):addClass(string,classname);
}

String.prototype.isEmail = function() {
	return this.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1;
}

String.prototype.isGreater = function(val) {
	return this >= val;
}

String.prototype.isLesser = function(val) {
	return this <= val;
}

String.prototype.isNumeric = function () {
	return this.search(/^[0-9]*$/) != -1;
}

String.prototype.makeNumeric = function() {
	return this.replace(/[^0-9]+/g,'');
}

String.prototype.isDecimal = function () {
	return this.search(/^[0-9\.]*$/) != -1;
}

String.prototype.isAlpha = function () {
	return this.search(/^[a-zA-Z]*$/) != -1;
}

String.prototype.isAlphaNum = function () {
	return this.search(/^[a-zA-Z0-9]*$/) != -1;
}

String.prototype.ucFirst = function () {
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

String.prototype.ordinal = function() {
	if (this.length > 1) {
		var end = this.substring(this.length - 2, this.length);
		var last = this.substring(this.length - 1, this.length);
	} else {
		var end = this;
		var last = this.substring(this.length - 1, this.length);
	}

	if (last == 1 && end != 11 ) {
		return 'st';
	}else if (last == 2 && end != 12) {
		return 'nd';
	} else if (last == 3 && end != 13) {
		return 'rd';
	} else {
		return 'th';
	}
}

function removeElement(id) {
	if (!$(id)) return;
	
	$(id).parentNode.removeChild($(id));
}


// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function openWin(args) {
	name = args.name?args.name:'name';
	attr = args.attr?args.attr:'height=200,width=150';
	url = args.url?args.url:'about:blank';
	newwindow=window.open(url, name, attr);
	if (window.focus) {newwindow.focus()}
}
//http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
	
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			if (func) func(); //itomic modification
		}
	}
}

function blurText(text){
	
	if(text.value.trim() == ''){
		text.value = text.defaultValue;
	}
}

function focusText(text){
	if(text.value.trim() == text.defaultValue){
		text.value='';
	}
}

//needs mootools installed
function playYoutube(params) {
	opt = Object.extend({color:'#FFFFFF', width:'425', height:'355', flashplayer:'6', wmode:'transparent', flashid:true}, params);
	
	if (!opt.id) return false;
	if (!opt.url) return false;
	if (!$(opt.id)) return false;
	
	if (opt.flashid === true) opt.flashid = opt.id + 'swf';
	
	var ytFlash = new SWFObject(opt.url, opt.flashid, opt.width, opt.height, opt.flashplayer, opt.color);
	ytFlash.addParam("wmode", opt.wmode);
	ytFlash.useExpressInstall('/clientlib/expressinstall.swf');
	ytFlash.write(opt.id);
}

function zap(el){
	if (el && el.style) el.style.display = "none"
}

function unzap(el){
	if (el && el.style) el.style.display = ""
}

function workzap(el){
	element = document.getElementById(el);
	if(element.style.display == 'none'){
		unzap(element);
	}else{
		zap(element);
	}
}