﻿// JScript File

var cWidth;
var cHeight;
var hdrHeight;
var divMainTop;

    mainTabOf = new Image();
	mainTabOf.src = "images/main_off.jpg"
	mainTabOn = new Image();
	mainTabOn.src = "images/main_on.jpg"
	loginTabOf = new Image();
	loginTabOf.src = "images/login_off.jpg"
	loginTabOn = new Image();
	loginTabOn.src = "images/login_on.jpg"

function initMain(){
    getPropertyList();
    getPropertyObjects();
    divMainTop = $("divMain").offsetTop;
    positionDivs();
    initMap();
    Effect.Fade("introScreen", {duration:.5}); 
    getPropertyEdit(0);
    getUserList();
}

function getNavTabs(){
    var url = "applications/login/tabMenu.jsp";
    new Ajax.Updater($("navtabs"),url);
}

function sendPassword(){
    fLogin = $("frmLogin");
    var inEmail = fLogin.username.value;
    var url = "applications/login/sendPassword.jsp?email=" + inEmail;
    var result = "";
    new Ajax.Request(url,{
        onComplete: function(t){
            result = t.responseText;
            alert("Your password has been sent to your e-mail address.")
        }
    });
}

function sendContactRequest(){
    fLogin = $("frmRequestContact");
    //var inEmail = fLogin.username.value;
    var url = "applications/login/processRequestContact.jsp";
    //var result = "";
    new Ajax.Request(url,{
        onComplete: function(t){
            result = t.responseText;
            alert("Your request has been forwarded to the Charles County EDD.")
        }
    });
}

function init(){

    $("introScreen").style.height = document.viewport.getHeight()+"px";
    getHdrMenu();
    getNavTabs();
    var url = "applications/login/navContainer.jsp"
    new Ajax.Request(url,{
        onComplete: function(t){
            $("divNavContainer").innerHTML = t.responseText;
            getSearchInt();
            
        }
    })
}

function getSearchInt(){
    var url = "applications/property/searchInt.jsp"
    new Ajax.Request(url,{
        onComplete: function(t){
            $("divSrchContent").innerHTML = t.responseText;
            initMain();
        }
    });
}

function getHdrMenu(){
    var url = "applications/login/hdrMenu.jsp"
    new Ajax.Updater($("divHdrMenu"),url);
}

function positionDivs(){
   // var oWin = document.viewport.getDimensions();

    cWidth = document.viewport.getWidth();
    cHeight = document.viewport.getHeight();
    
    hdrHeight = $("divHdr").getHeight();
    $("divMain").style.width = (cWidth * .67) + "px";
    divMainTop = $("divMain").offsetTop;
    divmainWidth = $("divMain").getWidth();
    mapHdrHeight = $("divMapHdr").getHeight();
    $("divMapHdr").style.width = ($("divMain").getWidth() - 5)+"px";

    $("divNav").style.top = (hdrHeight) + "px";
    $("divNav").style.left = ($("divMain").getWidth()+10) + "px";
    $("divMapTools").style.top = ($("divMain").offsetTop + 10) + "px";
    $("divMap").style.height = (cHeight * .50) + "px";
    $("divMap").style.width = (divmainWidth) + "px";
    $("divMapEvents").style.height = $("divMap").style.height;
    $("divMapEvents").style.width = ($("divMap").getWidth()-4)+ "px";
    $("divMapEvents").style.top = ($("divMain").offsetTop + 2)+ "px";
    $("divMapEvents").style.left = ($("divMap").offsetLeft + 2) + "px";
   
    $('divLoadingMap').style.height = $('divMapEvents').style.height;
    $('divLoadingMap').style.width = $('divMapEvents').style.width;
    $('divLoadingMap').style.top = ($('divMapEvents').offsetTop+2)+"px";
    $('divLoadingMap').style.left = ($('divMapEvents').offsetLeft)+"px";

    $('divObjects').style.height = $('divMapEvents').style.height;
    $('divObjects').style.width = $('divMapEvents').style.width;
    $('divObjects').style.top = ($('divMapEvents').offsetTop-($("divMain").offsetTop + 2))+"px";
    $('divObjects').style.left = ($('divMapEvents').offsetLeft-($("divMap").offsetLeft + 2))+"px";
    
    $("divNav").style.width = (cWidth - $("divMain").getWidth()-30) + "px";
    $("divNav").style.height = (cHeight - $("divHdr").getHeight() - 15) + "px";
    $("divInfoViewer").style.top = ($("divMap").getHeight()+ $("divMain").offsetTop + 5) + "px";
    $("divInfoViewer").style.width = (divmainWidth + 7) + "px";
    
    $("divInfoList").style.height = (cHeight - $("divHdr").getHeight()- $("divInfoHdr").getHeight()-$("divMap").getHeight() - 40-20) + "px";
    // to be removed
    $("gitMapImage").width = $("divMapEvents").offsetWidth;
    $("gitMapImage").height = $("divMapEvents").offsetHeight;
    //$("gitMapRef").style.left = $("gitMapImage").style.width - 100;
    
    $("divPropViewContent").style.height = (cHeight - hdrHeight - 40);
    if($("divPropEditorContent")){
        $("divPropEditorContent").style.height = (cHeight * .82);
    }
    $("divSrchContent").style.height = (cHeight * .82);
    mapProperties();
}

