/* Manuelx10.com Header - General Site Functions */

	function getElement(id){
		if(typeof id=='string') id=document.getElementById(id);
		return id;
	}
	function get_randNum(){
	    var ranNum= Math.floor(Math.random()*10000);
	    return ranNum;
	}
	function MM_preloadImages() {
		var d=document;
		if(d.images){
			if(!d.MM_p) d.MM_p=new Array();
			var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
			for(i=0; i<a.length; i++)
			   if (a[i].indexOf("#")!=0){
				  d.MM_p[j]=new Image;
				  d.MM_p[j++].src=a[i];
			   }
		}
	}

	var win=null;
	function NewWindow(mypage,myname,w,h,scroll,pos){
		if (pos=="random"){
		  LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		  TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
		}
		if (pos=="center"){
		  LeftPosition=(screen.width)?(screen.width-w)/2:100;
		  TopPosition=(screen.height)?(screen.height-h)/2:100;
		}
		  else if (((pos!="center")&&(pos!="random"))||(pos==null)){
			 LeftPosition=0;TopPosition=20
		  }
		settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
		win=window.open(mypage,myname,settings);
	}
	
	function winLoader(thisObj){
		thisObj.onclick = function(){
			NewWindow(this.href,'Preview Window','780','460','yes','center');
			return false;
		}
		thisObj.onfocus = function(){
			thisObj.blur();
		}
	}

	function deliciousSubmit(thisHref){
		window.open('http://del.icio.us/post?v=4&noui&jump=close&url='+encodeURIComponent(thisHref)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=700,height=400'); 
		return false;
	}

	/* APPLESEARCH */
		// Code Source from: http://www.brandspankingnew.net/
		if (!applesearch)	var applesearch = {};
		applesearch.init = function (){
			// Add applesearch css for non-safari, dom-capable browsers
			if ( navigator.userAgent.toLowerCase().indexOf('safari') < 0  && document.getElementById ){
				this.clearBtn = false;
				// add style sheet if not safari
				//var dummy = document.getElementById("dummy_css");
				//if (dummy)	dummy.href = "/scripts/applesearch.css";
			}
		}
		// Called when on user input - toggles clear fld btn
		applesearch.onChange = function (fldID, btnID){
			// check whether to show delete button
			var fld = document.getElementById( fldID );
			var btn = document.getElementById( btnID );
			if (fld.value.length > 0 && !this.clearBtn)
			{
				btn.style.background = "white url('/graphics/srch_r_f2.gif') no-repeat top left";
				btn.fldID = fldID; // btn remembers it's field
				btn.onclick = this.clearBtnClick;
				this.clearBtn = true;
			} else if (fld.value.length == 0 && this.clearBtn)
			{
				btn.style.background = "white url('/graphics/srch_r.gif') no-repeat top left";
				btn.onclick = null;
				this.clearBtn = false;
			}
		}
		// Clears field
		applesearch.clearFld = function (fldID,btnID){
			var fld = document.getElementById( fldID );
			fld.value = "";
			this.onChange(fldID,btnID);
		}
		// called by btn.onclick event handler - calls clearFld for this button
		applesearch.clearBtnClick = function (){
			applesearch.clearFld(this.fldID, this.id);
		}
		
		window.onload = function () {
			applesearch.init();
		}
		
	/* END APPLESEARCH */

	/* ABOUT SECTION TOGGLE */
		/***********************************************
		* Switch Content script II- ?Dynamic Drive (www.dynamicdrive.com)
		* This notice must stay intact for legal use. Last updated April 2nd, 2005.
		* Visit http://www.dynamicdrive.com/ for full source code
		***********************************************/
		var switcheffect = null;
		var enablepersist="off" //Enable saving state of content structure using session cookies? (on/off)
		var memoryduration="7" //persistence in # of days
		var contractsymbol='/graphics/minus.gif' //Path to image to represent contract state.
		var expandsymbol='/graphics/plus.gif' //Path to image to represent expand state.
		function getElementbyClass(rootobj, classname){
			var temparray=new Array()
			var inc=0
			var rootlength=rootobj.length
			for (i=0; i<rootlength; i++){
			if (rootobj[i].className==classname)
			temparray[inc++]=rootobj[i]
			}
			return temparray
		}
		function sweeptoggle(ec){
			var inc=0
			while (ccollect[inc]){
			ccollect[inc].style.display=(ec=="contract")? "none" : ""
			inc++
			}
			revivestatus()
		}
		function expandcontent(curobj, cid){
			if (ccollect.length>0){
			document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="none")? "none" : ""
			curobj.src=(document.getElementById(cid).style.display=="none")? expandsymbol : contractsymbol
			}
		}
		function revivecontent(){
			selectedItem=getselectedItem()
			selectedComponents=selectedItem.split("|")
			for (i=0; i<selectedComponents.length-1; i++)
			document.getElementById(selectedComponents[i]).style.display="none"
		}
		function revivestatus(){
			var inc=0
			while (statecollect[inc]){
			if (ccollect[inc].style.display=="none")
			statecollect[inc].src=expandsymbol
			else
			statecollect[inc].src=contractsymbol
			inc++
			}
		}
		function get_cookie(Name) {
			var search = Name + "="
			var returnvalue = "";
			if (document.cookie.length > 0) {
			offset = document.cookie.indexOf(search)
			if (offset != -1) {
			offset += search.length
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
			}
			}
			return returnvalue;
		}
		function getselectedItem(){
			if (get_cookie(window.location.pathname) != ""){
			selectedItem=get_cookie(window.location.pathname)
			return selectedItem
			}
			else
			return ""
		}
		function saveswitchstate(){
			var inc=0, selectedItem=""
			while (ccollect[inc]){
			if (ccollect[inc].style.display=="none")
			selectedItem+=ccollect[inc].id+"|"
			inc++
			}
			if (get_cookie(window.location.pathname)!=selectedItem){ //only update cookie if current states differ from cookie's
			var expireDate = new Date()
			expireDate.setDate(expireDate.getDate()+parseInt(memoryduration))
			document.cookie = window.location.pathname+"="+selectedItem+";path=/;expires=" + expireDate.toGMTString()
			}
		}
		function do_onload(){
			uniqueidn=window.location.pathname+"firsttimeload"
			var alltags=document.all? document.all : document.getElementsByTagName("*")
			ccollect=getElementbyClass(alltags, "switchcontent")
			statecollect=getElementbyClass(alltags, "showstate")
			if (enablepersist=="on" && get_cookie(window.location.pathname)!="" && ccollect.length>0)
			revivecontent()
			if (ccollect.length>0 && statecollect.length>0)
			revivestatus()
			sweeptoggle("contract")
		}
		if (window.addEventListener)
			window.addEventListener("load", do_onload, false)
		else if (window.attachEvent)
			window.attachEvent("onload", do_onload)
		else if (document.getElementById)
			window.onload=do_onload
		if (enablepersist=="on" && document.getElementById)
			window.onunload=saveswitchstate
	/* END ABOUT SECTION TOGGLE */

	/* Effects Functions */
	function blog_imgToggle(e,state){
		e.className = (state==0)?'blog_img_s1_hover':'blog_img_s1';
	}
	/* END Effects Functions */

	/* CUSTOM EFECTS */
	Effect.SlideUpAndDown = function(element) {
 			element = $(element);
 			if(Element.visible(element)) new Effect.SlideUp(element);
 			else new Effect.SlideDown(element);
	}

	Effect.SlideRightIntoView = function(element) {
	  $(element).style.width = '0px';
	  $(element).style.overflow = 'hidden';
	  //$(element).firstChild.style.position = 'relative';
	  Element.show(element);
	  new Effect.Scale(element, 100,
	    Object.extend(arguments[1] || {}, {
	      scaleContent: false,
	      scaleY: false,
	      scaleMode: 'contents',
	      scaleFrom: 0,
	      afterUpdate: function(effect){}
	    })
	  );
	}
	
	Effect.SlideRightOutOfView = function(element) {
	  $(element).style.overflow = 'hidden';
	  //$(element).firstChild.style.position = 'relative';
	  Element.show(element);
	  new Effect.Scale(element, 0,
	    Object.extend(arguments[1] || {}, {
	      scaleContent: false,
	      scaleY: false,
	      afterUpdate: function(effect){},
	      afterFinish: function(effect)
	        { Element.hide(effect.element); }
	    })
	  );
	}
	
	Effect.SlideLeftAndRight = function(element) {
	  element = $(element);
	  if(Element.visible(element)) new Effect.SlideRightOutOfView(element);
	  else new Effect.SlideRightIntoView(element);
	}

	/* END CUSTOM EFECTS */




	/* ======================= FORMS JS ========================== */
	
	
		// Insert Style Tags
	function ins_styles(theform,tag_code,prompt_text,tag_prompt) {
		// Insert <tag_code>xxx</tag_code> style markup
		// Get selected text
		var selected_text = getSelectedText(theform);
		if (selected_text == '') {
			// Display prompt if no text is selected
			var insert_text = prompt( 'Enter the text to be formatted:'+"\n<"+tag_code+">Your Input</"+tag_code+">", '' );
			if ( (insert_text != null) ) {
				insertAtCaret(theform, "<"+tag_code+">"+insert_text+"</"+tag_code+">");
				theform.focus();
			}
		} else {
			// Insert text automatically around selection
			insertAtCaret(theform, "<"+tag_code+">"+selected_text+"</"+tag_code+">");
			theform.focus();
		}
	}


	// Insert Image Tag
	function ins_image(theform,prompt_text) {
		// Insert [x]yyy[/x] style markup
		inserttext = prompt('Enter the URL for the image:'+"\n[img="+prompt_text+"xxx]",prompt_text);
		if ((inserttext != null) && (inserttext != "")) {
			insertAtCaret(theform, "[img="+inserttext+"]");
		}
		theform.focus();
	}


	// Insert Image Tag
	function ins_image_v2(theform) {
		image_url = prompt('Enter the URL for the image:'+'\n[img=http://xxx] or [img=xxx]\n\nOptional:\nwidth=xxx height=xxx popup=true/false float=left/right','http://');
		if ((image_url != null) && (image_url != '')) {
			// Optional
			image_width = prompt('Width (Optional):'+'\n[img=xxx width=xxx]','');
			image_height = prompt('Height (Optional):'+'\n[img=xxx height=xxx]','');
			image_popup = prompt('View full-size in pop-up when clicked (Optional):'+'\n[img=xxx popup=true/false]', '');
			image_float = prompt('Float (Optional):'+'\n[img=xxx float=left/right]','');
			str = '[img='+image_url;
			if ((image_width != null) && (image_width != '')) {
				str += ' width='+image_width;
			}
			if ((image_height != null) && (image_height != '')) {
				str += ' height='+image_height;
			}
			if ((image_popup != null) && (image_popup != '')) {
				image_popup.toLowerCase;
				if ( image_popup == 'true' || image_popup == 'false' ) {
					str += ' popup='+image_popup;
				}
			}
			if ((image_float != null) && (image_float != '')) {
				image_float.toLowerCase;
				if ( image_float == 'left' || image_float == 'right' ) {
					str += ' float='+image_float;
				}
			}
			str += ']';
			insertAtCaret(theform, str);
			theform.focus();
		}
	}


	// Insert URL Tag
	function ins_url(theform) {
		// inserts named url link - [url=mylink new=true]text[/url]
		link_url = prompt('Enter the full URL for the link:'+'\n[url=xxx][/url]',"http://");
		if ( (link_url != null) ) {
			// Get selected text
			var link_text = getSelectedText(theform);
			if (link_text == '') {
				// Display prompt if no text is selected
				link_text = prompt('Enter the text to be displayed for the link (Optional):'+'\n[url=]xxx[/url]',"");
			}
			if ( (link_text == null) || (link_text == '') ) {
				link_text = link_url;
			}
			link_target = prompt('Open URL in new window (Optional):'+'\n[url= new=true/false][/url]','');
			str = '[url='+link_url;
			if ((link_target != null) && (link_target != '')) {
				link_target.toLowerCase;
				if ( link_target == 'true' || link_target == 'false' ) {
					str += ' new='+link_target;

				}
			}
			str += ']'+link_text+'[/url]';
			insertAtCaret(theform, str);
			theform.focus();
		}
	}


	// Insert URL Tag
	function ins_url_no_options(theform) {
		// inserts named url link - [url=mylink new=true]text[/url]
		link_url = prompt('Enter the full URL for the link:'+'\n[url=xxx][/url]',"http://");
		if ( (link_url != null) ) {
			// Get selected text
			var link_text = getSelectedText(theform);
			if (link_text == '') {
				// Display prompt if no text is selected
				link_text = prompt('Enter the text to be displayed for the link (Optional):'+'\n[url=]xxx[/url]',"");
			}
			if ( (link_text == null) || (link_text == '') ) {
				link_text = link_url;
			}
			str = '[url='+link_url+']'+link_text+'[/url]';
			insertAtCaret(theform, str);
			theform.focus();
		}
	}


	//Insert Emoticon
	function ins_emoticon(theform, emoticon) {
		//alert(emoticon);
		insertAtCaret(theform, emoticon);
		theform.focus();
	}


	// From: http://parentnode.org/javascript/working-with-the-cursor-position/
	function insertAtCaret(obj, text) {
		if (document.selection && document.selection.createRange) {
			// Internet Explorer 4.0x
			obj.focus();
			var orig = obj.value.replace(/\r\n/g, "\n"); // IE Bug
			var range = document.selection.createRange();
			if(range.parentElement() != obj){
				return false;
			}
			range.text = text;
			var actual = tmp = obj.value.replace(/\r\n/g, "\n");
			for(var diff = 0; diff < orig.length; diff++){
				if(orig.charAt(diff) != actual.charAt(diff)) break;
			}
			for(var index = 0, start = 0;
				tmp.match(text)
					&& (tmp = tmp.replace(text, ""))
					&& index <= diff;
				index = start + text.length
			){
				start = actual.indexOf(text, index);
			}
		} else if (obj.selectionStart >= 0){
			// FireFox & Safari
			var start = obj.selectionStart;
			var end   = obj.selectionEnd;
			obj.value = obj.value.substr(0, start) + text + obj.value.substr(end, obj.value.length);
		}
		if (start != null) {
			setCaretTo(obj, start + text.length);
		} else {
			obj.value += text;
		}
	}


	function getSelectedText(obj) {
		if (document.selection && document.selection.createRange) {
			// Internet Explorer 4.0x
			obj.focus();
			var orig = obj.value.replace(/\r\n/g, "\n"); // IE Bug
			var range = document.selection.createRange();
			if(range.parentElement() != obj) {
				return '';
			}
			txt = range.text;
			return txt;
		} else if (obj.selectionStart >= 0) {
			// FireFox & Safari
			var start = obj.selectionStart;
			var end    = obj.selectionEnd;
			var txt    = obj.value.substr(start, end-start);
			return txt;
		} else {
			return '';
		}
	}


	function setCaretTo(obj, pos) {
		if(obj.createTextRange) {
			var range = obj.createTextRange();
			range.move('character', pos);
			range.select();
		} else if(obj.selectionStart) {
			obj.focus();
			obj.setSelectionRange(pos, pos);
		}
	}


	function getSel() {
		var txt = '';
		var foundIn = '';
		if (window.getSelection) {
			// the alternative code
			txt = window.getSelection();
			foundIn = 'window.getSelection()';
		} else if (document.getSelection) {
			// Navigator 4.0x
			txt = document.getSelection();
			foundIn = 'document.getSelection()';
		} else if (document.selection) {
			// Internet Explorer 4.0x
			txt = document.selection.createRange().text;
			foundIn = 'document.selection.createRange()';
		} else {
			return;
		}
		return txt;
	}




	/* ------------------------------------------------------------------------ */
	// Manuelx10.com Javascript Functions




	//Text Area HTML Code Preview
	function preview_text(htmldata,container){
		document.getElementById(container).innerHTML = htmldata;
	}

	var ns6 = document.getElementById&&!document.all;

	// Count Number of Characters in Place Holder
	function countlimit(e,placeholder){
		var theform = eval(placeholder);
		var curlength = theform.value.length;
		var placeholderobj = document.all ? document.all[placeholder] : document.getElementById(placeholder)
		if( window.event || e.target && e.target == eval(placeholder) ){
			placeholderobj.innerHTML=curlength;
		}
	}

	// Display Input Text Area Counter
	function displaycounter(theform){
		var limit_text='<b><span id="'+theform.toString()+'">'+0+'</span></b>';
		if (document.all||ns6)
			document.write(limit_text);
		if (document.all){
			eval(theform).onkeypress = function(){ }
			eval(theform).onkeyup = function(){ countlimit(event,theform) }
		}
		else if (ns6){
			document.body.addEventListener('keypress', function(event){  }, true);
			document.body.addEventListener('keyup', function(event){ countlimit(event,theform) }, true);
		}
	}


	// Submit once function
	function submitonce(thisobj){
		if( document.all || document.getElementById ){
			for( i=0; i < thisobj.length; i++ ){
				var tempobj = thisobj.elements[i];
				if( tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset" )
					tempobj.disabled=true;
			}
		}
	}
	






