
//do on opening page
$("document").ready(function() {

	//make search box inside label
	makeInBoxInputLabel($('#glbSearchBox'));
});

/* 
Social Bookmarks
*/

	function insertSocialBookmarkLinks(pageUrl,title,whichOnes) {
		if( !pageUrl )
			pageUrl = location.href;
		if( !title )
			title = document.title;
		if(!whichOnes)
			whichOnes = ["delicious","digg","linkedin"];
		
		var info;
		var output = "";
		
		for( var i=0; i < whichOnes.length; i++ ) {
			info = null;
			switch( whichOnes[i] ) {
				case "delicious":
					info = getDeliciousInfo(pageUrl,title);
					break;
				case "digg":
					info = getDiggInfo(pageUrl,title);
					break;
				case "technorati":
					info = getTechnoratiInfo(pageUrl,title);
					break;
				case "reddit":
					info = getRedditInfo(pageUrl,title);
					break;
				case "stumbleupon":
					info = getStumbleUponInfo(pageUrl,title);
					break;
				case "slashdot":
					info = getSlashdotInfo(pageUrl,title);
					break;
				case "facebook":
					info = getFacebookInfo(pageUrl,title);
					break;
				case "newstrust":
					info = getNewsTrustInfo(pageUrl,title);
					break;
				case "linkedin":
					info = getLinkedInInfo(pageUrl,title);
					break;
			}
			if( info ) {
				output += getListItem(info);
			}
		}
		document.write("<ul class=\"ulHm\">" + output + "</ul>");
	}
	function getListItem(info) {
		var output = "<li><a href=\"" + info.href + "\" onclick=\"" + info.onclick + "\" class= \"" + info.handle + "\" title=\""+info.alt+"\"> &gt; " + info.text + "</a></li>";
		return output;
	}
	function getDeliciousInfo(pageUrl,title) {
		var info = new Object();
		info.handle = "delicious";
		info.text = "del.icio.us";
		info.href = "http://del.icio.us/post?v=4&noui&jump=close&url="+encodeURIComponent(pageUrl)+"&title="+encodeURIComponent(title);
		info.onclick = "window.open(this.href,'"+info.handle+"','toolbar=no,width=700,height=400');return false;";
		info.alt = "del.icio.us";
		return info;
	}
	function getDiggInfo(pageUrl,title) {
		var info = new Object();
		info.handle = "digg";
		info.text = "Digg";
		info.href = "http://digg.com/submit?phase=2&url="+encodeURIComponent(pageUrl)+"&title="+encodeURIComponent(title)+"&topic=television";
		info.onclick = "window.open(this.href,'"+info.handle+"','toolbar=no,width=700,height=400,resizable=1');return false;";
		info.alt = "Digg!";
		return info;
	}
	function getLinkedInInfo(pageUrl,title) {
		var info = new Object();
		info.handle = "linkedin";
		info.text = "LinkedIn";
		info.href = "http://www.linkedin.com/groups?about=&gid=1765657&trk=anet_ug_grppro&url="+encodeURIComponent(pageUrl)+"&title="+encodeURIComponent(title)+"";
		info.onclick ="window.open(this.href,'"+info.handle+"','toolbar=no,width=700,height=400,resizable=1');return false;";
		info.alt = "LinkedIn";
		return info;
	}


/* End Social Bookmarks */

	function launch_mylibrary(id)
	{
		window.open("http://www.hcbs.org/myLibraryAddRemove.php?id="+id, 'mylibary', 'width=430,height=610,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1');
		return false;
	}
	
	function privacy_policy()
	{
		window.open("http://www.hcbs.org/myLibraryAddRemove.php?act=priv", 'mylibarypolicy', 'width=430,height=610,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1');
		return false;
	}	

/*
 */
function myLibrary(id)
{
	if(!document.getElementById) return true;
	
	//test for ajax support
	if (window.XMLHttpRequest) 
	{
		//ok
	} 
	else if (window.ActiveXObject) {
		//ok
	} 
	else 
	{
		//use regular scripts
		return launch_mylibrary(id);
	}
   
	/** see if cookie exists **/
	/** if not force login   **/
	


	// between 0 - 1
	var rndNum = Math.random()
	
	// rndNum from 0 - 1000
	rndNum = parseInt(rndNum * 1000);
	
	// rndNum from 33 - 127
	rndNum = (rndNum % 94) + 33;
	textChar = String.fromCharCode(rndNum);

	
	a_id = document.getElementById("mylib"+id);
	
//	$.get("/hcbs/include/pub/myArchive-ajax.php?wh=br&id="+id+"&"+textChar,function(xml)
	$.get("/include/pub/myArchive-ajax.php?wh=br&id="+id+"&"+textChar,function(xml)
	{
		mylibary_action(xml,id);
	  });
	return false;
		
}

/*
 */
