// Javascript functions for File Manager

// Global Variables

var ajax_controller = 'default_ajax.asp';
var guiForm;
var orderItemsForm;
var dictionaryForm;
var guiFormName = 'frmMessage';
var chooserFilter = '';
var queryCount = 1; 


// Application Functions

function showHeader() {

	v_callmethod = 'showheader';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	//alert(strURL); 
	document.getElementById("headerDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'showHeader_return', 'null');
	return true;
}

function showHeader_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("divProjects").innerHTML = vLoader;
		document.getElementById("headerDIV").innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function showNav(vMinistryID) {

	v_callmethod = 'shownav';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	strURL = strURL + '&ministryid=' + vMinistryID;
	//alert(strURL); 
	document.getElementById("leftDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'showNav_return', 'null');
	return true;
}

function showNav_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("divProjects").innerHTML = vLoader;
		document.getElementById("leftDIV").innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function showLogin() {

	v_callmethod = 'showlogin';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	//alert(strURL);
	document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'showLogin_return', 'null');
	return true;
}

function showLogin_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("divProjects").innerHTML = vLoader;
		document.getElementById("contentDIV").innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function showMessageList(vPageNo,vMessage,vMinistryID) {

    showNav(vMinistryID)
	
	v_callmethod = 'showmessagelist';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	strURL = strURL + '&pageno=' + vPageNo + '&message=' + vMessage + '&ministryid=' + vMinistryID;
	//alert(strURL); 
	document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'showMessageList_return', 'null');
	return true;
}

function showMessageList_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("divProjects").innerHTML = vLoader;
		document.getElementById("contentDIV").innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function viewMessage(vLogID,vMinistryID,vPageNo) {

    showNav(vMinistryID)
	
	v_callmethod = 'viewmessage';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	strURL = strURL + '&pageno=' + vPageNo + '&logid=' + vLogID + '&ministryid=' + vMinistryID;
	//alert(strURL); 
	document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'viewMessage_return', 'null');
	return true;
}

function viewMessage_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("divProjects").innerHTML = vLoader;
		document.getElementById("contentDIV").innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function createMessage(vMinistryID) {

    //showNav(vMinistryID)
	
	v_callmethod = 'createmessage';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	strURL = strURL + '&ministryid=' + vMinistryID;
	//alert(strURL); 
	document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'createMessage_return', 'null');
	return true;
}

function createMessage_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("divProjects").innerHTML = vLoader;
		document.getElementById("contentDIV").innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function saveMessage(vMinistryID) {

    //showNav(vMinistryID)
	
	if (document.frmMessages.subject.value != '' && document.frmMessages.message.value != '') {
    	v_callmethod = 'savemessage';
    	strURL = 'callmethod=' + v_callmethod;
    	strURL = strURL + '&ministryid=' + vMinistryID;
    	strURL = strURL + '&lastlogid=' + document.frmMessages.lastlogid.value;
    	strURL = strURL + '&userid=' + document.frmMessages.recipient.value;
    	
        for (i=document.frmMessages.type.length-1; i > -1; i--) {
            if (document.frmMessages.type[i].checked) {
            	strURL = strURL + '&type=' + escape(document.frmMessages.type[i].value);
            }
        }
    	
    	strURL = strURL + '&subject=' + escape(document.frmMessages.subject.value);
    	strURL = strURL + '&urgencylevel=' + escape(document.frmMessages.urgencylevel.value);
    	strURL = strURL + '&message=' + escape(document.frmMessages.message.value);
    	strURL = strURL + '&attachments=' + escape(document.frmMessages.attachments.value);
    	document.getElementById('sendButton').value = 'Sending...Please Wait...';
    	document.getElementById('sendButton').disabled = true;
    	//alert(strURL);  
    	//document.getElementById("contentDIV").innerHTML = vLoader;
    	PassAjaxResponseToFunctionUsingPost(ajax_controller,strURL, 'saveMessage_return', 'null');
    } else {
        alert('Please enter a SUBJECT and MESSAGE before attempting to send this message.');
    }
	return true;
}

function saveMessage_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("divProjects").innerHTML = vLoader;
        //alert(arr_response[1]);
		showMessageList('1','',arr_response[2]);
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function signIn() {

	if (document.frmMessages.username.value != '' && document.frmMessages.password.value != '') {
    	v_callmethod = 'signin';
    	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
    	strURL = strURL + '&username=' + document.frmMessages.username.value + '&password=' + document.frmMessages.password.value;
    	//alert(strURL); 
    	//document.getElementById("contentDIV").innerHTML = vLoader;
    	PassAjaxResponseToFunction(strURL, 'signIn_return', 'null');
	} else {
	    alert('Please provide a USERNAME and PASSWORD before click on the SIGN IN button');
	}
	return true;
}

