var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function initTip(){
	jQuery(".tooltip-target").ezpz_tooltip();
	jQuery(".toolpic-target").ezpz_tooltip({
	contentPosition: 'aboveFollow'});
}

function switchContent(topic) {
	
	str = 'content.php?topic='+topic;
	
		jQuery.ajax({
			  url: str,async: true,
				success: function(data) {
				jQuery("#mainContent").hide();	
				jQuery("#mainContent").html(data);
				jQuery("#mainContent").fadeIn(500);
;
			//	window.location.hash = topic;
				// focus on details
			  }
			});
		
	str = 'titles.php?topic='+topic;
	
		jQuery.ajax({
			  url: str,async: true,
				success: function(data) {
			//	jQuery("#mainTitle").hide();	
				jQuery("#mainTitle").html(data);
			//	jQuery("#mainTitle").fadeIn(500);
				// focus on details
			  }
			});	
			
}

function switchContent2(topic,id) {
	
	str = 'content.php?topic='+topic+'&lid='+id;
	
		jQuery.ajax({
			  url: str,async: true,
				success: function(data) {
				jQuery("#mainContent").html(data);
				// focus on details
			  }
			});
		
	str = 'titles.php?title='+topic;
	
		jQuery.ajax({
			  url: str,async: true,
				success: function(data) {
				jQuery("#mainTitle").html(data);
				// focus on details
			  }
			});	
		
		
	
}

function setNav(page){
	// this function sets the navigation highlight color based on the current page we are on.
	 if ($(page) != undefined) {
	$(page).addClassName('current');
	 }
	//$('fnav'+page).addClassName('current');
}

function pageit(actions, page, rpp, searchit,loc) {
//	alert('here');
	if (loc == 'admin') {   // whether we are running from admin panel or main
		str = '../flow.php?page='+page+'&action='+actions+'&rpp='+rpp+'&search='+searchit+'&loc='+loc;
	} else {
		str = 'flow.php?page='+page+'&action='+actions+'&rpp='+rpp+'&search='+searchit+'&loc='+loc;
	}
	var myAjax = new Ajax.Request(
			str, 
			{
				method: 'post', 
				asynchronous: true,  
				parameters: '', 
				onFailure: function (response) {  
                   $('showContent').update(response.statusText);
                     },  
				onLoading: function (response) {
                $('showStatus').update('<span class="loading"><img src="_images/loading.gif" title="Loading..." alt="Loading..." border="0" /></span>')},
				onComplete: function(response) {
					result = response.responseText;
					$('showContent').update(result);
					$('showStatus').update('');
				//	initTip();
				//	resarray = result.split("**SPLIT**",2);
               	 //       $('showContent').update(resarray[0]);
				//		$('showNav').update(resarray[1]);
				//	$('showPeople').innerHTML = response.responseText;
				}
			});	
}


/* menu Code START */

function jsddm_open()
{	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = jQuery(this).find('ul').eq(0).css('visibility', 'visible');}

function jsddm_close()
{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

jQuery(document).ready(function()
{	jQuery('#jsddm > li').bind('mouseover', jsddm_open);
	jQuery('#jsddm > li').bind('mouseout',  jsddm_timer);});

document.onclick = jsddm_close;

/* Menu CODE END */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function sendFormAddComment2(event) {  
	
                 // we stop the default submit behaviour  
				 $('commentFeedback').show();
                 var oOptions = {  
                     method: "POST",  
                     parameters: Form.serialize("frm_contact"),  
                     asynchronous: true,  
                     onFailure: function (oXHR) {  
                         $('commentFeedback').update(oXHR.statusText);
                     },  
                     onLoading: function (oXHR) {
                         $('loadFeedback').update('<span class="saving"><img src="_images/loading.gif" title="Saveing..." alt="Saving..." border="0" /></span>');  
                     },                            
                     onSuccess: function(oXHR) {  
				     	$('loadFeedback').update('');
                        $('commentFeedback').update(oXHR.responseText);
						$('name').value = '';
					    $('email').value = '';
						$('phone').value = '';
					    $('message').value = '';
						jQuery(document).ready(function() {
jQuery("#frm_contact").validationEngine({	
		  success : function() { sendFormAddComment2() },
		  failure : false })
      })
                     }                 
                 };  
//				 var currdiaryid = 14;
                 var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "dbmodify.php?action=contact", oOptions);             
   }

