function grayOut(vis, zindex, options) {  
// Pass true to gray out screen, false to ungray  
// options are optional.  This is a JSON object with the following (optional) properties  
// opacity:0-100         
// Lower number = less grayout higher = more of a blackout  
// zindex: #             
// HTML elements with a higher zindex appear on top of the gray out  
// bgcolor: (#xxxxxx)    // Standard RGB Hex color code  // 

if (!zindex)
	zindex = 99;

var options = options || {};  
//var zindex = options.zindex || 99;
var opacity = options.opacity || 50;
var opaque = (opacity / 100);  
var bgcolor = options.bgcolor || '#000000';  
var dark=document.getElementById('darkenScreenObject');  
if (!dark) {
	// The dark layer doesn't exist, it's never been created.  So we'll    // create it here and apply some basic styles.    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
	var tbody = document.getElementsByTagName("body")[0];
	var tnode = document.createElement('div');// Create the layer.
	        
	tnode.style.position='absolute';                 // Position absolutely   
	tnode.style.top='0px';                           // In the top    
	tnode.style.left='0px';                          // Left corner of the page   
	tnode.style.overflow='hidden';                   // Try to avoid making scroll bars                
	tnode.style.display='none';                      // Start out Hidden    
	tnode.id='darkenScreenObject';                   // Name it so we can find it later   
	tbody.appendChild(tnode);                            // Add it to the web page   
	dark=document.getElementById('darkenScreenObject');  // Get the object. 
	}
	
	
	 if (vis) {    
	 // Calculate the page width and height   
	 if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) 
	 {       
	 	var pageWidth = document.body.scrollWidth+'px';  
		var pageHeight = document.body.scrollHeight+'px';  
	} 
	else if( document.body.offsetWidth )
	{     
		var pageWidth = document.body.offsetWidth+'px';   
		var pageHeight = document.body.offsetHeight+'px';  
		} 
	else {    
		var pageWidth='100%'; 
		var pageHeight='100%';  
		}      
		//set the shader to cover the entire page and make it visible.   
		dark.style.opacity=opaque;                    
		dark.style.MozOpacity=opaque;                 
		dark.style.filter='alpha(opacity='+opacity+')'; 
		dark.style.zIndex=zindex;         
		dark.style.backgroundColor=bgcolor; 
		dark.style.width= pageWidth;   
		dark.style.height= pageHeight;  
		dark.style.display='block';      
		}
		else {    
		dark.style.display='none';  
		}
		}
		
function isGray()
{
	var dark=document.getElementById('darkenScreenObject');  
	if (!dark) {
		return false;
	}
	else if (dark.style.display=='none')
			return false;
	else
		return true;
}		
		
function showModal(divId)
{
		
	var element      = document.getElementById(divId);
	if (element){
		grayOut(true);
		element.style.display = "block";
		
		var width        = 800;
		var height       = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

		element.style.position = "absolute"; 
		
		element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )/2)+'px';
		element.style.top  = (document.documentElement.scrollTop +(height-element.clientHeight)/2)+'px';

	}

}
	
	function hideModal(divId)
	{
		grayOut(false);
		document.getElementById(divId).style.display = "none";
	}
	
	 function getRadioValue(radioObject) {
          var value = null
          for (var i=0; i<radioObject.length; i++) {
               if (radioObject[i].checked) {
                    value = radioObject[i].value;
                    break ;
               }
          }
          return value
     }
	 
	 function updateHtml(idString, html)
	{
		obj = document.getElementById(idString);
		if (obj)
		{
			obj.innerHTML = html;	
		}
	}
	function getInnerHtml(idString)
	{
		obj = document.getElementById(idString);
		if (obj)
		{
			return obj.innerHTML;	
		}
		else
			return null;
	}
	
	function objectExists(idString)
	{
		obj = document.getElementById(idString);
		if (obj)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	function hideObject(objId)
	{
		document.getElementById(objId).style.display = "none";
	}
	function showObject(objId, asBlock)
	{
		var displayType;
		if (!asBlock)
			displayType = "inline";
		else
			displayType = "block";
		document.getElementById(objId).style.display = displayType;
	}
	function getTextOfSelectedOption(objId)
	{		
		obj = document.getElementById(objId);	
		var w = obj.selectedIndex;		
		var text = obj.options[w].text;		
		return text;
	}
	
	function getCookie(c_name)
	{
		if (document.cookie.length>0)
		  {
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1)
			{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
			} 
		  }
		return "";
	}
	function setCookie(c_name,value,expiredays)
	{
		//var exdate=new Date();	
		//exdate.setDate(exdate.getDate()+expiredays);
		var today      = new Date(); 
		var exdate = new Date(today.getTime() + (expiredays * 86400000));
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	}

	
	
	function limitChars(obj, limit, errorObj)
	{
		var numChars = obj.value.length;
		if(numChars > limit) {
			if (errorObj)
				errorObj.innerHTML='<span style="color:red;">' + limit + '/' + limit + ' characters!</span>';
			obj.value = obj.value.substring(0, limit);
		}
		else if (numChars == limit)
		{
			if (errorObj)
				errorObj.innerHTML='<span style="color:red;">' + numChars + '/' + limit + ' characters!</span>';
		}
		else
		{
			if (errorObj)
				errorObj.innerHTML=numChars + '/' + limit + ' characters';
		}
	}
	
	
