$(function(){
	
	$("a.soclinks").each(function(){
		var id=this.id.replace(/soclink/,"");
		$(this).bind("mouseover",function(){
			$("#socbookmark"+id).slideDown();
		})
	});
	
	$("#dob_otz_button").click(function(){
		$(this).hide();
		$("#dob_otz_loading").show();
		d = $(this).parent().attr("id").split("_");
		$.post("/rb/add_form.php",{p:d[2],f:d[3]},function(data){
			$("#dob_otz_loading").hide();
			$("#dob_otz_form").html(data).slideDown();
		});
	});
	
	$("#add_form_submit").live("click",function(){data_check()});
	
	$("#add_form_cancel,#dob_otz_hide").live("click",function(){
		location.href='#top4';
		$("#dob_otz_button").fadeIn("slow");
		$("#dob_otz_form").slideUp();
		if(tinyMCE.getInstanceById('n6'))
			tinyMCE.execCommand('mceRemoveControl', false, 'n6');
	});
});
	

function get_ch_radio(radioObj){
	if(!radioObj) return "3";
	var radioLength = radioObj.length;
	if(radioLength == undefined){
		if(radioObj.checked) return radioObj.value; else return "3";
	}
	for(var i = 0; i<radioLength; i++) if(radioObj[i].checked) return radioObj[i].value;
	return "3";
}

function isEmpty(elem){
	if(elem.value.length == 0) return true; else return false;
}

function isNumeric(elem){
	var numericExpression=/^[0-9]+$/;
	if(elem.value.match(numericExpression)) return true; else return false;
}

function emailV(elem){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)) return true; else return false;
}

function data_check(){
	var ed=tinyMCE.get('n6');
	$(".form_error").slideUp();
	var four_digit=/^[0-9]{4}$/;
	var n=0;
	with(document.forms['add_form']){
		if(isEmpty(n3)){
			$("#n3_error").slideDown();
			n++;
		}
		if(isEmpty(n4)){
			$("#n4_error").slideDown();
			n++;
		}
		if(n5.value.length>0){
			if(!emailV(n5)){
				$("#n5_error").slideDown('slow');
				n++;
			}
		}
		var mod_n6='';
		if(tinyMCE.getInstanceById('n6')) mod_n6=tinyMCE.activeEditor.getContent();
		else mod_n6=n6.value;
		if(mod_n6.length==0){
			$("#n6_error").slideDown('slow');
			n++;
		}
		if(!isNumeric(n8)){
			n8.value="";
			$("#n8_error").slideDown('slow');
			n++;
		}
		if(!isNumeric(n9)){
			n9.value="";
			$("#n9_error").slideDown('slow');
			n++;
		}
		if(n>0){
			ed.setProgressState(0);
			return;
		}
		ed.setProgressState(1);
		$.post(
			'/rb/add_form.php',{
				'n0':0,
				'p':p.value,
				'n2':n2.value,
				'n3':n3.value,
				'n4':n4.value,
				'n5':n5.value,
				'n6':mod_n6,
				'n7':get_ch_radio(n7),
				'n8':n8.value,
				'n9':n9.value
			},
			function(data){
				location.href='#top4';
				$('#dob_otz_form').hide().html(data).fadeIn();
			}
		);
		ed.setProgressState(0);
	}
}