function sendFormHomeBuyer(event) {  
	
                 // we stop the default submit behaviour  
			
                 var oOptions = {  
                     method: "POST",  
                     parameters: Form.serialize("frm_HomeBuyer"),  
                     asynchronous: true,  
                     onFailure: function (oXHR) {  
                      
                     },                            
                     onSuccess: function(oXHR) { 
					 window.location.href = 'index.php?page=thanks&mess='+oXHR.responseText;
				     	
                     }                 
                 };  
//				 var currdiaryid = 14;
                 var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "dbmodify.php?action=buy_home", oOptions);             
   }

function sendFormHomeSeller(event) {  
	
                 // we stop the default submit behaviour  
			
                 var oOptions = {  
                     method: "POST",  
                     parameters: Form.serialize("frm_HomeSeller"),  
                     asynchronous: true,  
                     onFailure: function (oXHR) {  
                      
                     },                            
                     onSuccess: function(oXHR) { 
					 window.location.href = 'index.php?page=thanks&mess='+oXHR.responseText;
				     	
                     }                 
                 };  
//				 var currdiaryid = 14;
                 var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "dbmodify.php?action=sell_home", oOptions);             
   }
   
function sendFormMailingJoin() {  	
                 // we stop the default submit behaviour  
				 $('commentFeedback').show();
                 var oOptions = {  
                     method: "POST",  
                     parameters: Form.serialize("frm_mailing"),  
                     asynchronous: true,  
                     onFailure: function (oXHR) {  
                         $('commentFeedback').update(oXHR.statusText);
                     },  
                     onLoading: function (oXHR) {
                         $('commentFeedback').update('<span class="Wait...">&nbsp;&nbsp;<img src="_images/loading.gif" title="Wait..." alt="Wait..." border="0" /></span>');  
                     },                            
                     onSuccess: function(oXHR) {  
					    $('email').value = 'Email';
						$('firstname').value = 'First Name';
				//		$('lastname').value = 'Last Name';
						$('commentFeedback').update(oXHR.responseText);
                     }                 
                 };  
                 var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "dbmodify.php?action=mailing", oOptions);             
   }
function showQuestion(id) { 

        var myAjax = new Ajax.Request(
			'../dbread.php?action=showQuestion&id='+id, 
			{
				method: 'get', 
				parameters: '', 
				/*onLoading: function (response) {
                $('rateMe').update('<span class="ratewait"><img src="_images/loading-black.gif" title="Working..." alt="Working..." border="0" /></span>')},*/
				onComplete: function(response) {
					$('showQuestion').innerHTML = response.responseText;
					jQuery(document).ready(function(){			
					jQuery('textarea').elastic();
							});	
					jQuery(document).ready(function() {
					jQuery("#frm_question").validationEngine()
     				 });
				//	new Effect.ScrollTo('View');  
				}
			});	             
   }

function showJobMain(id) { 

        var myAjax = new Ajax.Request(
			'dbread.php?action=showJobMain&id='+id, 
			{
				method: 'get', 
				parameters: '', 
				onLoading: function (response) {
                $('showJob').update('<span class="loading"><img src="_images/loading.gif" title="Loading..." alt="Loading..." border="0" /></span>')},
				onComplete: function(response) {
					$('showJob').innerHTML = response.responseText;
				}
			});	             
   }    
   
function sendFormFTPUpload(event) { 
	
		$('loadFeedback').update('<span class="saving">Uploading Files <img src="_images/loading.gif" title="Saveing..." border="0" /></span>');  
				
}

function sendFormStopFTPUpload(result, message) {
			
			window.location.href = 'index.php?page=4&result='+result;
		
}

   

function letmeknow(what) {  

	// send request to phpbb3 and change default language
	var myAjax = new Ajax.Request(
			'dbmodify.php?action=letmeknow&what='+what, 
			{
				method: 'get', 
				parameters: '', 
				asynchronous: false,
				onSuccess: function(response) {
					translate = response.responseText;
				}
			});	
}

function doDelPost(postID) {  
	
	var myAjax = new Ajax.Request(
			'../dbmodify.php?action=delPost&postID='+postID, 
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					jQuery(this).dialog('close');
					window.location="index.php?page=3&messagefeedback=Posting Has been Deleted";	
						
				}
			});	
}

