// common functions used by the menu

// function that should replace the cmnCmd function from cmn.js (because we use a different form to submit now
function submitLoginForm(sCmd) {
	var frmLoginForm = document.forms.frmLogin;
	if (frmLoginForm && frmLoginForm.command) {
		self.focus();
		frmLoginForm.command.value = sCmd;
		frmLoginForm.target = "_top";
		frmLoginForm.submit();
	}
}

// login function that calls directly the submitLoginForm function
function doLogin() {
	submitLoginForm('login');
}

// logout function will log the user out and redirect user to startpage (index.jsp redirects to jsp/homepage.jsp)
function doLogout() {
	deleteSessionCookies();
	submitLoginForm('logout');
	//document.location = jsWepAppContext+"/index.jsp";
}

function validateBlankSpace(frm) {
	if(hasBlankSpace(frm.sUsername.value) || hasBlankSpace(frm.sPassword1.value) || hasBlankSpace(frm.sPassword2.value)) {
		return true;
	} else {
		return false;
	}
}
// called by doSignin to verify the length of the username/password entered
function validateUserLen(frm,length) {
	if (frm.sUsername.value.length >= length && frm.sPassword1.value.length >= length
	&& frm.sPassword2.value.length >= length) {
		return true;
	} else {
		return false;
	}
}

// functions to show/hide the login form AND the new use registration form
function switchVisibility(id1,id2){
	var div1 = document.getElementById(id1);
	var div2 = document.getElementById(id2);
	var div3 = document.getElementById("logErrMsgText");
	if (div1 != null && div2 !=null) {
		div2.style.display = 'block'; div2.style.visibility = 'visible';
		div1.style.display = 'none';div1.style.visibility = 'hidden';
		if (div3 != null) {
			div3.style.display = 'none';div3.style.visibility = 'hidden';
		}
	}
}
function showLoginDiv() {
	switchVisibility('SignInDiv','LoginDiv');
}
function showSignInDiv() {
	switchVisibility('LoginDiv','SignInDiv');
}

//********************************************************************************************
// manage channels
function manageChannels() {
	var oFrm =  document.forms.fSubmit;
	if (oFrm != null) {
		var OldAction = oFrm.action;
		oFrm.action = jsWepAppContext+"/ChMmgt.do?act=myChannels";
		oFrm.submit();
		oFrm.action = OldAction;
		return false;
	}
	return true;
}

function closeNW() {
	if (nw) nw.close();
}

function manageMySearches() {
	var s = jsWepAppContext + "/ptk?command=mysearches&act=showopen&dtx="+((new Date()).getTime());
	document.location = s;
}

function manageMyMaps() {
	var s = jsWepAppContext + "/ptk?command=mymaps&act=showopen&dtx="+((new Date()).getTime());
	document.location = s;
}

function openMap() {
	cmnViewMap("<%=_baseMapURL%>", "<%=_baseMapSvc%>", "image", true); // in cmn.js
}

function openSavedSearch(pk,name) {
	var oFrm = document.forms.fSubmit;
	if (oFrm != null) {
		oFrm.action = jsWepAppContext+"/DiscoveryServlet";
		oFrm.pk.value = pk;
		oFrm.name.value = name;
		oFrm.notify.value = 0;
		oFrm.xmlsearch.value = "";
		oFrm.command.value = "mysearches";
		oFrm.submit();
	}
}

function openMarketplace() {
	var oFrm = document.forms.fSubmit;
	if (oFrm != null) {
		oFrm.action = jsWepAppContext+"/DiscoveryServlet";
		cmnCmd('marketplace');
	}
}

function openMySavedMap(pk,name,linkid) {
	openSavedMap(linkid); // in cmn.js
}

// set the focus to the element specified by id
// loaded on page body's onload event
function setFocus(id) {
	var el = document.getElementById(id);
	if (el) {
		el.focus();
	}
}

function hasBlankSpace(s) {
     var i;

    // Search through string's characters one by one
    // until we find a blank space.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        // Check for blank spaces
        if (c == ' ' )
         return true;
    }

    // no blank space
    return false;
}