function myLibraryMI(id)
{
	if(!document.getElementById) return true;
	
	//test for ajax support
	if (window.XMLHttpRequest) 
	{
		//ok
	} 
	else if (window.ActiveXObject) {
		//ok
	} 
	else 
	{
		//use regular scripts
		return true;
	}
   
	/** see if cookie exists **/
	/** if not force login   **/
	


	// between 0 - 1
	var rndNum = Math.random()
	
	// rndNum from 0 - 1000
	rndNum = parseInt(rndNum * 1000);
	
	// rndNum from 33 - 127
	rndNum = (rndNum % 94) + 33;
	textChar = String.fromCharCode(rndNum);

	a_id = document.getElementById("mylib"+id);
	// with /hcbs for iaclients, without for live site
	//$.get("/hcbs/include/pub/myArchive-ajax.php?wh=mi&id="+id+"&"+textChar,function(xml)
	$.get("/include/pub/myArchive-ajax.php?wh=mi&id="+id+"&"+textChar,function(xml)
	{
		mylibary_action(xml,id);
	  });
	return false;
		
}

/*
 */
function myLibraryText(id)
{
	if(!document.getElementById) return true;
	
	//test for ajax support
	if (window.XMLHttpRequest) 
	{
		//ok
	} 
	else if (window.ActiveXObject) {
		//ok
	} 
	else 
	{
		//use regular scripts
		return true;
	}
   
	/** see if cookie exists **/
	/** if not force login   **/
	


	// between 0 - 1
	var rndNum = Math.random()
	
	// rndNum from 0 - 1000
	rndNum = parseInt(rndNum * 1000);
	
	// rndNum from 33 - 127
	rndNum = (rndNum % 94) + 33;
	textChar = String.fromCharCode(rndNum);

	a_id = document.getElementById("mylib"+id);
	//$.get("/hcbs/include/pub/myArchive-ajax.php?wh=tx&id="+id+"&"+textChar,function(xml)
	$.get("/include/pub/myArchive-ajax.php?wh=tx&id="+id+"&"+textChar,function(xml)
	{
		mylibary_action(xml,id);
	  });
	return false;
		
}
	
function mylibary_action(xml_response,id)
{
	response = $("result",xml_response).text();

	if(response == 'false')
	{
		//not logged in
		launch_mylibrary(id);
		//return true;
		//window.location="http://www.hcbs.org/myLibrary.php?id="+id;
		//return true;
		return false;
	}
	else
	{
		a_id = document.getElementById("mylib"+id);
		//$(a_id).click(function() {myLibrary('this',id); });
		a_id.innerHTML = response;
		$(a_id).fadeIn("slow");	
		initRollovers();
		//a_id.innerHTML = response;
		return false;
	}
	
	return false;
	
}


	/** note this puts roll over on image, not href **/
	function initRollovers()
	{
		//if we don't support getElementById then exit
		if (!document.getElementById) return
	
		//yes it even does preload
		var aPreLoad = new Array();
		var aImages  = new Array();
		var sTempSrc;
	
		//grab all nav images
		//if(document.getElementById("dl-c-container")) {
		//var aImages = document.getElementById("dl-c-container").getElementsByTagName("img");
		//}
	
		//nb if you want all images on page  you do this instead (i only wanted nav)
		var aImages = document.getElementsByTagName("img");
	
	
		//loop over images
		for (var i = 0; i < aImages.length; i++)
		{
			//we use class name to find image
			if (aImages[i].className.match('img-roll'))
			{
				//grab src
				var src = aImages[i].getAttribute('src');
	
				//grab extension (neato works for gif and jpg nd png if you so feel)
				var ftype = src.substring(src.lastIndexOf('.'), src.length);
	
				//create a new attribute for rollover (for fyi = '-over')
				var hsrc = src.replace(ftype, '_hover'+ftype);
				aImages[i].setAttribute('hsrc', hsrc);
	
				//PRELOAD !!
				aPreLoad[i] = new Image();
				aPreLoad[i].src = hsrc;
	
				//on mouseover grab hsrc
				aImages[i].onmouseover = function()
				{
					sTempSrc = this.getAttribute('src');
					this.setAttribute('src', this.getAttribute('hsrc'));
				}
	
				//on mouse out strip out "on"
				aImages[i].onmouseout = function()
				{
					//alert(sTempSrc);
					if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_on'+ftype, ftype);
					this.setAttribute('src', sTempSrc);
				}
			}
		}
	}


	
	function initPage()
	{ 
		initRollovers();
	}



   function init() {
       // quit if this function has already been called
       if (arguments.callee.done) return;

       // flag this function so we don't do the same thing twice
       arguments.callee.done = true;

       // create the "page loaded" message
       initPage();
   };

   /* for Mozilla */
   if (document.addEventListener) {
       document.addEventListener("DOMContentLoaded", init, false);
   }

   /* for other browsers */
   window.onload = init;
   
   
// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
    if (this.readyState == "complete") {
        init(); // call the onload handler
    }
};
/*@end @*/