function delTrans(transID) {

var txt = 'Are you sure you want to delete this Transaction?';

jQuery.prompt(txt, { 
			buttons:{Delete:true, Cancel: false},
			callback: function(v,m,f){
			if(v){
				doDelTrans(transID);
		 	}}});

}

function doDelTrans(transID) {  
	
	var myAjax = new Ajax.Request(
			'../dbmodify.php?action=delTrans&transID='+transID, 
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					jQuery.prompt('Transaction has been deleted', {
								  callback: function(){window.location.href = 'index.php?page=4';}
								  }
								  );
				}
			});	
}

function delResult(resID) {
var txt = 'Are you sure you want to delete this Result?';

jQuery.prompt(txt, { 
			buttons:{Delete:true, Cancel: false},
			callback: function(v,m,f){
			if(v){
			
				str = '../dbmodify.php?action=delResult&resid='+resID;	
				jQuery.ajax({
					  url: str,async: false,
						success: function(data) {
							jQuery.prompt('Result has been deleted', {
								  callback: function(){window.location.reload()}
								  }
								  );
						
					  }
					});
		 	}}});

}

function delEvent(eventID) {
var txt = 'Are you sure you want to delete this Event?';

jQuery.prompt(txt, { 
			buttons:{Delete:true, Cancel: false},
			callback: function(v,m,f){
			if(v){
			
				str = '../dbmodify.php?action=delEvent&eventID='+eventID;	
				jQuery.ajax({
					  url: str,async: false,
						success: function(data) {
							jQuery.prompt('Event has been deleted', {
								  callback: function(){window.location.href = 'index.php?page=8';}
								  }
								  );
						
					  }
					});
		 	}}});

}

function delCust(custID) {

var txt = 'Are you sure you want to delete this Customer Record?';

jQuery.prompt(txt, { 
			buttons:{Delete:true, Cancel: false},
			callback: function(v,m,f){
			if(v){
				doDelCust(custID);
		 	}}});

}

function doDelCust(custID) {  
	
	var myAjax = new Ajax.Request(
			'../dbmodify.php?action=delCust&custID='+custID, 
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					jQuery.prompt('Customer Record has been deleted', {
								  callback: function(){window.location.href = 'index.php?page=2';}
								  }
								  );
				}
			});	
}

function delQuestion(questionID) {

var txt = 'Are you sure you want to delete this Question?';

jQuery.prompt(txt, { 
			buttons:{Delete:true, Cancel: false},
			callback: function(v,m,f){
			if(v){
				doDelQuestion(questionID);
		 	}}});

}

function doDelQuestion(questionID) {  
	var myAjax = new Ajax.Request(
			'../dbmodify.php?action=delQuestion&questionID='+questionID, 
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					jQuery.prompt('Question has been deleted', {
								  callback: function(){window.location.href = 'index.php?page=3';}
								  }
								  );
				}
			});	
}

function remName(a, b){
if(a.value==b){
a.value='';
}else if(a.value==''){
a.value=b;
}else{
a.value=a.value;
}
}
 
function chkName(a, b){
if(a.value==''){
a.value=b;
}else{
a.value=a.value;
}
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}



function getCatInfo(cid, types) {  
		
	var myAjax = new Ajax.Request(
			'../dbread.php?action=getCatInfo&cid='+cid+'&types='+types, 
			{
				method: 'get', 
				parameters: '', 
				asynchronous: false,
				onComplete: function(response) {
				//	resarray = "hello";	
					result = response.responseText;
				    resarray = result.split("**SPLIT**",4);	
				}
			});
	return resarray;
}

function fetchEvent(id) {
//	grabs and displays calendar event

	// get info from db
	
	var dirs=window.location.href.split('/'),
	cdir=dirs[dirs.length-2];

if (cdir == 'admin') {
	str = '../dbread.php?action=fetchEvent&id='+id;
} else {
	str = 'dbread.php?action=fetchEvent&id='+id;
}			
		jQuery.ajax({
			  url: str,async: true,
				success: function(data) {
				jQuery("#eventDetails").html(data);
				// focus on details
				$('eventD').scrollTo();

			  }
			});
	

}

