var docroot = "";
var importSelectedAppendedOption = new Array(0,0,0,0,0,0,0,0);
var d = document;

function redirect()
{
	if( document.menu.urlstring.value != "NULL" )
	{
		window.location = docroot+document.menu.urlstring.value;
	}
}

function content_redirect()
{
	if( document.content_menu.content_type.value != "NULL" )
	{
		window.location = docroot+document.content_menu.content_type.value;
	}
}

function mcontent_redirect()
{
	if( document.content_menu.content_type.value != "NULL" )
	{
		window.location = docroot+document.content_menu.content_type.value;
	}
}

function popup(file,winName,w,h)
{
	if (w && h) {
		window_top = (screen.height/2) - (w/2);
		window_left = (screen.width/2) - (h/2);
		window.open(file,winName,'width='+w+',height='+h+',left='+window_left+',top='+window_top);
	} else {
		window.open(file,winName);
	}
}

function classChange(ele, cn)
{
	ele.className = cn;
}

function form_confirm(msg, formObject)
{
	response = confirm(msg);
	if (response == true) {
		formObject.submit;
	} else {
		return false;
	}
}

function gotopage(urlstr)
{
	window.location = docroot+urlstr;
}

function remove(urlstr)
{
	response = confirm("Are you sure you want to remove this item?");

	if(response==true)
	window.location = docroot+urlstr;
}
var deletebtn = remove;

function jcms_confirm(urlStr,msgStr)
{
	response = confirm(msgStr);
	if(response==true)
	window.location = docroot+urlStr;
}

function sendBulk(urlstr)
{
	response = confirm("Send bulk mailout? Are you really sure?");

	if(response==true)
	window.location = docroot+urlstr;
}

function showHide(container) {
	con = d.getElementById(container);
	if( con != null ) {
		con.style.display = (con.style.display == 'none' || con.style.display == '') ? 'block' : 'none';
		conCheck = d.getElementById(container+'Check');
		if( conCheck != null ) {
			conCheck.checked = (conCheck.checked) ? false : true;
		}
	}
}

function showHideRadio(container, show)
{
	con = d.getElementById(container);
	
	if (show) {
		con.style.display = 'block';
	} else {
		con.style.display = 'none';
	}
}

function showBannerType( type )
{
    $( '#links' ).hide();
    $( '#text' ).hide();
    $( '#wys_text' ).hide();
    $( '#text_image' ).hide();
	$( '#image' ).hide();
	$( '#wys_image' ).hide();
	$( '#flash' ).hide();
	$( '#flash_video' ).hide();
	$( '#adsense' ).hide();
	$( '#custom' ).hide();
	$( '#multi' ).hide();
	
	if( type ) {
	   $( '#' + type ).show();
	}
}

function changeViewState(id) {
	if(document.getElementById(id+'_children').style.display == 'none') {
		document.getElementById(id+'_expand').src = '../../images/minus.gif';
		document.getElementById(id+'_folder').src = '../../images/folder_open.gif';
		document.getElementById(id+'_children').style.display = 'block';
		document.getElementById(id+'_text').innerHTML = '<a href="#" onclick="changeViewState('+id+')">close</a>';
		alterInCookie(id+'_children', 'add');
	}
	else {
		document.getElementById(id+'_expand').src = '../../images/plus.gif';
		document.getElementById(id+'_folder').src = '../../images/folder.gif';
		document.getElementById(id+'_children').style.display = 'none';
		document.getElementById(id+'_text').innerHTML = '<a href="#" onclick="changeViewState('+id+')">open</a>';
		alterInCookie(id+'_children', 'remove');
	}
}

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
	begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

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

function TextAreaAutoGrow(obj) {
    var pad = 15;
    var computeDivId = 'compute_'+obj.id;
    var computeDiv;

    if(!(computeDiv=document.getElementById(computeDivId))) {
        computeDiv=document.createElement('div');
        computeDiv.id=computeDivId;
        computeDiv.style.position="absolute";
        computeDiv.style.left="-10000px";
        computeDiv.style.top="-10000px";
        computeDiv.fontSize=get_style(obj,'fontSize')+'px';
        computeDiv.style.width=parseInt(obj.clientWidth-8)+'px';
        obj.setAttribute('startHeight',obj.clientHeight);
        obj.parentNode.appendChild(computeDiv);
    }
    var clientHeight=obj.clientHeight;
    computeDiv.innerHTML=htmlspecialchars(obj.value).replace(/[\n]/g,'<br />&nbsp;');
    var shadowHeight=computeDiv.clientHeight;
    var to_height;
    var startHeight=obj.getAttribute('startHeight');
    if(shadowHeight<startHeight) {
        to_height=startHeight;
    }
    else {
        to_height=shadowHeight+pad;
    }
    if(to_height&&to_height!=clientHeight) {
        obj.style.height=to_height+'px';
    }
}