var bMapIntVisible = true;

function toggleMapInt(){
    if(bMapIntVisible == false){

        $("divMapTools").style.display = "none";
        //Effect.BlindUp("divMain", {duration:.3});
        $("divInfoViewer").style.top = (divMainTop + 3) + "px";
        $("imgMapToggle").src = "images/bartoggleUp.gif";
        $("divInfoList").style.height = (cHeight - $("divHdr").getHeight()- $("divInfoHdr").getHeight() - 40) + "px";
    }
    else{
        $("divMapTools").style.display = "block";
        //Effect.BlindDown("divMapStuff", {duration:.3});
        $("divInfoViewer").style.top = ($("divMap").getHeight()+ $("divMain").offsetTop + 5) + "px";
        $("imgMapToggle").src = "images/bartoggleDn.gif"
        $("divInfoList").style.height = (cHeight - $("divHdr").getHeight()- $("divInfoHdr").getHeight()-$("divMap").getHeight() - 40) + "px";
    }
}

var opentab = "divSrchContent"

function selectTab(tab,cont){
	opentab = cont;
    var tabObj = $("tabset").immediateDescendants();
    tabObj.each(function(obj, index) {
        if(obj.className != 'tabspacer'){
            
            if($(tab).id == obj.id){
                obj.className = "tabon";
            }
            else{
                obj.className = "taboff";
            }
        }
    });
    
    var contObj = $("divNavContainer").immediateDescendants();
    contObj.each(function(obj, index) {
            if($(cont).id == obj.id){
                obj.show()
            }
            else{
                obj.hide();
            }

    });
    
}



function getLogin(){
    var url = "applications/login/frmLogin.jsp"
    new Ajax.Updater($("popUpContent"),url);
    doPopUp();
}

function getContactRequest(){
    var url = "applications/login/requestContact.jsp"
    new Ajax.Updater($("popUpContent"),url);
    doPopUp();
}

function getCreateUser(){
    var url = "applications/login/createUser.jsp"
    new Ajax.Updater($("popUpContent"),url);
    doPopUp();
    
}

function getChangePassword(){
    var url = "applications/login/changePassword.jsp"
    new Ajax.Updater($("popUpContent"),url);
    doPopUp();
}

function doLoginFormSubmit(){

	// this will automatically submit the form via ajax to whatever file is in the forms
	// action attribute.  The text results from the action page will be returned
	// in the responseText property.  So you can notify the person..and kick off other
	// functions i.e. refresh the property list or something.
        //var res;
	$("frmLogin").request({
		onComplete: function(t){
			//$("returnLoginResult").innerHTML = t.responseText;
                        alert(t.responseText);
			getSearchInt();
                        closePopUp();
			getHdrMenu();
                        getNavTabs();
			
		}
	});
}

// new submit function specific to the search form because it needs to update the propertyObjects oncomplete.
function doSearchFormSubmit(){

	$("frmSrchType").request({
		onComplete: function(t){
			$("divInfoList").innerHTML = t.responseText;
			getPropertyObjects(); 
                        //getSearchInt();
		}
	});
}

// new submit function specific to the edit form because it needs to update the list/objects oncomplete.
function doEditFormSubmit(){

	$("frmEdit").request({
		onComplete: function(t){
			$("divEditResult").innerHTML = t.responseText;
			doSearchFormSubmit();       
		}
	});
}

