/*  merged_js.js
L 12 <script type="text/javascript" src="http://greatnonprofits.org/.src/default.js"></script>

L 106 <script type="text/javascript" src="http://greatnonprofits.org/.src/submit_form.js"></script>

L 280 <script type="text/javascript" src="http://greatnonprofits.org/.src/validate.js"></script>

*/

function blurLinks() {
	if((navigator.platform == "Win32") && document.getElementsByTagName) {
		var lnks = document.getElementsByTagName('a');
	    for (var i = 0; i < lnks.length; i++) {
			var lnk = lnks[i];
			lnk.onfocus = new Function("if(this.blur)this.blur()");
    	}
	}
}

function targetLinks() {
	var lnks = document.getElementsByTagName('a');
    for (var i = 0; i < lnks.length; i++) {
		var lnk = lnks[i];
		if (lnk.getAttribute("href") && lnk.getAttribute("rel") == "external") lnk.target = "_blank"; 
   	}
}

function init() {
	blurLinks();
	targetLinks();
}

// Sorting List Form script

function sortForm(form, field, sorting) {
	var theForm = $('sort-form');
	var order 	= $('orderby');
	var sort	= $('sort');
	order.value = field;
	if(arguments[2]) sort.value = sorting;
	theForm.submit();
}

// Extended toggling script for use with showing and hiding content.
// Can be used with A tags or input/submit form buttons

function toggleBlock(obj, affected, hideStr, showStr) {
	var value	= obj.innerHTML ? obj.innerHTML : obj.value ? obj.value : null;
	if(hideStr && hideStr == value) {
		Element.hide($(affected));
		obj.innerHTML ? obj.innerHTML = showStr : obj.value = showStr;
	} else if(showStr && showStr == value) {
		Element.show($(affected));
		obj.innerHTML ? obj.innerHTML = hideStr : obj.value = hideStr;
	}
}

function setpass(e) {
  document.getElementById(e).value = "931d18ea3f2f0a57214ef3c26";
}

var fb_uid;
        
function redir(e)
  {

    var e = e.replace(/http:\/\/greatnonprofits.org/, "");
    e = "/fb-c/ismember" + e;
    top.location.href = e;
  }
  
function do_login(e)
  {
  
    FB.Connect.requireSession();

    FB.Facebook.get_sessionState().waitUntilReady(function()
    {
        redir(e);
  
        });
  }

function fb_login()
  {

    $("fb_logout").observe('click', function()
    { 
      FB.Connect.logout(function()
      {top.location="/?ACT=192";}); 
    });
    
  } 


function fb_showUser(id)
 {FB.XFBML.Host.parseDomTree();}

 
//	-------------------------------------
//	Global Vars
//	-------------------------------------

var timeout	= 5000;	// Use this to set the timeout interval on AJAX requests.

//	-------------------------------------
//	Submit Form
//	-------------------------------------

function submit_form( to, from, loading )
{
	var target		= $(to);
	var spinner		= $(loading);
	var theform		= $(from);
	var act			= theform.elements['ACT'].value;
	var url			= theform.action + '?ACT=' + act;
	var pars		= Form.serialize(theform);
	
	if ( ! theform.elements['template'] )
	{
		target.innerHTML	= 'Unable to find a template to use for output formatting.';
		return false;
	}
	else
	{
		url	= theform.elements['template'].value;
	}
	
	//	-------------------------------------
	//	Prepare timeout handler
	//	-------------------------------------
	
	function callInProgress (xmlhttp)
	{
		switch (xmlhttp.readyState)
		{	
			case 1: case 2: case 3:	
				return true;	
			break;
	
			// Case 4 and 0	
			default:	
				return false;	
			break;	
		}	
	}
	
	//	-------------------------------------
	//	Register global responders that will
	//	occur on all AJAX requests.
	//	-------------------------------------

	Ajax.Responders.register({
		onCreate: function(request)
		{
			request['timeoutId'] = window.setTimeout(
	
				function()
				{
					//	-------------------------------------
					//	If we have hit the timeout and the
					//	AJAX request is active, abort it
					//	and let the user know.
					//	-------------------------------------
	
					if (callInProgress(request.transport))
					{	
						request.transport.abort();
						
						//	-------------------------------------
						//	Run the onFailure method if we set
						//	one up when creating the AJAX object.
						//	-------------------------------------	
	
						if (request.options['onFailure'])
						{	
							request.options['onFailure'](request.transport, request.json);	
						}	
					}	
				},	
				timeout
			);	
		},
	
		onComplete: function(request)
		{
			//	-------------------------------------
			//	Clear the timeout, the request
			//	completed ok.
			//	-------------------------------------
			
			window.clearTimeout(request['timeoutId']);
		}	
	});
	
	//	-------------------------------------
	//	Show loading.
	//	-------------------------------------
	
	Element.hide( spinner );
	Element.toggle( spinner );
	
	//	-------------------------------------
	//	Remove contents from target.
	//	-------------------------------------
	
	target.innerHTML	= '';
	Element.hide( target );
	
	//	-------------------------------------
	//	Execute AJAX
	//	-------------------------------------
	
	var	aj	= new Ajax.Request( url, { method: 'post', postBody: pars, onSuccess: put, onFailure: fail, onException: exception } );

	function put(t)
	{
		//	Hide loader
		Element.hide(spinner);
		
		//	Place list
		var output				= t.responseText;
		Element.show( target );
		target.innerHTML		= output;
		
		Element.hide( theform );
	}
	
	function fail()
	{
		//	Show slow server message
		Element.hide(spinner);
		Element.show( target );
		target.innerHTML	= 'The server is currently under heavy load. Please try later.';
	}
	
	function exception(t)
	{
		//	Show exception message
		Element.hide( spinner );
		Element.show( target );
		target.innerHTML	= 'A server exception occurred. Please try later.';
	}
}