function editCat(type,types, index){
	
	var name = "content";
	var description = "content";
		
// fetch name and description from database

var catInfo = getCatInfo(index, types);
    name = catInfo[0];
	description = catInfo[1];
	active = catInfo[2];
	shortname = catInfo[3];
	if (active == 'Y') {
		checkedstr = 'CHECKED';
	} else {
		checkedstr = '';
	}

 if (types == 'categories') {
	
		str = '../dbread.php?action=leagueAssociation&team_id='+index;
		
		jQuery.ajax({
			  url: str,async: false,
				success: function(data) {
				index2 = data;
			  }
			});
	
		str = '../dbread.php?action=leagueSelect&league_id='+index2+'&first_item=Select League&required=yes';
	
		 jQuery.ajax({
			  url: str,async: false,
				success: function(data) {
				leagueS = '<br />League This Team Belongs to:<br />'+data+'<br />';
			  }
			});
		 extraFields = '';
} else {
	leagueS = '<input type="hidden" id="league_id" name="league_id" value="none" />';
	extraFields = 'Short Name<br/><input type="text" class="niceInput" id="shortname" name="shortname" value="'+ shortname +'" /><br/>';
}
	
	// add this to the txt var below to enable "moving of teams to different leagues" <br />'+leagueS+'
	
var txt = '<div class="txt2">Modify This Information<br /><form id="frm_catEdit" method="post">Name<br /><input type="text" class="niceInput" id="cname" name="cname" value="'+ name +'" /><br />'+extraFields+'Description<br /><textarea class="niceArea" id="cdescription" name="cdescription">'+description+'</textarea><br /><br/>Active <input name="active" id="active" type="checkbox" value="Y" '+checkedstr+' /></form></div>';	

			jQuery.prompt(txt,{ 
					buttons:{Change:true, Cancel:false},
					overlayspeed : 1,
					opacity: 0.4,
					callback: function(v,m,f){
						if(v){
						str = '../dbmodify.php?action=updateCatInfo&cid='+index+'&name='+escape(f.cname)+'&description='+escape(f.cdescription)+'&type='+type+'&types='+types+'&active='+f.active+'&lid='+f.league_id+'&short_name='+escape(f.shortname);
						var myAjax = new Ajax.Request(str,
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					results = response.responseText;
					jQuery.prompt(results, {
								  callback: function(){window.location.reload();}
								  }
								  );
				}
			});
						
					}
					
					}});

}

function showRecord(memid) { 

        var myAjax = new Ajax.Request(
			'../dbread.php?action=showRecord&memid='+memid, 
			{
				method: 'get', 
				parameters: '', 
				/*onLoading: function (response) {
                $('rateMe').update('<span class="ratewait"><img src="_images/loading-black.gif" title="Working..." alt="Working..." border="0" /></span>')},*/
				onComplete: function(response) {
					$('showRecord').innerHTML = response.responseText;
					jQuery(document).ready(function() {
jQuery("#frm_record").validationEngine()
      });
				}
			});	             
   }       


function addCat(type, types,lid){

// add category or location based on level and description to database
	
var index = 0;

var txt = '<div class="txt2">Create New<br /><form id="frm_catAdd" method="post">Name<br /><input type="text" class="niceInput" id="cname" name="cname" value="" /><br />Description<br /><textarea class="niceArea" id="cdescription" name="cdescription"></textarea><br /><br />Active <input name="active" id="active" type="checkbox" value="Y" /></form></div>';	

			jQuery.prompt(txt,{ 
					buttons:{Create:true, Cancel:false},
					overlayspeed : 1,
					opacity: 0.4,
					callback: function(v,m,f){
						if(v){	
						var myAjax = new Ajax.Request('../dbmodify.php?action=addCatInfo&type='+type+'&types='+types+'&name='+escape(f.cname)+'&description='+escape(f.cdescription)+'&active='+f.active+'&lid='+lid,
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					results = response.responseText;
					jQuery.prompt(results, {
								  callback: function(){window.location.reload();}
								  }
								  );
					
				}
			});
						
					}
					
					}});
}

