//pop-up window

var commonjsversion = 2.0;

document.write("<style> .javascriptOnly { display:block; }</style>"); // show javascript reliant content - these should be hidden by previous CSS

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Populate SEO fields
function seoPopulate(title,content) {
	if (typeof content == 'undefined') var content = title; // if no content sent - just use title
title = title.replace(/[^a-zA-Z 0-9-]+/g,''); // get rid of tags, non-alphanumeric, spaces and dashes
content = removeHTMLTags(content);
content = content.replace(/[^a-zA-Z 0-9/-]+/g,''); // get rid of tags, non-alphanumeric, spaces and dashes
longID = title.replace('- ',''); // get rid of long dashes
longID = longID.replace(/[ ]+/g,'_'); // replace spaces with underscores
if (document.getElementById('longID').value == "") document.getElementById('longID').value = longID;
if (document.getElementById('metadescription').value == "") document.getElementById('metadescription').value = truncate(content,250);
if (document.getElementById('metakeywords').value == "") document.getElementById('metakeywords').value = title;
} // end function

//Bookmark code
function addBookmark(title, url) {
	url = url.replace(/\?SearchTerm=(\d)*/,"")
	url = 'http://' + window.location.host + url
	if (window.sidebar) { // firefox
					window.sidebar.addPanel(title, url,"");
				} else if( document.all ) { //MSIE
	                window.external.AddFavorite(url,title);
				} else {
					alert("Your browser doesn't support automatic bookmarking.\n\nIn Firefox or Safari use the Bookmarks menu.");
				}
}

//print page
function printPage(){
	window.print();
}



function waitMessage() {
 document.getElementById('submit').disabled = true;
   document.getElementById('uploadwarning').style.visibility='visible';
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(HTML){
 	
	
 	 	/*HTML = HTML.replace(/&(lt|gt);/g, function (strMatch, p1) {
 		 	return (p1 == "lt")? "<" : ">";
 		});*/
 		var cleaned = HTML.replace(/(<([^>]+)>)/ig," "); // replace tags with spaces
		cleaned = cleaned.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' '); // get rid of extra spaces
 		return cleaned;	
   
 	
}

// Input parameters:
// String text, [Number length, String ellipsis]
// Returns:
// String text

function truncate(text, length, ellipsis) {    
// Set length and ellipsis to defaults if not defined
if (typeof length == 'undefined') var length = 100;
if (typeof ellipsis == 'undefined') var ellipsis = '...';
// Return if the text is already lower than the cutoff
    if (text.length < length) return text;    
	 // Otherwise, check if the last character is a space.   
	 // If not, keep counting down from the last character   
	 // until we find a character that is a space   
	 for (var i = length-1; text.charAt(i) != ' '; i--) {         
	 length--;
	 }
	 // The for() loop ends when it finds a space, and the length var
	 // has been updated so it doesn't cut in the middle of a word.
	 return text.substr(0, length) + ellipsis; 
	 }   
	 
function setCookie( name, value, expires, path, domain, secure )
{
// backwards compat	
path = (typeof(path)=="undefined") ? "" : path;
domain = (typeof(path)=="undefined") ? "" : domain;
secure = (typeof(path)=="undefined") ? "" : secure;

// set time, it's in milliseconds
var today = new Date();

today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}



function getCookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
	
path = (typeof(path)=="undefined") ? "" : path;
domain = (typeof(path)=="undefined") ? "" : domain;

if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


function getRadioValue(idOrName) {
        var value = null;
        var element = document.getElementById(idOrName);
        var radioGroupName = null;  
        
        // if null, then the id must be the radio group name
        if (element == null) {
                radioGroupName = idOrName;
        } else {
                radioGroupName = element.name;     
        }
        if (radioGroupName == null) {
                return null;
        }
        var radios = document.getElementsByTagName('input');
        for (var i=0; i<radios.length; i++) {
                var input = radios[ i ];    
                if (input.type == 'radio' && input.name == radioGroupName && input.checked) {                          
                        value = input.value;
                        break;
                }
        }
        return value;
}

function setSelectListToValue(value, selectId){
	var i, si, v, args=setSelectListToValue.arguments;
	if ((obj=document.getElementById(args[1])) != null){
		v = args[0];
		for(i=0; i<obj.length; i++){
			if(obj.options[i].value == v){
				si = i;
			}
		}
		obj.selectedIndex = si;
	}
}

function checkUncheckAll(theElement) {
     var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
	  theForm[z].checked = theElement.checked;
	  }
     }
    }
	
function changecss(theClass,element,value) { // e.g. changecss(".payForm","display","table-cell"); NOTE DOT!
	
	 var cssRules;

	 var added = false;
	 for (var S = 0; S < document.styleSheets.length; S++){

    if (document.styleSheets[S]['rules']) {
	  cssRules = 'rules';
	 } else if (document.styleSheets[S]['cssRules']) {
	  cssRules = 'cssRules';
	 } else {
	  //no rules found... browser unknown
	 }

	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    if(document.styleSheets[S][cssRules][R].style[element]){
	    document.styleSheets[S][cssRules][R].style[element] = value;
	    added=true;
		break;
	    }
	   }
	  }
	  if(!added){
	  if(document.styleSheets[S].insertRule){
			  document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			} else if (document.styleSheets[S].addRule) {
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
	  }
	 }
	}
	
	
function getUrlVars()
{ // returns vars[variable1name] = variable1value, vars[variable2name] = variable2value, etc...
    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 setSelRange(inputEl, selstart, selEnd) { // sets the selection range in a texbox or text area
 if (inputEl.setSelectionRange) { 
  inputEl.focus(); 
  inputEl.setSelectionRange(selstart, selEnd); 
 } else if (inputEl.createTextRange) { 
  var range = inputEl.createTextRange(); 
  range.collapse(true); 
  range.moveEnd('character', selEnd); 
  range.movestart('character', selstart); 
  range.select(); 
 } 
}

function addToFavourites(url,pagetitle) { 
if(confirm('Do you want to add this page to your favourites?\n\nIt will appear on the list in your Control Panel home page.')) {
getData("/admin/favourites/ajax/addtofavourites.php?url="+escape(url)+"&pagetitle="+escape(pagetitle),"favouritescallback");
}
return false;
}


function submitAndGo(url) {
	document.getElementById('returnURL').value = url;
	document.forms[0].submit();
	return false;
}

function writeEmail(recipient, domain) { // email obfuscation routines
   email = recipient+"@"+domain;
	document.write("<a href='mailto:"+email+"'>"+email+"</a>");
 }
 
function printFormatWindow() {
	// offers to convert all texboxes into paragraphs then prints
	
	var pageContent = document.getElementById('mainContent').innerHTML;
	if(pageContent.indexOf("<textarea")>0) {
		if(confirm('Do you want to convert text boxes for print?\n\nScroll bars will be removed and text boxes will no longer be editable.')) {
			pageContent = pageContent.replace(/<textarea/g,"<p style='white-space:pre-wrap; height: auto;'"); 
			pageContent = pageContent.replace(/textarea/g,"p");
			document.getElementById('mainContent').innerHTML = pageContent;
		}	
	}	
	window.print();
}

function openMainWindow(url) { // function to open the main site from Control Panel in sepcified url
	if(typeof(mainSiteWindow)=="undefined") {
		if(top.opener && !top.opener.closed) {
			mainSiteWindow = top.opener;
		} else { 
			mainSiteWindow = window.open(url,'mainSite');
		}
	}
	mainSiteWindow.href = url;
	mainSiteWindow.focus();
}