//	End submit_form


//	-------------------------------------
//	Print_r utility function for testing
//	-------------------------------------

function print_r(theObj)
{
	var out	= '';
	
	if (theObj.constructor == Array || theObj.constructor == Object)
	{		
		for (var p in theObj)
		{
			if (theObj[p].constructor == Array || theObj[p].constructor == Object)
			{
				out	= out + "- ["+p+"] => "+typeof(theObj) + "\n";
				out	= out + "\n";
				print_r(theObj[p]);
				out	= out + "\n";
			}
			else
			{
				out	= out + "- ["+p+"] => "+theObj[p] + "\n";
			}
		}
	}
	
	return out;
}

//	End print_r

//	----------------------------
//	Validate review
//	----------------------------

function validate_review( page, state )
{
	var rating		= $('rating');
	var review		= $('review');
	var tasks		= $('tasks');
	var work_date	= $('work_date');
	
	var errors	= "";

	//	----------------------------
	//	Branch for page 1
	//	----------------------------
	
	if ( page == 1 )
	{		
		//	----------------------------
		//	Rating
		//	----------------------------
		
		if ( rating.value == '' || rating.value == '0' )
		{
			errors = errors + "Please select a rating value for this organization.\n\n";
		}
		
		//	----------------------------
		//	Review Count
		//	----------------------------
		
		if ( _countwords( review.value, 'y' ) < 20 )
		{
			errors = errors + "Please make sure your review has at least 20 words.\n\n";
		}
		
		//	----------------------------
		//	Tasks
		//	----------------------------
		
		if ( tasks.value == '' )
		{
			errors = errors + "Please complete the 'What Did You Do' field.\n\n";
		}
		
		//	----------------------------
		//	Date
		//	----------------------------
		//
		//if ( work_date.value == '' || work_date.value == 'MM/DD/YY' )
		//{
		//	errors = errors + "Please indicate the date of your most recent experience with this nonprofit.\n\n";
		//}
	
		//	----------------------------
		//	Any errors?
		//	----------------------------
		
		if ( errors != "" )
		{
			alert( errors );
			return false;
		}
		
		//	----------------------------
		//	No errors, switch page
		//	----------------------------
		
		if ( state == 'write' )
		{
			$('main').removeClassName('write1');
			$('main').addClassName('write2');
		}
		if ( state == 'edit' )
		{
			$('main').removeClassName('edit1');
			$('main').addClassName('edit2');
		}
	}
		
	//	----------------------------
	//	Branch for page 2
	//	----------------------------
	
	if ( page == 2 )
	{
		//	----------------------------
		//	Positive / Negative
		//	----------------------------
		
		var pos	= false;
		var neg	= false;
		
		for ( i=1;i<5;i++ )
		{
			if ( $('pos_' + i).value != '' )
			{
				pos = true;
			}
			
			if ( $('neg_' + i).value != '' )
			{
				neg = true;
			}
		}
		
		if ( pos == false )
		{
			errors = errors + "Please indicate what is great about this nonprofit using one of the fields below.\n\n";
		}
		
		if ( neg == false )
		{
			errors = errors + "Please indicate what could be made better about this nonprofit using one of the fields below.\n\n";
		}
	
		//	----------------------------
		//	Any errors?
		//	----------------------------
		
		if ( errors != "" )
		{
			alert( errors );
			return false;
		}
	}
	
	return true;
}

//	End validate reviews

//	----------------------------
//	Validate org submission
//	----------------------------

function validate_org()
{
	alert('kj');
}


//	----------------------------
//	Count words
//	----------------------------

function _countwords(str, striphtml)
{
	var count	= 0;
	var txt		= str;
	if (striphtml == "y") txt = txt.replace(/(<([^>]+)>)/ig,""); //strip HTML
	txt			= txt.replace(/\s*((\S+\s*)*)/, "$1"); //ltrim
	txt			= txt.replace(/((\s*\S+)*)\s*/, "$1"); //rtrim
	count		= txt.match(/^ *$/) ? 0 : txt.split(/\s+/g).length;
	return count;
}

//	End count words

//	----------------------------
//	Validate field
//	----------------------------

function validate_field( field,label )
{
	var field	= $(field);
	
	if ( field.value == '' )
	{
		alert(label + " must be provided.");
		return false;
	}
	
	return true;
}

//	End validate field

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}


function validate_registration()
{
	var username		= $('username');
	var screen_name		= $('screen_name');
	var password		= $('password');
	var password_confirm	= $('password_confirm');
	var accept_terms		= $('accept_terms');
	var comments	= $('comments');
	
	var errors	= "";
	
	if ( username.value == '' )
	{
  	errors = errors + "Your Email must be provided.\n\n";
	}
	if ( screen_name.value == '' )
	{
  	errors = errors + "Your Screen Name must be provided.\n\n";
	}
	
	if ( accept_terms.value =! 'yes' )
	{
  	errors = errors + "You should agree to the terms and conditions.\n\n";
	}
	if ( comments.value == '' )
	{
  	errors = errors + "Please enter a response.\n\n";
	}
	if ( password_name.value == '' )
	{
  	errors = errors + "A password must be provided.\n\n";
    if ( password_name.value == '' ) 
    {
      errors = errors + "Passwords dont't match.\n\n";
    }
	}
  if ( errors != "" )
  {
    alert( errors );
    return false;
  }
	return true;
}

