//<script runat=server language="Javascript">

/******************************************************************************/
// common notes & attachments methods
function Notes( noteProfileName, entityPrimaryId, additionalParams ) {
	sURL = "/servlet/RouterServlet?p_build_page=Note&p_popup=true&p_profile=" + noteProfileName + "&p_entity_primary_id=" + entityPrimaryId;
	if( additionalParams != undefined ) {
		sURL += additionalParams;
	}
	hWnd=homeTelosWindowOpen(sURL,"notesPopup","width=750,height=400,resizable=yes,scrollbars=yes");
}

function Attachments( pageName,  allowAdd, caseId, additionalParams ) {
	sURL = "/servlet/RouterServlet?p_build_page=Attachment&p_popup=true&p_case_id=" + caseId + "&p_attachment_page=" + pageName + "&add=" + allowAdd;
	if( additionalParams != undefined ) {
		sURL += additionalParams;
	}
	hWnd=homeTelosWindowOpen(sURL, "attachmentPopup", "width=750,height=300,resizable=yes,scrollbars=yes");
}

function AddAttachments( pageName, additionalParams, additionalParams1 ) {

	sURL = "/servlet/RouterServlet?p_build_page=AttachmentAdd&p_popup=true&p_attachment_page=" + pageName;
	if( additionalParams != undefined ) {		
		sURL += additionalParams;
	}
	if( additionalParams1 != undefined ) {		
		sURL += additionalParams1;
	}
	hWnd=homeTelosWindowOpen(sURL, "attachmentPopup", "width=830,height=350,resizable=no,scrollbars=yes")
}

function AddDocuments( pageName, additionalParams, additionalParams1 ) {

	sURL = "/servlet/RouterServlet?p_build_page=DocumentAdd&p_popup=true&p_attachment_page=" + pageName;
	if( additionalParams != undefined ) {		
		sURL += additionalParams;
	}
	if( additionalParams1 != undefined ) {		
		sURL += additionalParams1;
	}
	hWnd=homeTelosWindowOpen(sURL, "attachmentPopup", "width=830,height=350,resizable=yes,scrollbars=yes")
}

/*******************************************************************************/
// The following 2 fuctions enable auto tabbing in form fields
function getNextElement(iElement) {
	var parentForm = iElement.form;

	for (i=0; i<parentForm.length; i++) {
		if (parentForm.elements[i]==iElement)
			if (parentForm.elements[i+1])
				return parentForm.elements[i+1];
	} return null;
}
function autotab(iCtl, iMax) {
	var nextElement, textValue = iCtl.value;
	if (textValue.length==iMax) {
		nextElement = getNextElement(iCtl);

		if (nextElement) {
			nextElement.select();
			nextElement.focus();
		}
	}
}

// Common popup function
function openPopUp(sUrl){
	hWnd=homeTelosWindowOpen(sUrl,'general_window','directories=no,toolbar=no,scrollbars=yes,width=700,height=550,resizable=yes');
}

//Common popup function
function openPopUpOptions(sUrl){
	hWnd=homeTelosWindowOpen(sUrl,"popUp",'directories=no,toolbar=no,scrollbars=yes,width=560,height=375,resizable=yes');
}

/*
Common method to call from all hyperlinks in pages
This ensures consistent behavior of the hyperlinks, like say prompting to 'save' the
form inc case there are any changes made by the user
*/
function hyperLink(sUrl) {
	if ( (typeof(document.main) != "undefined") && (typeof(document.main.js_changed) != "undefined") && (document.main.js_changed.value == 'true') && (confirm ("   Save Form Changes?\n\n    'OK' to save changes.\n'Cancel' to discard changes.")) ) {
		formAction('SAVE');
	}
	else {
		homeTelosWindowLocation(sUrl);		
	}
}
function navigate(pageName, reset) {
	url = '/servlet/RouterServlet?p_build_page=' + pageName;
	if( reset == 'Y' || reset == 'y' )
		url += '&p_reset=Y';
	hyperLink( url );
}