function doFormSubmit(formName, returnDiv, alertMsg){

	// this will automatically submit the form via ajax to whatever file is in the forms
	// action attribute.  The text results from the action page will be returned
	// in the responseText property.  So you can notify the person..and kick off other
	// functions i.e. refresh the property list or something.

	$(formName).request({
		onComplete: function(t){
			$(returnDiv).innerHTML = t.responseText;
			getPropertyObjects(); 
            if (alertMsg != "")
            {
              alert(alertMsg);
            }
		}
	});
}


function submitContactRequest(formName){
    var x;
    x = submitForm(formName);
    alert("here now: " + x);
    //alert("Your request as been forwarded to the Charles County EDD.");
}


function submitForm(formName){

	// this will automatically submit the form via ajax to whatever file is in the forms
	// action attribute.  The text results from the action page will be returned
	// in the responseText property.  So you can notify the person..and kick off other
	// functions i.e. refresh the property list or something.
        var r;

	$(formName).request({
		onComplete: function(t){
			r = t.responseText;
		}
	});
        //var f = "" + r;
        alert("here: " + r);
        return r;
}

function getProperty(propid){
    
    var url = "applications/property/propertyDetail.jsp?propID=" + propid;
    new Ajax.Updater($("divPropViewContent"),url);
    
    if($("divPropEditorContent")){
        getPropertyEdit(propid)   
        if(opentab == "divPropEditorContent"){
           selectTab('tabEditor',opentab);
        }
        else{
           selectTab('tabView','divPropViewContent');
        }
    }
}

function getUserList(){
    var url = "applications/login/userList.jsp";
    new Ajax.Updater($("divUserList"),url,{asynchronous: false});    
}

function getUserList2(){
    rndm = Math.round(Math.random() * 1000000);
    var url = "applications/login/userList.jsp?rnd=" + rndm;
    new Ajax.Request(url,{
        onComplete: function(t){
            $("divUserList").innerHTML = t.responseText;
            //initMain();
        }
    });
}

function getPropertyEdit(propid){
  if($("divPropEditorContent")){
          var url = "applications/editor/editorForm.jsp?cmd=select&propID=" + propid;
          new Ajax.Updater($("divPropEditorContent"),url);
  }
}

function getPropertyDelete(propid){
  var answer = confirm("Are you sure you want to delete this property?");
  if (answer){
  if($("divPropEditorContent")){
          var url = "applications/editor/editorForm.jsp?cmd=delete&propID=" + propid;
          new Ajax.Updater($("divPropEditorContent"),url);
          doSearchFormSubmit();
  }
  }
}

function getUserDelete(username){
  var answer = confirm("Are you sure you want to delete this user?");
  if (answer){
  if($("divUserEditorMessages")){
          var url = "applications/login/processDeleteUser.jsp?username=" + username;
          new Ajax.Updater($("divUserEditorMessages"),url);
  }
  }
  getUserList();
  getUserList();
}

function getPropertyList(){
    var url = "applications/property/propertyList.jsp";
    new Ajax.Updater($("divInfoList"),url,{asynchronous: false});
}

function getPropertyObjects(){
    var now = new Date();
    var url = "applications/property/propertyObjects.jsp?time=" + now.toString();
    new Ajax.Request(url,{asynchronous: false,
        onComplete:function(t){
            $("divObjects").innerHTML = t.responseText;
           positionObjects();
        }
        });
}

function createUser()
{
    var s;
    s = submitForm("frmCreateUser");
    alert(s);
}


function doPopUp(inText){
    pUp = $("popUp");
    pModal = $("modalPopUp");
    pModal.style.width = cWidth+"px";
    pModal.style.height = cHeight + "px";
    
    //$("popUpContent").innerHTML = inText;
    pUp.style.left = (cWidth/2 - pUp.getWidth()/2) + "px";
    pUp.style.top = (cHeight/2 - pUp.getHeight()/2) + "px";
    pModal.style.display = "block";
    pUp.style.display = "block";
}

function closePopUp(){
    pModal.style.display = "none";
    pUp.style.display = "none";
}

function toggleCheckbox(obj){
    var chk = obj.checked;
    obj.value = "off";
    if (chk){
        obj.value = "on";
    }
}





