var checkflag = "false";
function check(field) {
	if (checkflag == "false")
	{
		for (i = 0; i < field.length; i++)
		{
			field[i].checked = true;
		}
		
		checkflag = "true";
   
		return "Uncheck All";
	}
	else
	{
		for (i = 0; i < field.length; i++)
		{
			field[i].checked = false;
		}
		
		checkflag = "false";
		
		return "Check All";
	}
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function versionSwap(id, title, time, mediumsrc, fullsrc, desc, Tlink, a, edtLink)
{
	//$("scratchImage").src = mediumsrc;
	
	flash = getFlashMovieObject("imageViewer");
	
	flash.SetVariable("image2", mediumsrc);

	//flash.LoadMovie(1, mediumsrc);
	
	$("scratchTitle").innerHTML = unescape(title);
	$("scratchDesc").innerHTML = unescape(desc);
	$("scratchFullLink").href = Tlink;
	
	li = JQ("#editPiece").get();
	if (li.length>0)
		li[0].href = edtLink;
	
	JQ("#loading").show();
	/*
	iW = $("scratchImage").width;
	iH = $("scratchImage").height;
	i = findPos($("scratchImage"));
	
	tp = i[1]+ parseInt(iH/2);
	
	JQ("#loaging").css("top", tp+"px");
	*/


	//alert(mediumsrc);

	
	t = findPos(a);
	c = findPos($("mycarousel"));
	
	d = t[0]-c[0]+35;
	
	JQ("#thumb-current").animate({
   	left: d }, 500);
}

function editName(id, container, toedit)
{
	Element.hide(container);
	
	new Insertion.After(container, '<div id="editor_'+id+'" style="float: left; width: auto;">Loading...</div>');
	
	var oldname = $(toedit).innerHTML;
	oldname = oldname.replace(/(<([^>]+)>)/ig,"");
	
	var form = '<input type="text" id="name_'+id+'" value="'+oldname+'" style="margin: 5px 0 0 5px;width:120px;" />';
	var save = '<input type="button" class="button" value="Save Changes" id="save_'+id+'" style="margin: 5px 0 0 5px;width: auto;height: 25px;clear:left;display:inline;float:none;" />';
	var cancel = '<input type="button" class="button" value="Cancel" id="cancel_'+id+'" style="margin: 5px 0 0 5px;width: auto;height: 25px;display:inline;float:none;" />';
	
	$('editor_'+id).innerHTML = form+save+cancel;
	
	Event.observe('save_'+id, 'click', function(){saveChanges(id, toedit, container, 'name_'+id, 'http://beta.creativestem.com/do/edit_spname.php')});
	Event.observe('cancel_'+id, 'click', function(){cleanUp(id, container)});
}

function editComment(id, container, toedit)
{ 
	Element.hide(container);

	new Insertion.After(container, '<div id="editor_'+id+'">Loading...</div>');
	
	var oldcomment = $(toedit).innerHTML;
	oldcomment = oldcomment.replace(/(<([^>]+)>)/ig,"")
	
	var textarea = '<div class="text"><textarea id="newbody_'+id+'">'+oldcomment+'</textarea>';
	var save = '<br><input type="button" value="Save Changes" id="save_'+id+'" class="button" />';
	var cancel = '<input type="button" Value="Cancel" id="cancel_'+id+'" class="button" /></div>';
	$('editor_'+id).innerHTML = textarea+save+cancel;
	
	Event.observe('save_'+id, 'click', function(){saveChanges(id, toedit, container, 'newbody_'+id, 'http://www.creativestem.com/do/edit_comment.php')});
	Event.observe('cancel_'+id, 'click', function(){cleanUp(id, container)});

}

function editCommentF(id, container, toedit)
{ 
	Element.hide(container);

	new Insertion.After(container, '<div id="editor_'+id+'">Loading...</div>');
	
	var oldcomment = $(toedit).innerHTML;	
	
	oldcomment = oldcomment.replace(/(<([^>]+)>)/ig,"")
	
var textarea = '<div class="text"><textarea id="newbody_'+id+'" cols="40" rows="5" class="form_back">'+oldcomment+'</textarea>';
	var save = '<br><input type="button" value="Save Changes" id="save_'+id+'" class="button2" />';
	var cancel = '<input type="button" Value="Cancel" id="cancel_'+id+'" class="button2" /></div>';
	$('editor_'+id).innerHTML = textarea+save+cancel;
	
	Event.observe('save_'+id, 'click', function(){saveChanges(id, toedit, container, 'newbody_'+id, 'http://dev.creativestem.com/do/edit_comment_forum.php')});
	Event.observe('cancel_'+id, 'click', function(){cleanUp(id, container)});

}

function cleanUp(id, tosave){
     Element.remove('editor_'+id);
     Element.show(tosave);
}
 
function saveChanges(id, toupdate, container, inputid, url){
	var new_body = escape($F(inputid));

	
	$('editor_'+id).innerHTML = "Saving...";
	
	var success = function(t){ cleanUp(id, container);editComplete(t, toupdate);}
	var failure = function(t){ editFailed(t, container);}

	var pars = 'id=' + id + '&new_body=' + new_body;

	var myAjax = new Ajax.Request(
					url,
					{
						method:'post',
						postBody:pars,
						onSuccess:success,
						onFailure:failure
					});
}

function editComplete(t, toupdate){
	$(toupdate).innerHTML = t.responseText;
}

function editFailed(t, toupdate){
	$(toupdate).innerHTML = '<div class="error">Sorry, the update failed.</div>';
	cleanUp(id);
}

function loadTab(changeTo, div, i)
{
	$(div).innerHTML = 'Loading...';

	var url = 'http://beta.creativestem.com/ajax_forms/'+changeTo;
	var pars = 'ajax=yes&u='; //<?=$u?>';

	switchTab(i);

	var myAjax = new Ajax.Updater(
					{ success: div },
					url,
					{
						method: 'get',
						parameters: pars,
						onFailure: reportError
					});

}

function switchTab(i){

	var oldLink = $('optiontablist').getElementsByTagName('a');
	var nodes = $A(oldLink);
	nodes.each(function(node){
    	Element.removeClassName($(node), 'current');
	});

	var myLink = $('tab_'+i);
	Element.addClassName(myLink, 'current');
}

function reportError(request)
{
	alert('Sorry. There was an error.');
}

function redirect(url){

	window.location = url;

}

String.prototype.trim = function(c, t){
    return c = "[" + (c == undefined ? " " : c.replace(/([\^\]\\-])/g, "\\\$1")) + "]+",
    this.replace(new RegExp((t != 2 ? "^" : "") + c + (t != 1 ? "|" + c + "$" : ""), "g"), "");
};

function isURL(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}