function navigate(pageName, reset, navDisabled) {
	if(navDisabled) {
		this.href ='javascript:void(0)';
		return;
	}
	url = '/servlet/RouterServlet?p_build_page=' + pageName;
	if( reset == 'Y' || reset == 'y' )
		url += '&p_reset=Y';
	hyperLink( url );
}

function createWorkOrder() {
	var caseId = 0;
	if ( (typeof(document.main) != "undefined") && (typeof(document.main.p_case_id) != "undefined") && (document.main.p_case_id.value > 0) ) {
		caseId = document.main.p_case_id.value;
	}
	var url = "/servlet/RouterServlet?p_build_page=WorkOrder&p_popup=true&p_case_id=" + caseId;
	hWnd = homeTelosWindowOpen(url,"wo","width=760,height=550,resizable=yes,scrollbars=yes");
}

/********************************     Help Functions ****************************************************/
var helpWindow;
var isNetsc = navigator.appName == "Netscape" ? true : false ;

function helpDoNothing() {}

function showHelp(id,width,height,x,y) {
	var parentWinX;
	var parentWinY;
	var helpWinX;
	var helpWinY;
	var url = '/help.html#'+id;

	if (!isNetsc) { // Then IE
		parentWinX = event.x;
		parentWinY = event.y;
		helpWinX = parentWinX+window.screenLeft+5;
		helpWinY = parentWinY+window.screenTop+5;
	}
	else { // netscape
		parentWinX = x;
		parentWinY = y;
		helpWinX = parentWinX+window.screenX + 20;
		helpWinY = parentWinY+window.screenY + 110;
	}
	//alert("X:"+x+"; Y:"+y);
	//alert("X:"+parentWinX+"; Y:"+parentWinY);
	
	if (helpWinX+width > 800){
		helpWinX = helpWinX-width-28;
		helpWindow = homeTelosWindowOpen(url,id,"width="+width+",height="+height+",left="+helpWinX+",top="+helpWinY+",resizable=no,scrollbars=no,location=no,menubar=no,toolbar=no");
	} else {
		helpWindow = homeTelosWindowOpen(url,id,"width="+width+",height="+height+",left="+helpWinX+",top="+helpWinY+",resizable=no,scrollbars=no,location=no,menubar=no,toolbar=no");
	}
}

function helpMouseOutEffects() {
	if (helpWindow!= undefined && typeof(helpWindow) != "undefined"){
		helpWindow.close();
	}
}
/********************************    End Help Functions ****************************************************/

function getAttachment(sFile) {
	var sURL;
	var nIndex = (sFile.lastIndexOf(".")+1);
	var fileExt;

	//Extensions that can be opened in a browser as an image and thus "aggregated"
	var viewExtensions = "jpg,bmp,gif,png,tiff";

	var sOptions = "width=750,height=550,resizable=yes,scrollbars=yes,toolbar=yes";

	if (nIndex > 0) {
		fileExt = sFile.substr(nIndex).toLowerCase();
		if (viewExtensions.indexOf(fileExt) < 0){
			sURL = sFile + '?content-type=application/octet-stream&name='+sFile;
			sOptions = "";
		} else {
			sURL = sFile;
		}
		hWnd=homeTelosWindowOpen(sURL,"popup",sOptions);
	}
}

// truncates text in a text area to the specified length.
// used by HTML.displayTextArea
function limitLengthOld( textarea, length ) {    
	if( textarea.value.length > length ) {
        alert("Please limit the Comments to "+length+" characters"); 
		textarea.value = textarea.value.substring( 0, length );
	}     
}
function limitLength( textarea, length ){
	limitLength( textarea, length, null );
}
// this method for ms sql server, preventing truncation error.
function limitLength( textarea, length, e ) {		
 	var temp = textarea.value;
 	//if (e && e.timeStamp > 0) return; //for w3 browsers 	 	
 	if (temp){
	 	temp = temp.replace(/[\n\r]+/g, "  ");//new line is 2 characters long in ms swl
 	}  	
	if( temp.length > length ) {
        alert("Please limit the Comments to "+length+" characters");
        temp = textarea.value.substring( 0, length );
        temp = temp.replace(/[\n\r]+/g, "  ");
        var to = length - Math.abs(temp.length - textarea.value.length);
		textarea.value = textarea.value.substring( 0, to );
	}
}

