function validate()
{
	valid=true;
	form_obj = document.getElementById("form");
	inputs = form_obj.getElementsByTagName("input");
	selects = form_obj.getElementsByTagName("select");
	textareas = form_obj.getElementsByTagName("textarea");
	
	for (i=0; i < inputs.length; i++)
	{	
		if ((inputs[i].type=='text' || inputs[i].type=='textarea' || inputs[i].type=='password') && inputs[i].value=='' && (!isHidden(inputs[i])) && (!isOptional(inputs[i]))){
			valid=false;
		}

		if (inputs[i].type=='file' && (inputs[i].value=='' || isValidPictureExtension(inputs[i]))){
			valid=false;
		}
		
		if((inputs[i].id=='username_valid' || inputs[i].id=='email_valid') && inputs[i].value=='false')
		{
			valid=false;
		 	alert_error("Whoops! Please make sure that both your username and email are valid.");
		 	return false;
		}
	}
		
	for (i=0; i < selects.length; i++)
		if (selects[i].value=='' && (!isHidden(selects[i])) && (!isOptional(selects[i])) && (selects[i].className!='optional'))
		{	
			valid=false;
		}

	for (i=0; i < textareas.length; i++)
		if (textareas[i].value=='' && (!isHidden(textareas[i])) && (!isOptional(textareas[i])) && (textareas[i].className!='optional'))
		{	
			valid=false;
		}

 	if (valid==false)
 	{
	 	alert_error("Whoops! Please complete all fields and click continue.");
	 	return false;
 	}

	$('button.btn.large').addClass('disabled');
	$('button.btn.large').click(function() {return false;});
	
}

function clearDefaultText(obj)
{

	if(obj.value=='Type a list of invitee email addresses.')
	{
		obj.value='';
		$(obj).removeClass('gray-text');
		return;
	}
	
	if(obj.value=='')
	{
		obj.value='Type a list of invitee email addresses.';	
		$(obj).addClass('gray-text');
	}
}


function isOptional(obj){
	if(obj.className=="optional"){
		return true;	
	}
	else{
		return false;	
	}
}

function isValidPictureExtension(obj){
	return (!/(\.(gif|jpg|jpeg|bmp|png))$/i.test(obj.value));
}

function isHidden(obj){
	if (obj.parentNode.style.display=='none' || obj.style.display=='none')
		return true;
	else
		return false;
}

function isValidFullname(name){
	var str=name.split(/\s/g);

	if (name==null ||str.length<2)
		return false;
        else
		return true;
}

function isValidEmail(email){
        	if (email.length==0)
                  return false;
		var match = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9_\-]+\.+[a-zA-Z]+$/.test(email);
		if (match)
		   return true;
		else
		    return false;
}

function validateForgotPassword(){
		xajax_showForgotPassword(1);	
}


function toggle(parent_id){
		
	for (i=0; i<=(document.getElementById(parent_id).length-1);i++)
		if (document.getElementById(parent_id+'-'+i))
		hide(parent_id+'-'+i);

	if (document.getElementById(parent_id).value!='')
		$("#"+parent_id+'-'+document.getElementById(parent_id).value).fadeIn("slow");

	return false;
}


function toggleEducationTitles(parent_id){
	var education_level_text = new Array('What type of degree are you getting?','What is your most recent (or highest) degree?');
	var school_description_text = new Array('What are you studying? (for ex. Physics)','What did you study?');
	var school_name_text = new Array('What school are you studying at?','Where did you go to school?');

	if (document.getElementById(parent_id).value==1){
		document.getElementById('education_level_text').innerHTML=education_level_text[0];
		document.getElementById('school_description_text').innerHTML=school_description_text[0];
		document.getElementById('school_name_text').innerHTML=school_name_text[0];
	}
	else{
		document.getElementById('education_level_text').innerHTML=education_level_text[1];
		document.getElementById('school_description_text').innerHTML=school_description_text[1];
		document.getElementById('school_name_text').innerHTML=school_name_text[1];
	}


}


function toggleReligion(parent_id)
{
	$("#preference").fadeOut("slow");
	if (document.getElementById(parent_id).value!='' && document.getElementById(parent_id).value!=10 && document.getElementById(parent_id).value!=11)
	$("#preference").fadeIn("slow");
}


