function checkIt(id)
{
	var el=$('#check'+id).attr('checked');
	
	if(!el)
	{
		//$('#title'+id).addClass('inputDisabled')
		//$('#user'+id).addClass('inputDisabled');
		
		$('#li'+id).remove();
		
	}
	else 
	{
		$("#sortable").append('<li id="li'+id+'"><div class="smallTab"><img src="/images/iconsBig/'+(id+1)+'.gif" class="handle" /></div></li>');
		$("#sortable").sortable( 'refresh' )
		//$('#title'+id).removeClass('inputDisabled')
		//$('#user'+id).removeClass('inputDisabled');
	}
	
	$('#title'+id).attr('disabled',!el);
	$('#user'+id).attr('disabled',!el);
}


function serial()
{

	var s = $("#sortable").sortable('toArray');
	
	var newArr = new Array();
	
	for(var i=0;i<s.length;i++)
	{
		s[i]=s[i].replace('li','');

		newArr.push({
			id:s[i],
			position:newArr.length,
			title: encodeURIComponent($('#title'+s[i]).val()),
			feed:encodeURIComponent($('#user'+s[i]).val())
		});
	}
	
	return newArr;
}

var lastSer="";

function getSortable(edit)
{
	var dat = serial();

	if(!dat.length)
	{
		//$('generating').style.display='none';
		//$('err').style.display='block';
		//$('err').innerHTML='You haven\'t chosen any service from the listed above!';
		showDialog('You haven\'t chosen any services for your widget!');
		return false;
	}
	
	var str=new Array();
	for(var i=0;i<dat.length;i++)
	{
		if(!dat[i].title || !dat[i].feed || (dat[i].id==5 && (!validateURL(decodeURIComponent(dat[i].feed)) || !validateURL($('#cUF').val()) )))
		{
			//$('generating').style.display='none';
			//$('err').style.display='block';
			//$('err').innerHTML="One of the services you've chosen is not filled in properly!";

			showDialog('One of the services you\'ve chosen is not filled in properly!');
			return false;
		}
		
		str[i]="sB["+i+"][id]="+dat[i].id+"&sB["+i+"][title]="+dat[i].title+"&sB["+i+"][feed]="+dat[i].feed;

		if(dat[i].id==5)
		{
			str[i]+="&sB["+i+"][customURL]="+encodeURIComponent($('#cUF').val());
		}
	}
	
	if(document.getElementById('emailEdit') && $('#emailEdit').val().length)
	{
		if(validateEmail($('#emailEdit').val()))
		{
			str[str.length]='emailEdit='+encodeURIComponent($('#emailEdit').val())+'&notifyMe='+($('#notifyMe').attr('checked')?'1':'0');
		}
		else
		{
			showDialog('The email address, you\'ve entered, is not valid');
			return false;
		}
	}
	
	if(edit) str.push('editLink='+edit);
	
	var tmpJoin = str.join("&");
	
	if(lastSer==tmpJoin) return false;
	lastSer=tmpJoin;


	$('#getCode').hide();
	showDialog('<img src="/images/18-0.gif" style="margin:20px 0px 0px 110px" />','Generating your widget');
	
	$.ajax({
	   type: "POST",
	   url: "/ajax/generate.aj.php",
	   data: tmpJoin,
	   dataType: 'json',
	   success: function(msg){
	   
			if(parseInt(msg.status)!=1)
			{
				$('#dialog').html(msg.txt);
			}
			else
			{
				$('#dialog').dialog('destroy');
				$('#responseTxt').html(msg.txt);
				$.scrollTo('#generate',800);
			}
		
	   }
	 });
}

var sweepChecks=true;
$(document).ready(function() {

	if(sweepChecks)
	{
		var i=0;
		while(document.getElementById('check'+i))
		{
			$('#check'+i).attr('checked',false);
			i++;
		}
	}
	else if(tabs.length)
	{
		var i=0;
		while(document.getElementById('check'+i))
		{
			if(!tabs[i])
			{
				$('#check'+i).attr('checked',false);
				i++;
				continue;
			}
			
			$('#check'+i).attr('checked',true);
			
			$('#title'+i).val(tabs[i].name);
			$('#title'+i).attr('disabled',false);
			
			$('#user'+i).val(i==5?tabs[i].feed:tabs[i].uname);
			$('#user'+i).attr('disabled',false);
			
			if(i==5)
			{
				$('#customURL').show();
				$('#cUF').val(tabs[i].customURL);
			}
			
			i++;
		}
	}

  $("#sortable").sortable({ 
    handle : '.handle',
	axis:'x',
	containment: 'document'
  });
  
  	$('#liDef').remove();
   

	$('#topBar .replaceLink').attr('onclick', '$.scrollTo(this.hash,800);return false;');
	
	
	$('#pagePeel').pagePeel({});

});

function validateEmail(str)
{
	if(!str) return false;
	return str.match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)?true:false;
}

function validateURL(str)
{
	if(!str) return false;
	return str.match(/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i) ? true : false;
}

function showDialog(str,strtitle)
{
	if(!strtitle) strtitle='Error message';

	$('#dialog').dialog('destroy');
	$('#dialog').show();
	$('#dialog').html(str);
	
	$("#dialog").dialog({
		resizable: false,
//		height:140,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.9
		},
		title:strtitle,
		buttons: {
			'OK': function() {
				//$(this).dialog('close');
				$(this).dialog( 'destroy' )
				$('#dialog').hide();

			}
		}
	});

}

function submitForm(el)
{
	el.disabled=true;
	
	if(!validateEmail($("#feedbackEmail").val()))
	{
		showDialog("Your feedback email is invalid!");
		el.disabled = false;
		return false;
	}

	
	if($("#feedbackMsg").val().length<=5)
	{
		showDialog("Your message is too short!");
		el.disabled = false;
		return false;
	}
	
	$('#js').val(jsCheck);
	
	document.forms.feedbackForm.submit();
}