/*** Inspection Calendar function that gets the needed month ***/
function getMonth(month, year){
    var towerid = $('towerid').value;
    var siteid = $('siteid').value;
    
    var url = '/inspectioncalendar/' + month+'/'+year+'/?towerid='+towerid+'&siteid='+siteid;
    window.location=url;
}

/*** Set the recurrance of event box to 0 days if the event is adhoc ***/
function setDayBox(etype){
    if(etype=="adhoc"){
        document.eventform.eventdays.value = 0;
    }
}


/** Get tower fixtures for selected tower **/
function getfixtures(){
    document.eventform.action.value = "towerselect"
    document.eventform.submit();
}

/** Adds comment for sites/towers/shelters/events ***/
function addComment(){
    document.commentform.action.value = 'addcomment';
    document.commentform.submit();
}


/*** Adds comments for sites/towers/shelters/events ***/
function removeComment(commentid){
    var answer = confirm("Are you sure you want to delete this comment?");
	if (answer){
        document.commentform.commentid.value=commentid;
        document.commentform.action.value='removecomment';
        document.commentform.submit();
    }
    
}

function removeDocument(documentid){
    var answer = confirm("Are you sure you want to delete this document?");
	if (answer){
        document.documentform.documentid.value = documentid;
        document.documentform.action.value = 'removedocument';        
        document.documentform.submit();
    }
}

function cancelfunc(gotopage){
    var answer = confirm("Warning, cancelling will result in you losing all unsaved information.\nAre you sure you want to cancel?");
	if (answer){
        window.location = gotopage
    }
}

function deleteContact(gotopage){
    var answer = confirm("Do you want to delete this contact?");
    if (answer){
        window.location = gotopage;
    }
}

/*** Sites Page functions ***/
function getFilterSitesList(){
    var filterval = $('filter').value;
    var page = 0
    changeSitesList(filterval, page);
}

function getSitesListPage(page){
    var filterval = $('filter').value;
    changeSitesList(filterval, page);
}

function changeSitesList(filterval, page){
    var url = '/sites/'+page+'/?filter='+$F('filter');
    var myAjax = new Ajax.Updater(
        'siteslist', 
        url, 
        {
            method:'get',
            asynchronous: true
        });
}

/*** Towers Page functions ***/
function getFilterTowersList(){
    var filterval = $('filter').value;
    var page = 0
    changeTowersList(filterval, page);
}

function getTowersListPage(page){
    var filterval = $('filter').value;
    changeTowersList(filterval, page);
}

function changeTowersList(filterval, page){
    var url = '/towers/'+page+'/?filter='+$F('filter');
    var myAjax = new Ajax.Updater(
        'towerslist', 
        url, 
        {
            method:'get',
            asynchronous: true
        });
}

/*** Shelters Page functions ***/
function getFilterShelterList(){
    var filterval = $('filter').value;
    var page = 0
    changeSheltersList(filterval, page);
}

function getShelterListPage(page){
    var filterval = $('filter').value;
    changeSheltersList(filterval, page);
}

function changeSheltersList(filterval, page){
    var url = '/shelters/'+page+'/?filter='+$F('filter');
    var myAjax = new Ajax.Updater(
        'shelterslist', 
        url, 
        {
            method:'get',
            asynchronous: true
        });
}

/** Contacts Page functions ***/
function getFilterContactsList(){
    var filterval = $('filter').value;
    var page = 0
    changeContactsList(filterval, page);
}

function getContactsListPage(page){
    var filterval = $('filter').value;
    changeContactsList(filterval, page);
}

function changeContactsList(filterval, page){
    var url = '/contacts/'+page+'/?filter='+$F('filter');
    var myAjax = new Ajax.Updater(
        'contactlist', 
        url, 
        {
            method:'get',
            asynchronous: true
        });
}

/*** show/hides display ***/
function show(area, arealink){
  if (document.getElementById(area).style.display == "none"){
    document.getElementById(area).style.display = "";
    document.getElementById(arealink).innerHTML = 'Hide';
  }
  else{
    document.getElementById(area).style.display = "none";
    document.getElementById(arealink).innerHTML = 'Show';
  }
  return false;
}

/*** Sort tower fixtures by heading on tower view page ***/
function sortfixtures(towerid, osb, sortbyval){
    var url = '/towerfixtsort/'+towerid+'/?sortby='+sortbyval+'&osb='+osb;
    var myAjax = new Ajax.Updater(
        'towerfixtureslist', 
        url, 
        {
            method:'get',
            asynchronous: true
        });

}

