/*
'**************************************************************************************************
'File Name:		QuoteEdit.js (QuoteEdit.aspx)
'Object Type:	Javascript file
'
'Author Name:	Mark E. Watkins - VariLogic Technologies, LLC
'Creation Date:	03/23/2005
'
'Purpose:		Used as a library of client-side functions for the aspx file specified above 
'				in 'File Name'.
'
'
'Include Dependencies:
'None in this file - all are included in the aspx file.
'
'
'Date Changed		Changed By		Reason
'--------------------------------------------------------------------------------------------------
'
'
'**************************************************************************************************
*/

var mbIsDirty = false;
var mbLoading = false;
var mbReturnToInvNo = false;
var mbReturnToInvName = false;
var mbReloadInventoryList;
var mbSubAllInOne = true;
var mbPrintQuoteId = 0;
var mbPrintCommand = "none";
var mbSaving = false;
var mbCancelling = false;

var prevTR = null;

var mbRecalculateTotals;
var inventoryTotal;

var curWidth = 0;
var curX = 0;
var newX = 0;
var mouseButtonState = "up";
var suppressPriceOverrideCol = false;
var resetInvNoFocus = null;
var invNumbers = new Array();

var treeImages = new Object();

//Split array (contained in the 'link' property) elements.
var LINK_INV_NO = 0;
var LINK_INV_DESCR = 1;
var LINK_INV_PRICE = 2;
var LINK_INV_PARENT_ID = 3;

//Inventory row column constants.
var ITEM_PRICE = 3;
var ITEM_PRICE_OVERRIDE = 4;

//Keycode constants.
var KEY_ENTER = 13;
var KEY_ZERO = 48;
var KEY_NINE = 57;
var KEY_DEC_PT = 46;
var KEY_UPPER_A = 65;
var KEY_UPPER_Z = 90;
var KEY_LOWER_A = 97;
var KEY_LOWER_Z = 122;
var KEY_FORWARD_SLASH = 47;
var KEY_DASH = 45;
var KEY_COLON = 58;
var KEY_UPPER_P = 80;
var KEY_LOWER_P = 112;
var KEY_UPPER_M = 77;
var KEY_LOWER_M = 109;
var KEY_TAB = 9;

// -------------------------------------------------------
// Window Events
// -------------------------------------------------------
function window.onload() {
	//
}
function bodyEntry_onkeypress() {
	if( event.keyCode == KEY_ENTER ) 
	{
		if( event.srcElement.id == "txtComment" ) { event.returnValue = true; }
		else if( event.srcElement.id == "cmdSave" || event.srcElement.id == "cmdSaveAndEmail" ) { event.returnValue = true; }
		else {
			window.event.keyCode = KEY_TAB; 
			window.event.returnValue = true; 
		}
	}
}
function body_onbeforeunload()
{
	if ( !mbDynaMenuClicked && !mbTreeClicked && !mbSaving && !mbCancelling) {
		mbDynaMenuClicked = false;
		if ( mbIsDirty ) {
			event.returnValue = "The Quote data has been changed.\n\r\n\rClick 'Cancel' to return to the Quote.\n\rClick 'OK' to continue (your changes will be lost)\n\r\n\r";
		}
	}
}

function PrintQuote(command)
{
	window.open(command, "_blank", 
		"height=500,width=825,directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,titlebar=no,toolbar=yes");
}

function cmdSave_onclick()
{
	mbSaving = true;
	buildInvItemsToken();
	var cmd = document.getElementById("__Command");
	cmd.value = "SAVE";
	//document.Form1.submit();
}

function cmdEmail_onclick()
{
	cmdSave_onclick();
}

function cmdPrint_onclick()
{
	cmdSave_onclick();
}

function cmdCancel_onclick()
{
	mbCancelling = true;
	if ( mbIsDirty ) {
		if ( confirm('Changes have been made to the current Quote.\n\rAre you sure you want to Cancel?') ) {
			document.forms[0].__EventSource.value = "cmdCancel";
			return true;
		}
		else {
			mbCancelling = false;
			return false;
		}
	}
	else
		return true;
}

