/* This function is used to change the style class of an element */
try{
	jQuery.noConflict();
} catch (e) {}

function swapClass(obj, newStyle) {
    obj.className = newStyle;
}

function isUndefined(value) {   
    return value == undefined; 
}

function isNumeric(value){
	//if(isNaN(value))
		//return false;
	return (value - 0) == value && value.length > 0;
}

function checkAll(theForm) { // check all the checkboxes in the list
  for (var i=0;i<theForm.elements.length;i++) {
    var e = theForm.elements[i];
        var eName = e.name;
        if (eName != 'allbox' && 
            (e.type.indexOf("checkbox") == 0)) {
            e.checked = theForm.allbox.checked;        
        }
    } 
}

/* Function to clear a form of all it's values */
function clearForm(frmObj) {
    for (var i = 0; i < frmObj.length; i++) {
        var element = frmObj.elements[i];
        if(element.type.indexOf("text") == 0 || 
                element.type.indexOf("password") == 0) {
                    element.value="";
        } else if (element.type.indexOf("radio") == 0) {
            element.checked=false;
        } else if (element.type.indexOf("checkbox") == 0) {
            element.checked = false;
        } else if (element.type.indexOf("select") == 0) {
            for(var j = 0; j < element.length ; j++) {
                element.options[j].selected=false;
            }
            element.options[0].selected=true;
        }
    } 
}

/* Function to get a form's values in a string */
function getFormAsString(frmObj) {
    var query = "";
    for (var i = 0; i < frmObj.length; i++) {
        var element = frmObj.elements[i];
        if (element.type.indexOf("checkbox") == 0 || 
            element.type.indexOf("radio") == 0) { 
            if (element.checked) {
                query += element.name + '=' + escape(element.value) + "&";
            }
        } else if (element.type.indexOf("select") == 0) {
            for (var j = 0; j < element.length ; j++) {
                if (element.options[j].selected) {
                    query += element.name + '=' + escape(element.value) + "&";
                }
            }
        } else {
            query += element.name + '=' 
                  + escape(element.value) + "&"; 
        }
    } 
    return query;
}

/* Function to hide form elements that show through
   the search form when it is visible */
function toggleForm(frmObj, iState) // 1 visible, 0 hidden 
{
    for(var i = 0; i < frmObj.length; i++) {
        if (frmObj.elements[i].type.indexOf("select") == 0 || frmObj.elements[i].type.indexOf("checkbox") == 0) {
            frmObj.elements[i].style.visibility = iState ? "visible" : "hidden";
        }
    } 
}

/* Helper function for re-ordering options in a select */
function opt(txt,val,sel) {
    this.txt=txt;
    this.val=val;
    this.sel=sel;
}

/* Function for re-ordering <option>'s in a <select> */
function move(list,to) {     
    var total=list.options.length;
    index = list.selectedIndex;
    if (index == -1) return false;
    if (to == +1 && index == total-1) return false;
    if (to == -1 && index == 0) return false;
    to = index+to;
    var opts = new Array();
    for (var i=0; i<total; i++) {
        opts[i]=new opt(list.options[i].text,list.options[i].value,list.options[i].selected);
    }
    tempOpt = opts[to];
    opts[to] = opts[index];
    opts[index] = tempOpt;
    list.options.length=0; // clear
    
    for (i=0;i<opts.length;i++) {
        list.options[i] = new Option(opts[i].txt,opts[i].val);
        list.options[i].selected = opts[i].sel;
    }
    
    list.focus();
} 

/*  This function is to select all options in a multi-valued <select> */
function selectAll(elementId) {
    var element = document.getElementById(elementId);
    len = element.length;
    if (len != 0) {
        for (var i = 0; i < len; i++) {
            element.options[i].selected = true;
        }
    }
}

/* This function is used to select a checkbox by passing
 * in the checkbox id
 */
function toggleChoice(elementId) {
    var element = document.getElementById(elementId);
    if (element.checked) {
        element.checked = false;
    } else {
        element.checked = true;
    }
}

/* This function is used to select a radio button by passing
 * in the radio button id and index you want to select
 */
function toggleRadio(elementId, index) {
    var element = document.getElementsByName(elementId)[index];
    element.checked = true;
}

/* This function is used to open a pop-up window */
function openWindow(url, winTitle, winParams) {
    winName = window.open(url, winTitle, winParams);
    winName.focus();
}


/* This function is to open search results in a pop-up window */
function openSearch(url, winTitle) {
    var screenWidth = parseInt(screen.availWidth);
    var screenHeight = parseInt(screen.availHeight);

    var winParams = "width=" + screenWidth + ",height=" + screenHeight;
        winParams += ",left=0,top=0,toolbar,scrollbars,resizable,status=yes";

    openWindow(url, winTitle, winParams);
}