/*** Sort documents by heading on tower document view page ***/

function sortdocuments(towerid, osb, sortbyval, doctype){
    var url = '/towerdocsort/'+towerid+'/?sortby='+sortbyval+'&osb='+osb+'&doctype='+doctype;
    var myAjax = new Ajax.Updater(
        'docsarea',
        url,
        {
            method:'get',
            asynchronous: true
        });
}

/*** Main Search Functionality ***/
function search(){
    var searchterm = $('keywords').value;
}


/*** Event Status Function ***/
function setStatus(status){
    if(status == "complete"){
        var response = window.confirm("Are you sure you want to set the event status to complete?");
        if (response) {
            document.statusform.action.value = "complete";
            document.statusform.submit();
         }
    }
    else if(status == "cancel"){
        var response = window.confirm("Are you sure you want to cancel the event?");
        if (response) {
            document.statusform.action.value = "cancel";
            document.statusform.submit();
        }
    }
    else{
        var response = window.confirm("Are you sure you want to set the event to incomplete?");
        if (response) {
            document.statusform.action.value = "incomplete";
            document.statusform.submit();
        }
    }
    
}

/*** Show/Hide other fields for tower fixture ***/
function showExtraFields(){
    var typeid = $('stypeid').value;    
    //if the type is antenna
    if(typeid == 'Antenna'){
        document.getElementById('antenna').style.display = "";
        document.getElementById('camera').style.display = "none";
    }
    else if(typeid == 'Camera'){
        document.getElementById('camera').style.display = "";
        document.getElementById('antenna').style.display = "none";
    }
    else{
        document.getElementById('antenna').style.display = "none";
    }
}

/*** Show/Hide radio height***/
function displayRadioHeight(){
    var isAssociated = document.getElementById('id_IsAssociated').checked;        
    
    // if the checkbox is off, disable the input fields
    if(isAssociated == true){        
        
        document.getElementById('id_RadioTowerFixture').disabled = false;
    }
    else{        
        
        document.getElementById('id_RadioTowerFixture').disabled = true;
    }
}

/*** Dynamically change Polarity drop down based on Antenna Type ***/
function changePolarity(fixtureID){
    var antennaID = $('antennaType').value;    
    var url = '/changepolarity/?antennaID=' + antennaID;
    var myAjax = new Ajax.Updater('polarityList', url, { method: 'post', asynchronous: true});        
    url = '/changeradome/?antennaID=' + antennaID + '&fixtureID=' + fixtureID;
    var polarityAjax = new Ajax.Updater('radomeOption', url, { method: 'post', asynchronous: true});
}

/*** Get towers for selected shelter ***/
function getshelterselect(){
    document.relationform.action.value = "shelterselect"
    document.relationform.submit();
}

function checkkey(e){
    var unicode=e.charCode? e.charCode : e.keyCode
    if (unicode<48||unicode>57){
        return false;
    }
}

/*** convert degree to lat/long ***/
function converttodecimal(coords){
    
    if(coords == 'NS'){
        deg = document.getElementById('degns').value;
        min = document.getElementById('mmns').value;
        sec = document.getElementById('ssns').value;
        direction = document.getElementById('ns').value;
    }
    else{
        deg = document.getElementById('degew').value;
        min = document.getElementById('mmew').value;
        sec = document.getElementById('ssew').value;
        direction = document.getElementById('ew').value;
    }    
    if(deg != '' && min != '' && sec != ''){
        decimal = parseInt(deg) + parseFloat(min/60.0) + parseFloat(sec/3600.0);
        if(direction == 'W' || direction == 'S'){
            decimal = decimal * -1;
        }
        if(coords == 'NS'){
            document.getElementById('latitude').value = decimal;
        }
        else{
            document.getElementById('longitude').value = decimal;
        }
    }
    return true;
}


/*** convert lat/long to degree ***/
function converttodegree(coords){
    if(coords == 'latitude'){
        dec = document.getElementById('latitude').value;
        if(dec > 0 ){
            direction = 0;
            directval = 1;
        }
        else{
            direction = 1;
            directval = -1;
        }
    }
    else{
        dec = document.getElementById('longitude').value;
        if(dec > 0 ){
            direction = 0;
        }
        else{
            direction = 1;
        }
    }

    
        dec = Math.abs(dec)
        deg = Math.floor(dec);
        
        leftover = ( dec - deg ) * 60;
        min = parseInt(Math.floor( leftover));
        leftover = ( leftover - min ) * 60;
        sec = parseInt(Math.floor(leftover));
       
        if(coords == 'latitude'){
            document.getElementById('degns').value = deg;
            document.getElementById('mmns').value = min;
            document.getElementById('ssns').value = sec;
            document.getElementById('ns').options[direction].selected = true
        }
        else{
            document.getElementById('degew').value = deg;
            document.getElementById('mmew').value = min;
            document.getElementById('ssew').value = sec;
            document.getElementById('ew').options[direction].selected = true;
        }
    
    
}