/**********************************************************************
** Used in lists for filter, sort, prev, next, excel, and search */

function toExcel() {
	document.main.p_action.value = 'EXCEL';
	document.main.p_mode.value = '';	
	document.main.submit();
	//document.main.p_action.value = '';
   
}

function toText() {
	document.main.p_action.value = 'EXCEL';
	document.main.p_mode.value = 'TEXT';	
	document.main.submit();
	//document.main.p_action.value = '';
}

function saveChanges(sAction) {  
    
    if( eval( document.main.js_changed != undefined ) ) {
        if ((document.main.js_changed.value == 'true') && (confirm ("   Save Form Changes?\n\n    'OK' to save changes.\n'Cancel' to discard changes.")) ) {
           formAction("SAVE");
        }
        else {           
            reposition( sAction );
        }
    }
    else {        
        reposition( sAction );
    }
}

function reposition(start) {
	if( eval( "document.main.p_action" != undefined ) ) {
		document.main.p_action.value = "REPOSITION";
	}
	if( start != undefined && eval( "document.main.p_start" ) != undefined ) {
		document.main.p_start.value = start;
	} else if( eval( "document.main.p_start" ) != undefined ) {
		document.main.p_start.value = 1;
	}
	// don't change criteria
	document.main.submit();
}

function changeCriteria(criteria) {
	document.main.p_criteria.value = criteria;
	saveChanges( 1 );
}

function refreshForFilterOrSort(why) {
	if( 'FILTER' == why ) {
		if( eval( "document.main.p_quick_search_case" ) != undefined ) {
			document.main.p_quick_search_case.value = '';
		}
		if( eval( "document.main.p_quick_search_loan" ) != undefined ) {
			document.main.p_quick_search_loan.value = '';
		}
		if( eval( "document.main.p_quick_search_fnma_loan" ) != undefined ) {
			document.main.p_quick_search_fnma_loan.value = '';
		}
		changeCriteria( 'FILTER' );
	} else {
		saveChanges( 1 );
	}
}

// called on keyUp event in the quick search input text field
function checkQuickSearchSubmitLists(e,criteria) {
	if ((e.which && e.which == 13) || e.keyCode == 13){
		changeCriteria(criteria);
	} 
}

function validateQSCriteria(formNm){

	var quickSearch = formNm.p_quick_search_by.value;
	
	if(quickSearch == 'undefined' || quickSearch == ''){
	 alert('Please select one search criteria');
	 return false;
	}
	
	if(quickSearch == 'QS_CASE'){
		formNm.p_quick_case_number.value = formNm.p_quick_search_value.value;
	}else if(quickSearch == 'QS_LOAN'){
		formNm.p_quick_loan_number.value = formNm.p_quick_search_value.value;
	}else if(quickSearch == 'QS_ADDRESS'){
		formNm.p_quick_address.value=formNm.p_quick_search_value.value;
	}	else if(quickSearch == 'QS_REOID'){
		formNm.p_quick_reo_number.value=formNm.p_quick_search_value.value;
	}else if(quickSearch == 'QS_FNMA_LOAN'){
		formNm.p_quick_fnma_loan_number.value = formNm.p_quick_search_value.value;
	}
	
	//formNm.p_quick_search_value.value=""; // To Reset the quick search box
	formNm.QUICKSEARCHUSED.value='Y'; //specify the search is from quick search
	formNm.p_quick_search_type_value.value=formNm.p_quick_search_by.value; //To retain the value of select box
	return ( true );
}