/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
    var prefix = name + "=";
    var start = document.cookie.indexOf(prefix);

    if (start==-1) {
        return null;
    }
    
    var end = document.cookie.indexOf(";", start+prefix.length);
    if (end==-1) {
        end=document.cookie.length;
    }

    var value=document.cookie.substring(start+prefix.length, end);
    return unescape(value);
}

/* This function is used to delete cookies */
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";
  }
}

// This function is for stripping leading and trailing spaces
String.prototype.trim = function () {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

// This function is used by the login screen to validate user/pass
// are entered. 
function validateRequired(form) {                                    
    var bValid = true;
    var focusField = null;
    var i = 0;                                                                                          
    var fields = new Array();                                                                           
    oRequired = new required();                                                                         
                                                                                                        
    for (x in oRequired) {                                                                              
        if ((form[oRequired[x][0]].type == 'text' || form[oRequired[x][0]].type == 'textarea' || form[oRequired[x][0]].type == 'select-one' || form[oRequired[x][0]].type == 'radio' || form[oRequired[x][0]].type == 'password') && form[oRequired[x][0]].value == '') {
           if (i == 0)
              focusField = form[oRequired[x][0]]; 
              
           fields[i++] = oRequired[x][1];
            
           bValid = false;                                                                             
        }                                                                                               
    }                                                                                                   
                                                                                                       
    if (fields.length > 0) {
       focusField.focus();
       alert(fields.join('\n'));                                                                      
    }                                                                                                   
                                                                                                       
    return bValid;                                                                                      
}

// This function is a generic function to create form elements
function createFormElement(element, type, name, id, value, parent) {
    var e = document.createElement(element);
    e.setAttribute("name", name);
    e.setAttribute("type", type);
    e.setAttribute("id", id);
    e.setAttribute("value", value);
    parent.appendChild(e);
}

function confirmDelete(obj) {   
    var msg = "Are you sure you want to delete this " + obj + "?";
    ans = confirm(msg);
    if (ans) {
        return true;
    } else {
        return false;
    }
}

function highlightTableRows(tableId) {
    var previousClass = null;
    var table = document.getElementById(tableId); 
    var startRow = 0;
    // workaround for Tapestry not using thead
    if (!table.getElementsByTagName("thead")[0]) {
	    startRow = 1;
    }
    var tbody = table.getElementsByTagName("tbody")[0];
    var rows = tbody.getElementsByTagName("tr");
    // add event handlers so rows light up and are clickable
    for (var i=startRow; i < rows.length; i++) {
        rows[i].onmouseover = function() { previousClass=this.className;this.className+=' over';};
        rows[i].onmouseout = function() { this.className=previousClass;};
        rows[i].onclick = function() {
            var cell = this.getElementsByTagName("td")[0];
            var link = cell.getElementsByTagName("a")[0];
            if (link.onclick) {
                call = link.getAttribute("onclick");
                if (call.indexOf("return ") == 0) {
                    call = call.substring(7);
                } 
                // this will not work for links with onclick handlers that return false
                eval(call);
            } else {
                location.href = link.getAttribute("href");
            }
            this.style.cursor="wait";
            return false;
        };
    }
}

function highlightFormElements() {
    // add input box highlighting
    addFocusHandlers(document.getElementsByTagName("input"));
    addFocusHandlers(document.getElementsByTagName("textarea"));
}

function addFocusHandlers(elements) {
    for (var i=0; i < elements.length; i++) {
        if (elements[i].type != "button" && elements[i].type != "submit" &&
            elements[i].type != "reset" && elements[i].type != "checkbox" && elements[i].type != "radio") {
            if (!elements[i].getAttribute('readonly') && !elements[i].getAttribute('disabled')) {
                elements[i].onfocus=function() {this.style.backgroundColor='#ffd';this.select();};
                elements[i].onmouseover=function() {this.style.backgroundColor='#ffd';};
                elements[i].onblur=function() {this.style.backgroundColor='';};
                elements[i].onmouseout=function() {this.style.backgroundColor='';};
            }
        }
    }
}

function radio(clicked){
    var form = clicked.form;
    var checkboxes = form.elements[clicked.name];
    if (!clicked.checked || !checkboxes.length) {
        clicked.parentNode.parentNode.className="";
        return false;
    }

    for (var i=0; i<checkboxes.length; i++) {
        if (checkboxes[i] != clicked) {
            checkboxes[i].checked=false;
            checkboxes[i].parentNode.parentNode.className="";
        }
    }

    // highlight the row    
    clicked.parentNode.parentNode.className="over";
}

window.onload = function() {
	
    highlightFormElements();
    try {
	    if ($('successMessages')) {
	        new Effect.Highlight('successMessages');
	        // causes webtest exception on OS X : http://lists.canoo.com/pipermail/webtest/2006q1/005214.html
	        // window.setTimeout("Effect.DropOut('successMessages')", 3000);
	    }
	    if ($('errorMessages')) {
	        new Effect.Highlight('errorMessages');
	    }
    } catch (e){}
    
    /* Initialize menus for IE */
    
    if ($("primary-nav")) {
        var navItems = $("primary-nav").getElementsByTagName("li");
    
        for (var i=0; i<navItems.length; i++) {
            if (navItems[i].className == "menubar") {
                navItems[i].onmouseover=function() { this.className += " over"; };
                navItems[i].onmouseout=function() { this.className = "menubar"; };
            }
        }
    }
};

// Show the document's title on the status bar
window.defaultStatus=document.title;

function changeCurrentUserPassword(){
	UserManager.getCurrentUserDWR(function(user) {
		userDWR = user;
		var message = 'Password:<br><input type="password" id="password" value="'+user.password+'"/><br>';
		message += 'Password Hint:<br><input id="passwordHint" value="'+user.passwordHint+'"/><br>';
	
		message += '<br><br>'+createButton("done3",Labels.done);
		message += createButton("cancel3",Labels.cancel,"hideDialog();");
		showDialog('Update Password for '+user.username,message,220,220);
		$('b_done3').onclick = function(){
			userDWR.password=$('password').value;
			userDWR.passwordHint=$('passwordHint').value;
			UserManager.updateUserDWR(userDWR,hideDialog);
		};
	});
	
}

function updateUserDWR(userDWR){
	UserManager.updateUserDWR(userDWR,function(result){
		if(result=='OK')
			hideDialog();
		else alert(result);
	});
}

var __isIE =  navigator.appVersion.match(/MSIE/);
var __userAgent = navigator.userAgent;
var __isFireFox = __userAgent.match(/firefox/i);
var __isFireFoxOld = __isFireFox && 
   (__userAgent.match(/firefox\/2./i) || __userAgent.match(/firefox\/1./i));
var __isFireFoxNew = __isFireFox && !__isFireFoxOld;

function __parseBorderWidth(width) {
    var res = 0;
    if (typeof(width) == "string" && width != null 
                && width != "" ) {
        var p = width.indexOf("px");
        if (p >= 0) {
            res = parseInt(width.substring(0, p));
        }
        else {
             //do not know how to calculate other 
             //values (such as 0.5em or 0.1cm) correctly now
             //so just set the width to 1 pixel
            res = 1; 
        }
    }
    return res;
}

//returns border width for some element
function __getBorderWidth(element) {
    var res = new Object();
    res.left = 0; res.top = 0; res.right = 0; res.bottom = 0;
    if (window.getComputedStyle) {
        //for Firefox
        var elStyle = window.getComputedStyle(element, null);
        res.left = parseInt(elStyle.borderLeftWidth.slice(0, -2));  
        res.top = parseInt(elStyle.borderTopWidth.slice(0, -2));  
        res.right = parseInt(elStyle.borderRightWidth.slice(0, -2));  
        res.bottom = parseInt(elStyle.borderBottomWidth.slice(0, -2));  
    }
    else {
        //for other browsers
        res.left = __parseBorderWidth(element.style.borderLeftWidth);
        res.top = __parseBorderWidth(element.style.borderTopWidth);
        res.right = __parseBorderWidth(element.style.borderRightWidth);
        res.bottom = __parseBorderWidth(element.style.borderBottomWidth);
    }
   
    return res;
}

//returns absolute position of some element within document
function getElementAbsolutePos(element) {
    var res = new Object();
    res.x = 0; res.y = 0;
    if (element !== null) {
        res.x = element.offsetLeft;
        res.y = element.offsetTop;
        
        var offsetParent = element.offsetParent;
        var parentNode = element.parentNode;
        var borderWidth = null;

        while (offsetParent != null) {
            res.x += offsetParent.offsetLeft;
            res.y += offsetParent.offsetTop;
            
            var parentTagName = offsetParent.tagName.toLowerCase();    

            if ((__isIE && parentTagName != "table") || 
                (__isFireFoxNew && parentTagName == "td")) {            
                borderWidth = __getBorderWidth(offsetParent);
                res.x += borderWidth.left;
                res.y += borderWidth.top;
            }
            
            if (offsetParent != document.body && 
                offsetParent != document.documentElement) {
                res.x -= offsetParent.scrollLeft;
                res.y -= offsetParent.scrollTop;
            }

            //next lines are necessary to support FireFox problem with offsetParent
               if (!__isIE) {
                while (offsetParent != parentNode && parentNode !== null) {
                    res.x -= parentNode.scrollLeft;
                    res.y -= parentNode.scrollTop;
                    
                    if (__isFireFoxOld) {
                        borderWidth = __getBorderWidth(parentNode);
                        res.x += borderWidth.left;
                        res.y += borderWidth.top;
                    }
                    parentNode = parentNode.parentNode;
                }    
            }

            parentNode = offsetParent.parentNode;
            offsetParent = offsetParent.offsetParent;
        }
    }
    return res;
}

function viewImage(id){
	Shadowbox.open({
		player:     'iframe',
        content:	ctx()+'/modal/viewimage.html?id='+id,
		height:     150,
		width:      150
	});
}	

function pageLoaded(){
	//UserFilterManager.savePageViewPerformance(window.location.pathname);
}

function showModalPage( page, width, height ){
	Shadowbox.open({
		player:     'iframe',
		content:	page,
		height:     height,
		width:      width
  });
}

function hasRole(role){
	for(var i=0;i<userRoles.length;i++)
		if(userRoles[i]==role)
			return true;
	return false;
}

function hasAnyRole(roles){
	for(var i=0;i<roles.length;i++)
		if(hasRole(roles[i]))
			return true;
	return false;
}

function editReferenceListValues(className,property) {
	Shadowbox.open({
        player:     'iframe',
        content:	ctx()+'/modal/referencelist.html?className='+className+'&property='+property,
        height:     billofmaterials_height,
        width:      billofmaterials_width,
        handleOversize:     'drag'
	});
}

function substituteLabelParams(labelText,param0,param1,param2,param3,param4,param5,param6,param7,param8,param9){
	for(var i=0;i<10;i++){
		eval("var param = ''+param"+i+";");
		if(param=='')
			break;
		labelText = labelText.replace('{'+i+'}',param);
	}
	return labelText;
}

var labelKey;
var labelItem;
var browsercontextmenu;

function editLabel(labelItem){
	// save the standard context menu event so it can be reinstated after closing this dialog
	browsercontextmenu = document.oncontextmenu;
	// now disable the standard contect menu
	document.oncontextmenu=function(){return false;};
	window.labelItem = labelItem;
	window.labelKey = labelItem.id.substring(4);
	SubscriberCustomLabelsManager.getCustomLabel(window.labelKey,function(labelText) {
		var message = Labels.labelText+':<br><input id="labelText"/>';
	
		message += '<br><br>'+createButton("submit3",Labels.done);
		message += createButton("cancel3",Labels.cancel);
		showDialog(substituteLabelParams(Labels.changeLabelTextFor,window.labelKey),message,220,220);
		$('labelText').value = labelText;
		$('b_submit3').onclick = function(){
			var text = $('labelText').value;
			// reinstate the standard browser context menu
			document.oncontextmenu = browsercontextmenu;
			window.labelItem.innerHTML = text;
			SubscriberCustomLabelsManager.updateCustomLabel(window.labelKey,text,function(){
				hideDialog();
				if(text=='')
					if(confirm('Re-load the page now?'))
						window.location.reload();
			});
		};
		$('b_cancel3').onclick = function(){
			// reinstate the standard browser context menu
			document.oncontextmenu = browsercontextmenu;
			hideDialog();
		};
	});
	
}

function showReleaseNotes(){
	//alert('future='+new Date().setYear(3000));
	if(getCookie('release_notes')==versionNo)
		return;
	
	UserFilterManager.getReleaseNotes(function(labelValue) {
		var message = '<textarea id="releaseNotes" style="width:380px;height:200px"></textarea>';
		message += '<br><br>'+createButton("done3",Labels.done,"hideDialog();");
		showDialog(Labels.releaseNotesTitle,message,400,330);
		$('releaseNotes').value = substituteLabelParams(Labels.releaseNotes,labelValue.value)+'\n\n'
			+labelValue.label;
		setCookie('release_notes',versionNo,new Date(2030,1,1));
	});
}

/*
 * find the screen coordinates of an element
 */
function getOffset( el ) {
    var x = 0;
    var y = 0;
    while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        x += el.offsetLeft - el.scrollLeft;
        y += el.offsetTop - el.scrollTop;
        el = el.parentNode;
    }
    return { top: y, left: x };
}

var Constants = new Object();

Constants.SELECT_OFF = 0;
Constants.SELECT_SINGLE = 1;
Constants.SELECT_POLYGON = 2;

function recalculateBookValues(){
	dwr.engine.setTimeout(300000);
	AssetManager.recalculateBookValues(function(){
		alert(Labels.finishedRecalculatingBookValues);
		dwr.engine.setTimeout(60000);
	});
}

function allocateOrgUnitsByGeometry(){
	dwr.engine.setTimeout(300000);
	SpatialLayerManager.allocateOrgUnitsByGeometry(function(result){
		alert(result?Labels.finishedAllocationOrgUnits:'allocation of org units is continuing in the background');
		dwr.engine.setTimeout(60000);
	});
}

function isModal(){
	return location.href.indexOf('/modal/')>=0;
}

function getMainWindow(){
	if(isModal())
		return parent;
	return window;
}