function toggleCity()
{
	hide("city");

	if (document.getElementById("state").value!='')
		show('city');
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function show(obj){
	document.getElementById(obj).style.display='block';
}

function hide(obj){
	document.getElementById(obj).style.display='none';
}

function toggleLoginForm(){
	if (document.getElementById("login").style.display=='none'){
			hide ('option1');
		 	show('option2');
		 	$("div#login").fadeIn('slow'); 
			document.getElementById("username_text").select();

	}
	else{
	 	$("div#login").fadeOut('slow', function(){hide('option2');show ('option1') }); 
		
	}
	
}

function alert(content){

	content = '<h2 style="margin-top:10px">Success</h2><div style="padding:20px 30px;"><div class="green p-10">'+content+'</div></div>';
	document.getElementById('FaceboxPopUp').innerHTML=content;
	setTimeout("jQuery.facebox(document.getElementById(\'FaceboxPopUp\').innerHTML)",1);
	setTimeout("jQuery(document).trigger(\'close.facebox\')",1500);
}

function alert_error(content){

	content = '<h2 style="margin-top:10px">Error</h2><div style="padding:20px 30px;"><div class="error" style="margin-top:0">'+content+'</div></div>';
	document.getElementById('FaceboxPopUp').innerHTML=content;
	setTimeout("jQuery.facebox(document.getElementById(\'FaceboxPopUp\').innerHTML)",1);
	setTimeout("jQuery(document).trigger(\'close.facebox\')",1500);
}

function hash(hashValue){
	window.location.hash=hashValue;
}

function hashNoReload(hashValue){
	window.location.hash=hashValue;
}
function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function requestEmailAccess()
{
	content = '<h2 style="margin-top:10px">Add Email Address</h2><div style="padding:20px 30px;"><div class="green" style="font-size:12px">Welcome! One last step. Please add your email address so that we can let you know when someone has sent you a message. We will never spam you and you can unsubscribe at any point in one click.</div><a href="complete?p=6" onclick="window.location=\'complete?p=6\'"><button class="btn medium dark">Continue</button></a></div>';
	document.getElementById('FaceboxPopUp').innerHTML=content;
	setTimeout("jQuery.facebox(document.getElementById(\'FaceboxPopUp\').innerHTML)",1);
}

function showPleaseWait(id)
{
	document.getElementById(id).innerHTML='<img style="margin:15px 40px 0 0; height:20px; width:20px" src="imgs/pleasewait.gif"/>';
}

function showPleaseWaitProfile(id)
{
	document.getElementById(id).innerHTML='<div style="text-align:center"><img style="margin-top:90px;height:30px; width:30px" src="imgs/pleasewait.gif"/></div>';
}

function forceSignup()
{
	content = '<h2 style="margin-top:10px">Signup for Free</h2><div style="padding:20px 30px;"><div class="green" style="font-size:12px">Sorry, you must be a member to view profiles. Please sign up with your Facebook account to continue. Please note myZamana does not post any activity to your Facebook profile.</div><button onclick="fb_login()" class="btn large green">Signup for Free</button></div>';
	
	document.getElementById('FaceboxPopUp').innerHTML=content;
	setTimeout("jQuery.facebox(document.getElementById(\'FaceboxPopUp\').innerHTML)",1000);
    $("div.close").hide();
}


function keyDown(e)
{ 
	var evt=(e)?e:(window.event)?window.event:null;
	if(evt)
	{ 
		var key=(evt.charCode)?evt.charCode: ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0)); 
		if(key=="39") 
			window.location=ge('next').value; 
	} 
} 

function keyApprove(e)
{ 
	var evt=(e)?e:(window.event)?window.event:null;
	if(evt)
	{ 
		var key=(evt.charCode)?evt.charCode: ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0)); 
		if(key>47 && key<58) 
		{
			if (key==48)
				score=10;
			else
				score= key-48;
			xajax_approve($('.user').attr('id').substr(5),score);
		}
		if (key==27)
			xajax_removePicture($('.user').attr('id').substr(5));	
		if (key==9)
			xajax_removeUsername($('.user').attr('id').substr(5));	
	} 
} 

function questionApprove(e)
{ 

	var evt=(e)?e:(window.event)?window.event:null;
	if(evt)
	{ 
		var key=(evt.charCode)?evt.charCode: ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0)); 
		if(key>47 && key<58) 
		{
			score= key-48;
			xajax_rateQuestion($('.question').attr('id').substr(9),score);
		}
	} 
} 

function go(url)
{
	window.location=url;
}

function areAllSelected(obj) 
{
		count=-1;
		$(":checkbox").each(function(){count++;});
		count2=0;
		$(":checkbox:checked").each(function(){count2++;});

		if(count==count2)
		{
			$("span#select_warning").html("<span style=\'color:red\'>Sorry, you can\'t select all possible answers. Please choose \'Irrelevant\' instead.</span>");
			id = "#"+$(obj).attr("for");
			$(id).removeAttr("checked");
			highlight();
		}
		else
			$("span#select_warning").html("You can select more than one option");

}

function highlight() 
{
	$("label").removeClass("selected");  
	$(":radio:checked").each(function(){$(this).parent().addClass("selected")});
	$(":checkbox:checked").each(function(){$(this).parent().addClass("selected")});
}

function toggleExplanation() 
{

	if($("#is_public").attr("checked"))
	{
		$("div#explain").show();
		$("textarea#explain_answer").select();
	}
	else
	{
		$("div#explain").hide();		
	}
}

