// All scripts in this file require prototype
//       some Require scrip.ac.lious
//
// Copyright Alan Cole (Kal-El) 2008
//	   Encore CMS Js Lib
//
//  DO NOT STEAL, USE WITH CREDIT!

function randomString(len) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = len;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function randomnumString(len) {
	var chars = "0123456789";
	var string_length = len;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function buildCode(){	
	if(!$('name').present()){
		//alert('Sorry, you need to add a title before we can build a code');
		if(confirm('No title was found, would you like to generate a random code instead?')){
			$('code').value = randomString(5);
		}
	} else {
		var start = $('name');
		var ar = start.value.toArray();
		letters = [];
		letters.push(ar[0]);
		ar.each(function(s, index){
			if(s == " "){
				letters.push(ar[index + 1]);
			}
		});
		mylet = letters.toString(" ");
		mylet = mylet.sub(',', '',letters.length);
		mylet = mylet.toLowerCase();
		mylet = mylet + randomnumString(3);
		$('code').value = mylet;
		
	}
	checkcode($('code').value);
}

function checkcode(code){
	var url = './checkcode.php?code=' + code;
	 new Ajax.Request(url, {   
		method: 'get',   
		onSuccess: function(transport) {     
			if (transport.responseText.match("Code Found")){
				$('code').setStyle({ background: '#fdd' });
				alert('That code is already in use, please select the \'Auto Generate\' button or enter a new code');
			} else {
				$('code').setStyle({ background: '#dfd' });
			}   
		} 
	}); 
}

function makeeditable(ele,idx,hash){
	new Ajax.InPlaceEditor($(ele), 'update.php?idx='+idx+'&hash='+hash, { rows:5 });
}


var isselected = "";

function sselect(ele){
	uploader.doit();
	alert(ele);
	var backcolor = 'fff';
	var restore = 'ff00cc';
	if(isselected == ele){
		$(ele).setStyle({backColor: restore});
		isselected = "";
	} else {
		$('buttons').descendants().each(function(s){
			s.setStyle({backColor: restore});
			$(ele).setStyle({backColor: backcolor});
			isselected = ele;
		});
	}
}

function copy2clipboard(form,field){
	if (window.clipboardData) {
		alert("copied");
		window.clipboardData.setData("Text",document.form.field.value);
	} else {
		alert('Sorry, your browser does not support this function. Please right-click in the textbox and select \'select all\' and then right click again and select \'copy\'.');
	}
}

function doit(idx){
	window.location = "switch-account_"+ $F(idx) +".html";
}

function replace(ele, cont){
	alert($(ele).innerHTML);
	$(ele).innerHTML = cont;
}

function switch2textbox(ele,name){
	alert(name);
	var t = "<input type='text' name='"+name+"' />";
	document.getElementById(ele).innerHTML = t;
}

function change(id){
	var changeto = document.getElementById(id).innerHTML;
	document.getElementById('right').innerHTML = changeto;				
}