/**** ****/
		function getElementsByClass(searchClass,node,tag) {
			var classElements = [];
			if ( node === null ) { node = document; }
			if ( tag === null ) { tag = '*'; }
			var els = node.getElementsByTagName(tag);
			var elsLen = els.length;
			var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
			for (var i = 0, j = 0; i < elsLen; i++) {
				if ( pattern.test(els[i].className) ) {
					classElements[j] = els[i];
					j++;
				}
			}
			return classElements;
		}
		
		function trim(str)
		{
		  return str.replace(/^\s+|\s+$/g, '');
		}
		
		
		function check_count(radios){
			var count = 0;
			for (i=0;i<radios.length;i++)
			{
				if (radios[i].checked)
				{
					count++;
				}
			}
			return count;
		}
		
		
		function check_radio(radios){
		
			for (i=0;i<radios.length;i++)
			{
				if (radios[i].checked)
				{
					return true;
				}
			}
			return false;
		}
		
		function do_radio(radio, question)
		{
			var why = '';
			var ff = null;
			if(!check_radio(radio))
			{		why += 'Question '+question+': Cannot be left blank.'+"\n";
				ff=  document.getElementById('q'+question);
				ff.className='err';
			}
			return why;	
		}
		
		
		
		function dovalidate_survey(formy)
		{
			var ff = null;
			
			//submit
			if (!document.getElementById) { return true; }
		
			var errs = getElementsByClass("err",document,null);
			for (var i = 0; i < errs.length; i++)
			{
				errs[i].className = '';
			}
		
			var why = '';
		
			//check question 1
			why += do_radio(formy['q1_often'],'1');
			why += do_radio(formy['q2_use'],'2');
			why += do_radio(formy['q3_first'],'3');
		
			//check question 3 OTHER
			if(formy['q3_first'][7].checked && trim(formy['q3_first_other'].value)==='')
			{
				why += 'Question 3: Selected other but did not specify.'+"\n";
				ff = document.getElementById('q3other');
				ff.className='err';
			}	
			
			why += do_radio(formy['q4_prefer'],'4');	
		
			//5+6 can be left blank
		
				
			//check question 7
			why += do_radio(formy['q7_a_navigate'],'7a');
			why += do_radio(formy['q7_b_download'],'7b');
			why += do_radio(formy['q7_c_uptodate'],'7c');
			why += do_radio(formy['q7_d_find'],'7d');
			why += do_radio(formy['q7_e_search'],'7e');
			why += do_radio(formy['q7_f_info'],'7f');
				
			//check question 8
				if(check_count(formy['q8_what[]']) != 3)
				{
					why += 'Question 8: You need to select exactly 3.'+"\n";
					ff = document.getElementById('q8');
					ff.className='err';
				}
				
			//check question 9
				if(check_count(formy['q9_look[]']) === 0)
				{
					why += 'Question 9: You need to select atleast 1 value'+"\n";
					ff = document.getElementById('q9');
					ff.className='err';
				}		
				
			//check question 3 OTHER
			if(formy['q9_look[]'][7].checked && trim(formy['q9_look_other'].value)==='')
			{
				why += 'Question 9: Selected other but did not specify.'+"\n";
				ff = document.getElementById('q9other');
				ff.className='err';
			}	
			
			//check question 10
			if(trim(formy['q10_look'].value)==='')
			{
				why += 'Question 10: Cannot be left blank.'+"\n";
				ff = document.getElementById('q10');
				ff.className='err';
			}
			if(trim(formy['q10_look'].value)==='')
			{
				why += 'Question 10: Cannot be left blank.'+"\n";
				ff = document.getElementById('q10');
				ff.className='err';
			}
			if(trim(formy['q11_useful'].value)==='')
			{
				why += 'Question 11a: Cannot be left blank.'+"\n";
				ff = document.getElementById('q11a');
				ff.className='err';
			}
			if(trim(formy['q11_friendly'].value)==='')
			{
				why += 'Question 11b: Cannot be left blank.'+"\n";
				ff = document.getElementById('q11b');
				ff.className='err';
			}
			if(trim(formy['q12_example'].value)==='')
			{
				why += 'Question 12: Cannot be left blank.'+"\n";
				ff = document.getElementById('q12');
				ff.className='err';
			}	
			if(trim(formy['q13_describe'].value)==='')
			{
				why += 'Question 13: Cannot be left blank.'+"\n";
				ff = document.getElementById('q13');
				ff.className='err';
			}		
				
			//check question 9
				if(check_count(formy['q14_describe[]']) === 0)
				{
					why += 'Question 14: You need to select atleast 1 value'+"\n";
					ff = document.getElementById('q14');
					ff.className='err';
				}			
				
		
			if(trim(why)==='') { return true; }
			alert(why);
			return false;
		}

function makeInBoxInputLabel(inputElem){
	

	
	$(inputElem).focus(function(e){
	    
		var eInputVal = $(this).val();
			 	
	    // Empty input value if it equals it's label
	    if ("SEARCH" == eInputVal) {
			
			$('#test').html('focus, remove');
	       
			document.getElementById($(this).attr("id")).value="";
			
	    }
  	});
	$(inputElem).blur(function(e){
	    
		var eInputVal = $(this).val();
	 	
	    if ($(this).val() == "") {
	      	$('#test').html('focus, add back');
			document.getElementById($(this).attr("id")).value="SEARCH";
	    }
  	});

}