function hideObject(id)
{
	document.getElementById(id).style.display='none';
}
 function $$(idName)
 {
 
 	return document.getElementById(idName).value;
 }
 function $(idName)
 {
	return document.getElementById(idName);
 }
function showEditContactInfoLink()
{
	document.getElementById('contactEdit').style.display = 'inline';
}
function hideEditContactInfoLink()
{
	document.getElementById('contactEdit').style.display = 'none';
}

function positionPickCrowdWindow(id, position_id)
{
	
	var element      = document.getElementById(id);
	var position_element = document.getElementById(position_id);
  
	element.style.position = "absolute";
	element.style.display  = "block";  
   
	element.style.zIndex = 200;

	x = 0;
	y = 0;

    for (var p = position_element; p; p = p.offsetParent)
	{
		if (p.style.position != 'absolute')
		{
			x += p.offsetLeft;
			y += p.offsetTop;
		}
	}

	x += position_element.clientWidth;
	y += position_element.clientHeight;
	
	//x = position_element.offsetLeft + position_element.offsetParent.offsetParent.style.left;
    //y = position_element.offsetTop + position_element.offsetParent.offsetParent.style.top;

	x = position_element.offsetLeft + position_element.offsetParent.offsetLeft;
    y = position_element.offsetTop + position_element.offsetParent.offsetTop;

	x = position_element.offsetParent.offsetLeft + 437;
	y = position_element.offsetParent.offsetTop + 67;
	

	
    element.style.left = x+'px';
    element.style.top  = y+'px';
}
	

function askForFacebookEmailPermission(showFeed)
{
	try
	{
		if (showFeed)
			FB.Connect.showPermissionDialog("email", recordIfAccepted);
		else
			FB.Connect.showPermissionDialog("email");

	}
	catch(err)
	{
		setTimeout ( "askForFacebookEmailPermission(" + showFeed + ")", 1000 );
	}

}
function recordIfAccepted(bool)
{	
	facebook_publish_feed_story('104337851393', {"images":[{"src":"http://iusem.com/images/iusem_textBubble.png", "href":"http://iusem.com"}]}); 
}



//TEST FACEBOOK STUFF

	function facebook_onload(already_logged_into_facebook) {
  // user state is either: has a session, or does not.
  // if the state has changed, detect that and reload.

  FB.ensureInit(function() {
      FB.Facebook.get_sessionState().waitUntilReady(function(session) {
          var is_now_logged_into_facebook = session ? true : false;

          // if the new state is the same as the old (i.e., nothing changed)
          // then do nothing
          if (is_now_logged_into_facebook == already_logged_into_facebook) {
            return;
          }

          // otherwise, refresh to pick up the state change
          refresh_page();
        });
    });
}

/*
 * Our <fb:login-button> specifies this function in its onlogin attribute,
 * which is triggered after the user authenticates the app in the Connect
 * dialog and the Facebook session has been set in the cookies.
 */
function facebook_onlogin_ready() {
  // In this app, we redirect the user back to index.php. The server will read
  // the cookie and see that the user is logged in, and will deliver a new page
  // with content appropriate for a logged-in user.
  //
  // However, a more complex app could use this function to do AJAX calls
  // and/or in-place replacement of page contents to avoid a full page refresh.
  alert("logged in");
  refresh_page();
}

/*
 * Do a page refresh after login state changes.
 * This is the easiest but not the only way to pick up changes.
 * If you have a small amount of Facebook-specific content on a large page,
 * then you could change it in Javascript without refresh.
 */
function refresh_page() {
  window.location = 'index.php';
}