/********************************************************************/

// called on keyUp event in the quick search input text field
function checkQuickSearchSubmit(e) {
	if ((e.which && e.which == 13) || e.keyCode == 13){

	   	if (document.main.p_action != undefined) {
			document.main.p_action.value = "REPOSITION";
		}	 
		formAction('QUICKSEARCH');
	}
}

// called on keyUp event in the quick search input text field
function checkQuickSearchSubmitForQSDropDown(e) {
	
	if ((e.which && e.which == 13) || e.keyCode == 13){
		var validateResult = validateQSCriteria(document.main);
		
		if(validateResult == false){
			return false;
		}
		if (document.main.p_action != undefined) {
			document.main.p_action.value = "REPOSITION";
		}
		formAction('QUICKSEARCH');
		return false ;
	}else{
	// document.main.submit();
	return ( true );
	}
}

function submitFromQSForQSDropDown() {
	var validateResult = validateQSCriteria(document.main);
	if(validateResult == false){
		return false;
	}
	if (document.main.p_action != undefined) {
		document.main.p_action.value = "REPOSITION";
	}	 
	
	if ( (document.main.js_changed != undefined) && (document.main.js_changed.value == 'true') && 
					(confirm ("   Save Form Changes?\n\n    'OK' to save changes.\n'Cancel' to discard changes.")) ) {
		if (document.main.p_quick_case_number != undefined) {
			document.main.p_quick_case_number.value = "";
		}		
		if (document.main.p_quick_loan_number != undefined) {
			document.main.p_quick_loan_number.value = "";
		}		
		if (document.main.p_quick_address != undefined) {
			document.main.p_quick_address.value = "";
		}
		
		if (document.main.p_quick_reo_number != undefined) {
			document.main.p_quick_reo_number.value = "";
		}
		
		document.main.QUICKSEARCHUSED.value=''; //specify the search is not from quick search. It is a save not a quick search
 		formAction("SAVE");
 	} else {
 		formAction("QUICKSEARCH");
		//document.main.submit();
	}
}

/*****************************************************************/
// used for selecting all checkboxes & printing all selected

function selectDeselectAllCheckboxes( checkboxName, checked ) {
	sField = eval("document.main." + checkboxName );
	if( sField != undefined ) {
		sLength = eval("document.main." + checkboxName + ".length");
		if (sLength == undefined){
			sField.checked = checked;
		} else {
			for(i=0;i<sLength; i++) {
				sField[i].checked = checked;
			}
		}
	}
}

function identifyCheckboxSelections( checkboxName, delimiter ) {
/*
this supports two styles of parameter submission.  to get a parameter like this:
&p_wo_id=99525,99523,99506,99556
submit ',' for the delimiter, then create the query string like this:
		var wos = identifyCheckboxSelections( 'p_wo_id', ',' );
		if( wos.length > 0 )
			wos = wos.substring( 1 );

to get a parameter like this:
&p_wo_id=99525&p_wo_id=99523&p_wo_id=99506&p_wo_id=99556
submit '&p_wo_id=' for the delimiter:
		var wos = identifyCheckboxSelections( 'p_wo_id', '&p_wo_id=' )
*/
	var idStr = '';
	sField = eval("document.main." + checkboxName );
	if( sField != undefined ) {
		sLength = eval("document.main." + checkboxName + ".length");
		if (sLength != undefined){
			for(i=0;i<sLength; i++) {
				if (sField[i].checked == true){
					idStr += delimiter + sField[i].value;
				}
			}
		} else if( sField.checked == true ) {
			idStr = delimiter + sField.value;
		}
	}
	return idStr;
}

/*****************************************************************************************************/

	function getAmount(amount) {
		var amt = amount;
		
		if ( isDecimal(amount)){
			if (amt == 0) {return 0;}
			return Math.round(parseFloat(amt)*100)/100;
		} else {
		 return 0;
		}
	}


//</script>