function delCat(type, types, index) {
	
	
	txt = 'Are you sure you want to delete this?';
	jQuery.prompt(txt, { 
			buttons:{Delete:true, Cancel: false},
			opacity: 0.4,
			focus: 1,
			overlayspeed : 'fast',
			callback: function(v,m,f){
			if(v){
				var myAjax = new Ajax.Request('../dbmodify.php?action=delCat&cid='+index+'&type='+type+'&types='+types, 
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					results = response.responseText;
					jQuery.prompt(results, {
								  callback: function(){window.location.href = 'index.php?page=6';}
								  }
								  );
				}
			});
		 	}}});
}


function addPage(level, parent){

// add category or location based on level and description to database
	
var index = 0;

var txt = '<div class="txt2">Create New Page<br /><form id="frm_pageAdd" method="post">Menu Item Name<br /><input type="text" class="niceInput" id="name" name="name" value="" /></form></div>';	

			jQuery.prompt(txt,{ 
					buttons:{Create:true, Cancel:false},
					overlayspeed : 1,
					opacity: 0.4,
					callback: function(v,m,f){
						if(v){	
						var myAjax = new Ajax.Request('../dbmodify.php?action=addPageInfo&level='+level+'&parent='+parent+'&name='+escape(f.name),
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					results = response.responseText;
					jQuery.prompt(results, {
								  callback: function(){window.location.reload();}
								  }
								  );
					
				}
			});
						
					}
					
					}});
}

function delPage(level, id) {
	
	
	txt = 'Are you sure you want to delete this Menu Item? You will lose all of it\'s sub levels and related content pages.';
	jQuery.prompt(txt, { 
			buttons:{Delete:true, Cancel: false},
			opacity: 0.4,
			focus: 1,
			overlayspeed : 'fast',
			callback: function(v,m,f){
			if(v){
				var myAjax = new Ajax.Request('../dbmodify.php?action=delPage&level='+level+'&id='+id, 
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					results = response.responseText;
					jQuery.prompt(results, {
						callback: function(){window.location.href = 'index.php?page=3';}
								  }
								  );
				}
			});
		 	}}});
}


function editPage(content_id){

// add category or location based on level and description to database
	

str = '../dbmodify.php?action=getPage&id='+content_id;
		
		jQuery.ajax({
			  url: str,async: false,
				success: function(data) {
				name = data;
			  }
			});


var txt = '<div class="txt2">Edit Page<br /><form id="frm_editAdd" method="post">Menu Item Name<br /><input type="text" class="niceInput" id="name" name="name" value="'+name+'" /></form></div>';	

			jQuery.prompt(txt,{ 
					buttons:{Update:true, Cancel:false},
					overlayspeed : 1,
					opacity: 0.4,
					callback: function(v,m,f){
						if(v){	
						var myAjax = new Ajax.Request('../dbmodify.php?action=updatePageInfo&id='+content_id+'&name='+escape(f.name),
			{
				method: 'get', 
				parameters: '', 
				asynchronous: true,
				onComplete: function(response) {
					results = response.responseText;
					jQuery.prompt('Edit Successful', {
								  callback: function(){window.location.reload();}
								  }
								  );
					
				}
			});
						
					}
					
					}});
}


function initEditor(config) {
	
	if (config == 1) {
		css = "../js/tiny_mce/mce_style.css";
	} else {
		css = "../js/tiny_mce/mce_style.css";
	}
	
	tinyMCE.init({
        // General options
        mode : "textareas",
        theme : "advanced",
        plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

        // Theme options
       theme_advanced_buttons1 : "forecolor, bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect",
						theme_advanced_buttons2_add : "link,unlink,anchor,|,image,formatselect, media",	
						forced_root_block : false,
   force_br_newlines : false,
   force_p_newlines : false,
   remove_linebreaks : true,
   convert_urls : false,

						theme_advanced_toolbar_location : "top",
						theme_advanced_toolbar_align : "left",
						theme_advanced_statusbar_location : "bottom",

        // Skin options
        skin : "o2k7",
        skin_variant : "silver",

        // Example content CSS (should be your site CSS)
        content_css : css,

        // Drop lists for link/image/media/template dialogs
        template_external_list_url : "js/template_list.js",
        external_link_list_url : "js/link_list.js",
        external_image_list_url : "js/image_list.js",
        media_external_list_url : "js/media_list.js",

        // Replace values for the template plugin
        template_replace_values : {
                username : "Some User",
                staffid : "991234"
        }
});


	
}
