// JavaScript Document
function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}
function htmlspecialchars(string, quote_style) {
    // http://kevin.vanzonneveld.net
    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno
    // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
    // *     returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
    
    string = string.toString();
    
    // Always encode
    string = string.replace(/&/g, '&amp;');
    string = string.replace(/</g, '&lt;');
    string = string.replace(/>/g, '&gt;');
    
    // Encode depending on quote_style
    if (quote_style == 'ENT_QUOTES') {
        string = string.replace(/"/g, '&quot;');
        string = string.replace(/'/g, '&#039;');
    } else if (quote_style != 'ENT_NOQUOTES') {
        // All other cases (ENT_COMPAT, default, but not ENT_NOQUOTES)
        string = string.replace(/"/g, '&quot;');
    }
    
    return string;
}
function createmodule(ids){	
	geturl = 'makemodule.php?'
	url = geturl;
	callback = function (){
		newlocation = 'editpage.html?' + ids;
		top.window.document.location.href=newlocation
		top.tb_remove();
		};
		if($('#moduleselect').val() == 'TextBlock'){			
			$('#modbody').html(tinyMCE.get('TextBlockInput').getContent());
		}
		
		modtitle = $("#ModuleTitle").val();
		modtitle = modtitle.replace(/&/g,'%26');
		moddescription = $("#ModuleDescription").val();
		moddescription = moddescription.replace(/&/g,'%26');
	
	if($('#moduleselect').val() == 'DBList'){

		data = "dbs=" + dbs + ids + '&title=' + modtitle + '&moddescription=' + moddescription + '&modtype=' + $("#moduleselect").val();
	}
	else if($('#moduleselect').val() == 'LinkList'){
		linknames = new Array();
		urls = new Array();
		classes = new Array();
		descriptions = new Array();
		i = 0;
		
		jQuery.each($("#modbody li"), function(){
			dbs[i] = $(this).attr("id");
			linknames[i] = $(this).text();
			urls[i] = $(this).contents('a').attr("href");								
			classes[i] = $(this).attr("class");
			descriptions[i] = $(this).contents('a').attr("title");
			if(linknames[i] != null){
						 linknames[i] = linknames[i].replace(/&/g,'%26');
					}
			 if(descriptions[i] != null){
						descriptions[i] = descriptions[i].replace(/&/g,'%26');
					}
			 if(urls[i] != null){
						urls[i] = urls[i].replace(/&/g,'%26');
					}
				
			i++;
			});		
			dbs = dbs.join('|||');
				linknames = linknames.join('|||');
				urls = urls.join('|||');
				classes = classes.join('|||');
				descriptions = descriptions.join('|||');
	// insides = "[";
	// for(x=0; x<i; x++){
	// insides = insides + '{"dbs": "' + dbs[x] + '", "linknames": "' + linknames[x] + '", "urls": "' + urls[x] + '", "classes": "' + classes[x] + '", "descriptions": "' + descriptions[x] + '"},'
	// }
	// insides = insides.slice(0, -1)
	// insides = insides + "]";
	// var myJSONObject = {"linkers": insides};
	// alert(myJSONObject);
		data = 'title=' + modtitle + '&moddescription=' + moddescription + ids + '&modtype=' + $("#moduleselect").val() + '&descriptions=' + descriptions + '&dbids=' + dbs + '&urls=' + urls + '&classes=' + classes + '&linknames=' + linknames;
			//data = 'title=' + $("#ModuleTitle").val() + '&moddescription=' + $("#ModuleDescription").val() + ids + '&modtype=' + $("#moduleselect").val() + '&jobject=' + myJSONObject;
			
			
	}
	else if($('#moduleselect').val() != 'RSS'){
	if($('#moduleselect').val() == 'HTMLBlock'){modbody = $("#HTMLBlockInput").val(); }
		else{
			modbody = $("#modbody").html(); 
			} 
	if(modbody != null){
						modbody = modbody.replace(/&/g,'%26');
					}
	data = 'data=' + modbody + '&title=' + modtitle + '&moddescription=' + moddescription + ids + '&modtype=' + $("#moduleselect").val();	
	
	}
	else{
	data = 'rssaddress=' + encodeURIComponent($("#rssaddress").val()) + '&rssnumrows=' + $("#rssnumrows").val() + '&title=' + modtitle + '&moddescription=' + moddescription + ids + '&modtype=' + $("#moduleselect").val();
	}
	jQuery.post( url, data, callback);	 
	}
	
function cancelpage(){
		var newlocation = 'editpage.html?' + ids;
		top.window.document.location.href=newlocation;
}
	
function updatemodule(ids){
		geturl = 'editmodule.php?'
	url = geturl;
	callback = function (){
		newlocation = 'editpage.html?' + ids;
	top.window.document.location.href=newlocation
	top.tb_remove();
		};
		if($('#moduleselect').val() == 'TextBlock'){			
			$('#modbody').html(tinyMCE.get('TextBlockInput').getContent());
		}
			
		modtitle = $("#ModuleTitle").val();
		modtitle = modtitle.replace(/&/g,'%26');
		moddescription = $("#ModuleDescription").val();
		moddescription = moddescription.replace(/&/g,'%26');
		
	if($('#moduleselect').val() != 'RSS' && $('#moduleselect').val() != 'LinkList'){
	
		if($('#moduleselect').val() == 'HTMLBlock'){modbody = $("#HTMLBlockInput").val(); }
		else{
			modbody = $("#modbody").html(); 
			}
	
		if(modbody != null){
				modbody = modbody.replace(/&/g,'%26');
				// modbody = modbody.replace(/\s+/g, " ");
				// modbody = modbody.replace(/^\s(.*)/, "$1"); 
				// modbody = modbody.replace(/(.*)\s$/, "$1"); 
				modbody = modbody.replace(/&nbsp;/g, "");
				// modbody = modbody.replace(/\x0A/g, "");
				// modbody =  modbody.replace(/^[\r\n]+$/g, "" ); 
				// modbody =  modbody.replace(/^[\n\r]+$/g, "" ); 
				// modbody=modbody.replace(/\r\n/g,"");  // replace CrLf with \n
				// modbody=modbody.replace(/\n\r/g,"");  // replace CrLf with \n
				// modbody=modbody.replace(/\n/g,"");    // replace single Lf with \n
				// modbody=modbody.replace(/\r/g,"");  
							
						}
	data = 'data=' + modbody + '&title=' + modtitle + '&moddescription=' + moddescription + ids + '&modtype=' + $("#moduleselect").val();	
	}
	else if($('#moduleselect').val() == 'LinkList'){
			linknames = new Array();
			urls = new Array();
			classes = new Array();
			descriptions = new Array();
			i = 0;
			
			
			jQuery.each($("#modbody li"), function(){			
			
				dbs[i] = $(this).attr("id");
				linknames[i] = $(this).text();
		
			if(linknames[i] != null){
						 linknames[i] = linknames[i].replace(/&/g,'%26');
					}
		
				urls[i] = $(this).contents('a').attr("href");								
				classes[i] = $(this).attr("class");				
					descriptions[i] = $(this).contents('a').attr("title");
				
					if(descriptions[i] != null){
						 descriptions[i] = descriptions[i].replace(/&/g,'%26');
					}
					 if(urls[i] != null){
						urls[i] = urls[i].replace(/&/g,'%26');
					}
				i++;
				});	
				dbs = dbs.join('|||');
				linknames = linknames.join('|||');
				classes = classes.join('|||');
				urls = urls.join('|||');
				descriptions = descriptions.join('|||');
// insides = "[";
	// for(x=0; x<i; x++){
	// insides = insides + '{"dbs": "' + dbs[x] + '", "linknames": "' + linknames[x] + '", "urls": "' + urls[x] + '", "classes": "' + classes[x] + '", "descriptions": "' + descriptions[x] + '"},'
	// }
	// insides = insides.slice(0, -1)
	// insides = insides + "]";
	// var myJSONObject = {"linkers": insides};
	// alert(myJSONObject);				
			data = 'title=' + modtitle + '&moddescription=' + moddescription + ids + '&modtype=' + $("#moduleselect").val() + '&descriptions=' + descriptions + '&dbids=' + dbs + '&urls=' + urls + '&classes=' + classes + '&linknames=' + linknames;
				//data = 'title=' + $("#ModuleTitle").val() + '&moddescription=' + $("#ModuleDescription").val() + ids + '&modtype=' + $("#moduleselect").val() + '&jobject=' + myJSONObject;
				
	}
	else{
	data = 'rssaddress=' + encodeURIComponent($("#rssaddress").val()) + '&rssnumrows=' + $("#rssnumrows").val() + '&title=' + modtitle + '&moddescription=' + moddescription + ids + '&modtype=' + $("#moduleselect").val();
	}	
	
	jQuery.post( url, data, callback);	
	}
	
	//takes the return xml from getSubjModules and displays modules on the 'edit page'
function createModXML(xml){
	$("module", xml).each(function(id) {	
	message = $("ModuleTitle", this).get();	
	moduleBox = '<div class="mod" id="' + $("ModuleID", this).text() + '"><div class="modtitle"><a style="float: right;"  class="thickbox" href="editmodule.html?modid=' + $("ModuleID", this).text() + '&keepThis=true&TB_iframe=true&height=600&width=600">edit</a>' + $("ModuleTitle", this).text() + '</div>' + $("HTMLBlob", this).text() + '</div>';
	$("#col1").append(moduleBox);
	}); 
	}	
	
function getSubjModules(pageid){
	url = 'getModules.php?pid=' + pageid;
	data = 'lala=' + pageid;
	lemons = jQuery.get(url, data, function(xml){createModXML(xml);});	
	}
	
function updateModLocation(col1, col2){	
	url = 'modlocation.php';
	data = 'col1=' + col1 + '&col2=' + col2 + '&pageid=' + pid;
	jQuery.post(url, data, function(){});
	}
function updatePageLocation(pagelist1){	
	url = 'pagelocation.php';
	data = 'pagelist1=' + pagelist1 + '&guideid=' + guideid;
	jQuery.post(url, data, function(){});
	}
	
function newpage(guideid){
	url = 'newpage.php';
	data = 'guideid=' + guideid;
	pageinsert = '<li><a href="#fragment-2"><span>new page</span></a></li>';
	jQuery.post(url, data, function(){
	window.location.reload()
		//$('#newpagemaker').before(pageinsert);							
		});
	}
	
function deleteModule(guideid, pageid, moduleid){
	url = 'deletemodule.php';
	data = 'guideid=' + guideid + '&pageid=' + pageid + '&moduleid=' + moduleid;
	jQuery.post(url, data, function (){
		newlocation = 'editpage.html?' + data;
		top.window.document.location.href=newlocation
		top.tb_remove();						 
		});
	}	
	
function addArray(){
	urldescrp = htmlspecialchars($('#URLdescription').val(), 'ENT_QUOTES');
	//alert(urldescrp);
	$('#modbody > ul').append('<li><a href="' + $('#URL').val() + '" title="' + urldescrp + '" >' + $('#URLname').val() + '</a></li>');
	$("#modbody > ul").sortable("refresh"); 	
	$("#trash").sortable("refresh"); 
	$('#URL').val("http://");
	$('#URLdescription').val("");
	$('#URLname').val("");	
	}
	
function addDBtoList(){
	//alert($('#dbselect option:selected').val());
	dbhold = '<li id="' + $("#dbselect option:selected").attr("id") + '" class="eresource" ><a title="">' + $('#dbselect option:selected').text() + '</a></li>';
	$('#modbody > ul').append(dbhold);	
  $("#modbody > ul").sortable("refresh"); 	
  $("#trash").sortable("refresh"); 
  	$('#URL').val("http://");
	$('#URLdescription').val("");
	$('#URLname').val("");
	}

function saveGuide(){
		if(uniqueguidelabelcheck == 'yes' && uniquepagelabelcheck =='yes'){
			var url = 'saveguide.php';
			//var pagetabname = escape($('#PageTabName').val());
			//pagetabname = pagetabname.replace(/&/g,'%26');
		//var data = 'guideid=' + guideid + '&pageid=' + pid + '&pagetitle=' + $('#PageTitle').val() +'&pagedescription=' + $('#PageDescription').val() +'&pagetabname=' + pagetabname +'&guidetitle=' + $('#GuideTitle').val() + '&guidedescription=' + $('#GuideDescription').val() + '&uniqueguidelabel=' + $('#UniqueGuideLabel').val() + '&uniquepagelabel=' + $('#UniquePageLabel').val();
		data=$("form").serialize();
		jQuery.post(url, data, function(){
					return true;
					window.location.reload()					
				});	
		}
		else{alert('Unique Guide Label or Page Label is not correct'); return false;}
		}
	
function checkUniqueGuide(term){
	url = 'checkUniqueGuide.php';
	data = 'guidelabel=' + term + '&guideid=' + guideid;
	jQuery.post(url, data, function(data){
		//$('#uniquetaken').html($('guidetaken').text());
		if($('guidetaken',data).text() != '' || term == '' || $('specchar',data).text() != ''){
		uniqueguidelabelcheck = 'no';
			if(term == ''){$('#uniquetaken').html('<img src="images/stop.png" /><span style="color: #666666;" >This Guide Label cannot be blank</span>');}
			else if($('specchar',data).text() != ''){
				$('#uniquetaken').html('<img src="images/stop.png" /><span style="color: #666666;" >' + $('specchar',data).text() + '</span>');
			}
			else{
				$('#uniquetaken').html('<img src="images/stop.png" /><span style="color: #666666;" >This Guide Label has been used already. Please choose another one.</span>');
			}
		}
		else{
		uniqueguidelabelcheck = 'yes';
		$('#uniquetaken').html('<img src="images/go.png" />');
		}
	})
}
function checkUniquePage(terms){
	url = 'checkUniquePage.php';
	data = 'pagelabel=' + terms + '&guideid=' + guideid + '&pageid=' + pid;
	
	jQuery.post(url, data, function(data){
		if($('pagetaken',data).text() != '' || terms == '' || $('specchar',data).text() != ''){
		uniquepagelabelcheck = 'no';
			if(terms == ''){$('#uniquepagetaken').html('<img src="images/stop.png" /><span style="color: #666666;" >This Page Label cannot be blank</span>');}
			else if($('specchar',data).text() != ''){
				$('#uniquepagetaken').html('<img src="images/stop.png" /><span style="color: #666666;" >' + $('specchar',data).text() + '</span>');
			}
			else{
				$('#uniquepagetaken').html('<img src="images/stop.png" /><span style="color: #666666;" >This Page Label has been used already. Please choose another one.</span>');
			}
		}
		else{
		uniquepagelabelcheck = 'yes';
		$('#uniquepagetaken').html('<img src="images/go.png" />');
		}
	});
}
function addDB(){
		dbs = new Array();
		i = 0;
		jQuery.each($(".db:checked"), function(){
		dbs[i] = $(this).val();
		i++;
			});		
			return dbs;
	}
function addAdmin(id){
		url="editadmin.php";
		data="guideid=" + guideid + "&admin=" + id + "&type=add";
		jQuery.post(url, data, function(xml){
			admin = $('admin',xml).text();	
					$('#adminlist').append("<li >" + admin + "&nbsp;&nbsp;<a class='deleteadmin' href='#deleteadmin' id='" + admin + "'>delete admin</a></li>");		
					$(".deleteadmin").click(function(){						
						deleteAdmin($(this).attr("id"));	
					});			
		});
		
		
	}
	
function deleteAdmin(id){
		url="editadmin.php";
		data="guideid=" + guideid + "&admin=" + id + "&type=delete";
	jQuery.post(url, data, function(xml){
	admin = $('admin',xml).text();	
	
		jQuery.each($('li'), function(){
		
			if($(this).contents('a').attr("id")	 ==  admin){
				$(this).remove();
				}
			});
		
		});
	}
	
function publishGuide(guide, checkvalue){
	url = "publishguide.php";
	data = "guide=" + guide + "&checkvalue=" + checkvalue;
	jQuery.post(url, data);
	
	}

function publishPage(guide, page, checkvalue){
	url = "publishpage.php";
	data = "page=" + page + "&guide=" + guide + "&checkvalue=" + checkvalue;
	jQuery.post(url, data);
	
	}

function browserCheck() {
	var agent= navigator.userAgent.toLowerCase();
	var ver = parseInt(navigator.appVersion);
	 
	var ie = agent.indexOf("msie")>=0;
	var ie6=ie && agent.indexOf("msie 6")>=0;
	var ie7=ie && agent.indexOf("msie 7")>=0;
	 
	var ff=!ie && agent.indexOf("mozilla")>=0;
	var ff2=ff && ver==4;
	var ff3=ff && ver==5;
	 
	 return ie;
	}