function SetSubAllInOne()
{
	if( document.forms[0].ddlSubContractor.selectedIndex != document.forms[0].ddlUSubContractor.selectedIndex )
		mbSubAllInOne = false;
	if( document.forms[0].txtActMoveDate.value != document.forms[0].txtUnloadDate.value )
		mbSubAllInOne = false;
	if( document.forms[0].txtMoveTime.value != document.forms[0].txtUnloadTime.value )
		mbSubAllInOne = false;
	if( document.forms[0].txtEstMoveDuration.value != document.forms[0].txtUnloadDuration.value )
		mbSubAllInOne = false;
}
// -------------------------------------------------------
// Control Events
// -------------------------------------------------------
//
//
function Validator_ddlIndexGreaterThan0(source, args) {
	var data = [];
	data = args.Value.split(";");
	if( data[0] > 0 ) {
		args.IsValid = true;
	}
	else {
		args.IsValid = false;
	}
}

function Validator_IsDateValid(source, args) {
	var isValid = true;
	if( !isDate(args.Value) ) {
		isValid = false;
		source.errormessage = "Not a valid Date";
	}
	if( isValid && !isDateCurrent(args.Value) ) {
		isValid = false;
		source.errormessage = "Date cannot be in the past";
	}
	if( isValid ) {
		source.errormessage = "";
	}
	args.IsValid = isValid;
}


function datePopup_onclick(control) {
	var prevVal = control.value;
	displayCalendar(control, true);
	if( control.value != prevVal ) {
		control.fireEvent("onchange");
		//
		isDirty();
	}
}

function dateEntry_onchange(control, callIsDirty, description) {
	//
	if( !isDate(control.value) ) {
		alert("The date you entered is invalid.");
		control.value = "";
		event.returnValue = false;
	}
	else {
		if( control.name == "txtEstMoveDate" ) {
			isDateCurrent(control.value, description);
		}
		control.value = formatDate(control.value, true, true, "/", true);
	}
	//
	if( callIsDirty ) isDirty();
}

function zipcodeEntry_onkeypress(control) {
	//
	if( (event.keyCode < KEY_ZERO || event.keyCode > KEY_NINE) && (event.keyCode != KEY_DASH || countOf(control.value, "-") > 1) ) event.returnValue = false;
}

// -------------------------------------------------------
// Common Functions
// -------------------------------------------------------
function buildInvItemsToken() {
	var table = document.getElementById("InventoryItems");
	var tr = null;
	var token = "";
	//Iterate over the inventory items.
	if( table != null )
	{
	    for( var i = 0; i < table.rows.length; i++ ) {
		    //Reference the current row.
		    tr = table.rows[i];
		    //
		    if( tr.childNodes[2].childNodes[0].value != 0 )
		    {
			    //Build the token to pass to the server.
			    token += tr.childNodes[1].innerText + ";";  //ID
			    token += tr.childNodes[2].innerText + ";";  //Name
			    //token += tr.childNodes[4].innerText + ";";  //Price
			    token += "[PRICE];";
			    token += tr.childNodes[3].childNodes[0].value + ";";  //Qty
			    //
			    token += tr.childNodes[4].innerText + ";"; //CategoryId
			    //
			    //This token value is used to restore the state of the inv. tree when the page is edited again.
			    token += " " + "|";
		    }
	    }
	}
	//Set the token variable to be passed to the server.
	document.forms[0].__InvItemsToken.value = token;
}
// -------------------------------------------------------
// Page State Functions
// -------------------------------------------------------
function isDirty() { 
	if ( mbLoading ) return;
	
	mbIsDirty = true;
	mbDynaIsDirty = true;
	mbDynaWhatIsDirty = "Quote";
}
//-eof-
