// some global data
var selectWindow = null;

function encodeURL(page) {
    if (!page)
        return page;

    var quoteIndex = page.indexOf('?');
    if (quoteIndex < 0 || (quoteIndex+1) == page.length)
        return page;

    var result = page.substr(0, quoteIndex);
    var params = page.substr(quoteIndex+1, page.length);

    var firstParam = true;
    while (params.length > 0) {
        var currentParam = '';
        var ampersandIndex = params.indexOf('&');
        if (ampersandIndex < 0) {
            currentParam = params;
            params = '';
        } else {
            currentParam = params.substr(0, ampersandIndex);
            params = params.substr(ampersandIndex+1, params.length);
        }

        var equalIndex = currentParam.indexOf('=');
        if (equalIndex > 0 && currentParam.length > equalIndex+1) {
            var varName = currentParam.substr(0, equalIndex);
            var paramValue = currentParam.substr(equalIndex+1, currentParam.length);
            currentParam = varName + '=' + escape(paramValue);
        }
        
        // add the 
        if (firstParam) {
            firstParam = false;
            result += '?';
        } else {
            result += '&';
        }
        result += currentParam;
    }

    // the encoded url
    return result;
}

function createSimpleWindow(targetPage, horzRatio, vertRatio, encodeTarget) {

    if (horzRatio == null)
        horzRatio = .8;
    if (vertRatio == null)
        vertRatio = horzRatio;
    
    var horzOutside = ((1-horzRatio)/2);
    var vertOutside = ((1-vertRatio)/2);
    
    var targetOptions = "";
    targetOptions += ",toolbar=no";
    //targetOptions += ",toolbar=yes";

    targetOptions += ",menubar=no";
    //targetOptions += ",menubar=yes";

    targetOptions += ",personalbar=no";
    targetOptions += ",statusbar=no";
    targetOptions += ",scrollbars=yes";

    // put the windows in the middle of the screen
    targetOptions += ",width=" + screen.availWidth * horzRatio;
    if (window.screenX) 
        targetOptions += ",screenX="+ screen.availWidth * horzOutside;
    else
        targetOptions += ",left="+ screen.availWidth * horzOutside;
    targetOptions += ",height=" + screen.availHeight * vertRatio;
    if (window.screenY)
        targetOptions += ",screenY=" + screen.availHeight * vertOutside;
    else 
        targetOptions += ",top=" + screen.availHeight * vertOutside;

    targetOptions += ",resizable=no";
    targetOptions += ",titlebar=yes";

    // close any old windows
    if (selectWindow != null) {
        selectWindow.close();
        selectWindow = null;
    }

    // encode the target page if requested
    if (encodeTarget)
        targetPage = encodeURL(targetPage);

    // open the new window
    selectWindow = window.open(targetPage,'',targetOptions);
}


// the best guess so far at window redirection
function openWin(fieldType, fieldName, fieldValue, header, recalculate, runMethod) {

    var targetPage = null;

    // validate the field type
    switch (fieldType) {
        case "AllCustomer" :
        case "ParentCustomer" :
        case "SoldTo" :
        case "SoldToWithParents" :
        case "ShipTo" :
        case "ShipToWithParents" :
        case "AllSupplier" :
        case "Supplier" :
        case "RemitTo" :
        case "ParentSupplier" :
        case "FreightCarrier" :
        case "Agent" :
        case "Salesman" :
        case "SalesmanAgent" :
        case "Employee" :
        case "AllProducts" :
        case "HeaderProducts" :
        case "LineItemProducts" :
			targetPage = "/selectlookup.do?fieldName=" + fieldName + "&fieldValue=" + fieldValue + "&fieldType=" + fieldType;
            if (header)
                targetPage += "&header=" + header;
			if (runMethod)
				targetPage += "&runMethod=" + runMethod;
            break;

        default :
            return null;
    }

    if (recalculate == true) 
        targetPage += "&recalculate=true";

    createSimpleWindow(targetPage, .8, .8, false);
}

// close an open window
function closeWin() {
    if (selectWindow != null) {
        selectWindow.close();
        selectWindow = null;
    }
    return null;
}


function assignValue(winName, formField, value) {

    // no window, nothing to do
    if (winName == null) return;
    // no form / field, nothing to do
    if (formField == null) return;
    // no value, nothing to do
    if (value == null) return;

    //alert("form=" + winName.document.forms[0].name + " formField=" + formField + " value=" + value);
    
    var fieldObj = Struts_eval(formField, 1, winName.document);
    if (fieldObj == null)
        return;
    
    var recalculate = null;
	var runMethod = null;
    if (Request) {
        recalculate = Request.QueryString("recalculate");
		runMethod   = Request.QueryString("runMethod");
	}
	if (recalculate == 'undefined')
		recalculate = null;
	if (runMethod == 'undefined')
		runMethod = null;

	if (recalculate == true || recalculate == 'true') {
        if (fieldObj.value == value)
            recalculate = false;
    }
    
    // make the assignment
    fieldObj.value = value;

    if (recalculate == true || recalculate == 'true') {
        if (winName.saveScreenPosition)
            winName.saveScreenPosition(fieldObj);
		if (runMethod != null) {
			if (winName.submitRecalculateMethod)
				winName.submitRecalculateMethod(runMethod);
		} else {
			if (winName.submitFormForRecalculate)
				winName.submitFormForRecalculate();
		}
    }
}