function checklatlong(){
    
    latitude = document.getElementById('latitude').value;
    longitude = document.getElementById('longitude').value;
    if(latitude == ''){
        converttodecimal('NS');
    }
    if(longitude == ''){
        converttodecimal('EW');
    }
    
}

function addContact(towerID, contactType){    
    // this function will add the contact and dynamically add the new created contact to the list of contacts
    var url = '/createcontact/';    
    var pars = 'contactName=' + $F('id_contactName') + '&contactCompany=' + $F('id_contactCompany') + '&contactEmail=' + $F('id_contactEmail') + '&contactPhone=' + $F('id_contactPhone') + '&contactFax=' + $F('id_contactFax') + '&towerid=' + towerID + '&contactCell=' + $F('id_contactCell') + '&contactType=' + contactType;
    var myAjax = new Ajax.Updater( 'contactlistdiv', url, { method:'post', parameters:pars, asynchronous: true });
}

var OLD_HIGHLIGHT = 0;

function viewcontactdetails(contactid){
    // this function will get the contact information of the selected user and display it
    var url = '/viewcontactdetails/' + contactid ;    
    var myAjax = new Ajax.Updater( 'contactdetailsdiv', url, { method:'get', asynchronous: true});
    
    var spanVar;
    var linkVar
    
    // unselect the old contact
    if (OLD_HIGHLIGHT != 0){
        spanVar = $('contactspan_' + OLD_HIGHLIGHT);
        linkVar = $('link_' + OLD_HIGHLIGHT);
        spanVar.style.background = '';
        linkVar.style.color = '#B96D00';
    }
    
    // highlight the new contact
    spanVar = $('contactspan_' + contactid);    
    linkVar = $('link_'+contactid);
    spanVar.style.background = '#8B9530';
    linkVar.style.color = '#ffffff';
    OLD_HIGHLIGHT = contactid
}

function addFile(tempNumber){
    // this function will hide the selected file and add a new file input to the div
    
    var currFile = $('addInput_' + tempNumber);    
    if (currFile.value != ""){
        // the file input isn't blank
    
        var newNumber = Number(tempNumber) + 1;
        nextNumber = String(newNumber);    
        
        // hide the file we just uploaded
        var currDiv = $('newaddfilediv_' + tempNumber)        
        currDiv.style.display = 'none';
        
        // create a new file input and add it to the div
        var fileDiv = $('addFileDiv');        
        var newdiv = document.createElement('div');
        newdiv.setAttribute('id', 'newaddfilediv_' + nextNumber)
        newdiv.innerHTML = "<input type='file' name=\"fileupload_" + nextNumber + "\" id=\"addInput_" + nextNumber + "\"><input id='addButton_" + nextNumber + "' type='button' value='Add' onclick=\"addFile('" + nextNumber + "');\">";
        fileDiv.appendChild(newdiv);        
        
        // display a text version of te file uploaded
        var toBeDisplayed =$('toBeUploadedDiv');
        var displayDiv = document.createElement('div');
        displayDiv.setAttribute('id', 'displaydiv_' + tempNumber);
        displayDiv.innerHTML = "<span id=\"displayFile\">" + currFile.value + "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"removeFile('" + tempNumber + "')\">remove</a></span>";
        toBeDisplayed.appendChild(displayDiv);
    }
}

function removeFile(addFileNumber){
    // this function will remove the selected file from the div    
    var displayDiv = $('toBeUploadedDiv');
    var removeDiv = $('displaydiv_' + addFileNumber);    
        
    displayDiv.removeChild(removeDiv);
    
    var addFileDiv = $('addFileDiv');
    var removeFileDiv = $('newaddfilediv_' + addFileNumber);
    
    addFileDiv.removeChild(removeFileDiv);
}

function removeUploadedFile(documentid) {
    // this function will remove the file that was already uploaded to the db
    var url = '/removelicensedocument/' + documentid + '/';
    var myAjax = new Ajax.Request( url, { method:'get', asynchronous: true});
    var toBeDisplayed = $('toBeUploadedDiv');
    var removeDiv = $('uploadedDiv_' + documentid);
    toBeDisplayed.removeChild(removeDiv);
}