function get_style(object,prop){
        function hyphenate(prop){
            return prop.replace(/[A-Z]/g, function(match){
                    return'-'+match.toLowerCase();
                }
            );
        }
    if(window.getComputedStyle){
        return window.getComputedStyle(object,null).getPropertyValue(hyphenate(prop));
    }
    if(document.defaultView&&document.defaultView.getComputedStyle){
        var computedStyle=document.defaultView.getComputedStyle(object,null);
        if(computedStyle)
            return computedStyle.getPropertyValue(hyphenate(prop));
        if(prop=="display")
            return"none";
        alert("Can't retrieve requested style " + prop + " due to a bug in Safari");
    }
    if(object.currentStyle){
        return object.currentStyle[prop];
    }
    return object.style[prop];
}

function htmlspecialchars(text) {
    if(typeof(text)=='undefined'||!text.toString) 
        return'';
    if(text===false) 
        return'0';
    else if(text===true)
            return'1';
    return text.toString().replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/'/g,'&#039;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}


function getElementsByClassName(className, tag, elm){
    var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
    var tag = tag || "*";
    var elm = elm || document;
    var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for(var i=0; i<length; i++){
        current = elements[i];
        if(testClass.test(current.className)){
            returnElements.push(current);
        }
    }
    return returnElements;
}

function ExpandAutoGrowTAs() {
    var x = getElementsByClassName("aform_textarea_autogrow");
    for(var i = 0; i < x.length; i++) {
        TextAreaAutoGrow(x);
    }
}


/* 
 * Based on:
 * http://parentnode.org/javascript/working-with-the-cursor-position/
 */
function insertAtCaret(obj, text) { 
    if(document.selection) { 
        // IE
        obj.focus(); 
        var range = document.selection.createRange(); 
        if(range.parentElement() != obj) { 
                return false; 
        } 
        var orig = obj.value.replace(/rn/g, "n"); 
        range.text = text; 
        var actual = tmp = obj.value.replace(/rn/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 || obj.selectionStart == 0) { 
        // Firefox/Mozilla etc...
        var start = obj.selectionStart; 
        var end   = obj.selectionEnd; 
        obj.value = obj.value.substr(0, start) 
            + text 
            + obj.value.substr(end, obj.value.length); 
        setCaretTo(obj, end + text.length);
    } else { 
        // Fallback
        obj.value += text;
        obj.focus();
    } 
}

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);
    }
}

/*
 * When we wish to pass from back-end to a front-end section
 * that requires authorization, we can add this function
 * to the onclick of the request
 * and it will generate and pass the auth token for us
 */
function pass_with_auth_token(a) {
    var authEndPoint = HTML_ROOT + '/login/generateAuthToken.php?q=';
    var target = a.href;

    if(target.match('generateAuthToken.php')) return;
    
    var str = authEndPoint + urlencode(target);
    a.href = str;
}

function urlencode(str) {
    str = escape(str);
    str = str.replace('+', '%2B');
    str = str.replace('%20', '+');
    str = str.replace('*', '%2A');
    str = str.replace('/', '%2F');
    str = str.replace('@', '%40');
    return str;
}

function urldecode(str) {
    str = str.replace('+', ' ');
    str = unescape(str);
    return str;
}

function clone_array( arr )
{
    var a = new Array();
    for( var property in arr )
    {
        a[property] = typeof( arr[property] ) == 'object' ? clone_array( arr[property] ) : arr[property];
    }
    return a;
}

function clone_object( obj )
{
    var o = new Object();
    for( var property in obj )
    {
        o[property] = typeof( obj[property] ) == 'object' ? clone_object( obj[property] ) : obj[property];
    }
    return o;
}