/*
 * Prompts the user to grant a permission to the application.
 */
function facebook_prompt_permission(permission) {
  FB.ensureInit(function() {
    FB.Connect.showPermissionDialog(permission);
  });
}

/*
 * Show the feed form. This would be typically called in response to the
 * onclick handler of a "Publish" button, or in the onload event after
 * the user submits a form with info that should be published.
 *
 */
function facebook_publish_feed_story(form_bundle_id, template_data) {
  // Load the feed form
  FB.ensureInit(function() {
         // FB.Connect.showFeedDialog(form_bundle_id, template_data);
          FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);

      // hide the "Loading feed story ..." div
	  obj = document.getElementById('feed_loading');
	  if (obj)
		obj.style.visibility = "hidden";
  });
}

/*
 * If a user is not connected, then the checkbox that says "Publish To Facebook"
 * is hidden in the "add run" form.
 *
 * This function detects whether the user is logged into facebook but just
 * not connected, and shows the checkbox if that's true.
 */
function facebook_show_feed_checkbox() {
  FB.ensureInit(function() {
      FB.Connect.get_status().waitUntilReady(function(status) {
          if (status != FB.ConnectState.userNotLoggedIn) {
            // If the user is currently logged into Facebook, but has not
            // authorized the app, then go ahead and show them the feed dialog + upsell
            checkbox = ge('publish_fb_checkbox');
            if (checkbox) {
              checkbox.style.visibility = "visible";
            }
          }
        });
    });
}


	function toggle(id)
	{
		if (document.getElementById(id).style.display == 'none')
			document.getElementById(id).style.display = 'inline';
		else
			document.getElementById(id).style.display = 'none';
	}
	
	

function clearTextIfStillDefault(id, defaultText)
{
	elem = document.getElementById(id);
	if (elem)
	{
		if (elem.value == defaultText)
		{			
			elem.value='';
			elem.style.color = 'black';
			if (id == "comment" || id == "question" || id == "emailsToAdd" || id =='message' || id =='questionBiz')
			{
				elem.style.fontSize = '';
				elem.style.fontWeight = '';
				//if (id == "question")
				//	elem.style.height = "50px";
			}
		}
	}
}

function putBackDefaultText(id, defaultText)
{
	elem = document.getElementById(id);
	if (elem)
	{
		if (elem.value == "")
		{
			if (id == "emailsToAdd" || id =="crowdName")
			{
				elem.style.color = 'Gray';
				elem.style.fontSize = '110%';
			}
			else if (id == "comment" || id =='message')
			{
				elem.style.color = '#4F81BD';
				elem.style.fontSize = '20px';
				elem.style.fontWeight = '900';
			}
			else if (id == "question" || id == 'questionBiz')
			{
				elem.style.color = '#4F81BD';
				elem.style.fontSize = '18px';
				elem.style.fontWeight = '900';
			}
			elem.value=defaultText;
			
			if (id == "question")
			{				
				//elem.style.height = "40px";
				showChooseCrowd(false);
			}
		}
	}
}




function reloadPage()
{
	window.location = '/';
}

var RecaptchaOptions = {
   theme : 'white'
  // tabindex : 2
};

var pageItsForLast = 'mainFeedCloud';
var extraInput1Last = '';

		
	
		
	function isBlank(text)
	{
		var temp = text.split(' ').join('').split('\n').join('').split('\r').join('');
	
		if (text == "" || temp == "")
			return true;
		else
			return false;
	}

	
	
	
	function stopEvent(e) {
if (!e) e = window.event;
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
}

function redirect(link)
{
	window.location = link;
}
 
function showImage(obj, image)
{
	obj.src = image;
}

function isValidZip(zip)
{
	var valid = "0123456789";
	
	if (zip.length!=5) {
		//error += "The zip code must be 5 digits.\n";
		return false;
	}
	var isAllDigits = true;
	var temp = "";
	for (var i=0; i < zip.length; i++) {
		temp = "" + zip.substring(i, i+1);	
		if (valid.indexOf(temp) == "-1")
		{
			isAllDigits = false;
		}
	}
	if (!isAllDigits)
	{
		//error += "The zip code can only contain numbers.\n";
		return false;
	}
	return true;
}

function gebi(name)
{
	return document.getElementById(name);
}
function partial(func /*, 0..n args */) {
   var args = Array.prototype.slice.call(arguments, 1);
 
  return function() {
    var allArguments = args.concat(Array.prototype.slice.call(arguments));
    return func.apply(this, allArguments);
  };
}