function signIn_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        document.getElementById("contentDIV").innerHTML = vLoader;
        if (arr_response[2] != '') { 
            showNav(arr_response[2]); 
        } else { 
            showNav(''); 
        }

		showHeader();

		if (arr_response[2] != '' && arr_response[3] != '') {
    		viewMessage(arr_response[3],arr_response[2],'1');
        } else if (arr_response[2] != '') {
            showMessageList('1','You have successfully signed in.',arr_response[2]);
		} else {
    		showMessageList('1','You have successfully signed in.','');
		}
	} else {
	    showLogin();
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function autoLogin(vUserID,vEmail,vMinistryID,vLogID) {

	v_callmethod = 'autologin';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	strURL = strURL + '&userid=' + vUserID;
	strURL = strURL + '&email=' + vEmail;
	strURL = strURL + '&ministryid=' + vMinistryID;
	strURL = strURL + '&logid=' + vLogID;
	//alert(strURL);  
	//document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'signIn_return', 'null');
	return true;
}

// ---------------------------------------------------------------------------------

function autoLoginByToken(vToken,vMinistryID,vLogID) {

	v_callmethod = 'autologin';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	strURL = strURL + '&token=' + escape(vToken);
	strURL = strURL + '&ministryid=' + vMinistryID;
	strURL = strURL + '&logid=' + vLogID;
	//alert(strURL);  
	//document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'signIn_return', 'null');
	return true;
}
// ---------------------------------------------------------------------------------

function signOff() {

	v_callmethod = 'signoff';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
	//alert(strURL); 
	//document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'signOff_return', 'null');
	return true;
}

function signOff_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("contentDIV").innerHTML = vLoader;
		showNav('');
		showHeader();
		showLogin();
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function sendReminder() {

	if (document.frmMessages.email.value != '') {
    	v_callmethod = 'sendreminder';
    	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod;
    	strURL = strURL + '&email=' + document.frmMessages.email.value;
    	//alert(strURL);  
    	//document.getElementById("contentDIV").innerHTML = vLoader;
    	PassAjaxResponseToFunction(strURL, 'sendReminder_return', 'null');
	} else {
	    document.frmMessages.email.style.backgroundColor = '#feffcc';
	    alert('You must provide your E-MAIL ADDRESS to have your username and password sent to you.');
	}
	return true;
}

function sendReminder_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
        //document.getElementById("contentDIV").innerHTML = vLoader;
        document.getElementById('divFeedback').innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function showMessageSupportForm(vClientId) {

	v_callmethod = 'showmessagesupportform';
	strURL = ajax_controller + '?'
	            + 'callmethod=' + v_callmethod
	            + '&clientid=' + vClientId;
	//alert(strURL);
	document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'showMessageSupportForm_return', 'null');
	return true;
}

function showMessageSupportForm_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
		document.getElementById("contentDIV").innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------

function handleMessageSupportForm() {

	v_callmethod = 'handlemessagesupportform';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	            + '&subject=' + encodeURIComponent(document.getElementById("subject").value)
	            + '&name=' + encodeURIComponent(document.getElementById("name").value)
	            + '&email=' + encodeURIComponent(document.getElementById("email").value)
	            + '&os=' + encodeURIComponent(document.getElementById("clientos").options[document.getElementById("clientos").selectedIndex].value)
	            + '&browser=' + encodeURIComponent(document.getElementById("clientbrowser").options[document.getElementById("clientbrowser").selectedIndex].value)
	            + '&errors=' + encodeURIComponent(document.getElementById("messageErrors").value)
	            + '&message=' + encodeURIComponent(document.getElementById("message").value)
	            + '&clientid=' + encodeURIComponent(document.getElementById("msgClientId").value);
	//alert(strURL);
	document.getElementById("contentDIV").innerHTML = vLoader;
	PassAjaxResponseToFunction(strURL, 'handleMessageSupportForm_return', 'null');
	return true;
}

function handleMessageSupportForm_return(str_response) {
	//alert('Response: ' + str_response);
	arr_response = str_response.split('||'); 
	if ( arr_response[0] == '1') {
		document.getElementById("contentDIV").innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ---------------------------------------------------------------------------------
