/*
 * Copyright 2001-2006 The Apache Software Foundation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

// ================= FIELD LOOKUP METHODS ============================

var target = null;
var target2 = null;

//Begin C-Libre FR#
function call_lookup(viewName, viewWidth, viewheight) {
    if (! viewWidth) viewWidth = 600;
    if (! viewheight) viewheight = 350;
    
    var obj_lookupwindow = window.open(viewName,'Details', 'width='+viewWidth+',height='+viewheight+',scrollbars=yes,status=no,resizable=yes,top='+my+',left='+mx+',dependent=yes,alwaysRaised=yes');
        obj_lookupwindow.opener = window;
        obj_lookupwindow.focus();    
}

//End C-Libre FR#
function call_fieldlookup(target, viewName, formName,viewWidth,viewheight) {   
    var fieldLookup = new fieldLookup1(target);  
    if (! viewWidth) viewWidth = 350;
    if (! viewheight) viewheight = 200;
    fieldLookup.popup(viewName, formName, viewWidth, viewheight);
}
function call_fieldlookup2(target, viewName) {   
    var fieldLookup = new fieldLookup1(target, arguments);  
    fieldLookup.popup2(viewName);
}
function call_fieldlookup3(target, target2, viewName) {
    var fieldLookup = new fieldLookup2(target, target2);
    fieldLookup.popup2(viewName);
}
//Begin C-Libre FR#
function call_fieldlookupFocus(target, viewName) {   
    var fieldLookup = new fieldLookup1(target, arguments);  
    fieldLookup.popup2(viewName);
    target.focus();
}
//End C-Libre FR#

// Begin neogia specific : TODO add comment
function call_helpfieldlookup(viewName, fieldName,target,viewWidth,viewheight) {   
    var helpFieldLookup = new fieldLookupHelp(viewName);  
    if (! viewWidth) viewWidth = 250;
    if (! viewheight) viewheight = 200;
    this.viewType = 'Field';
    helpFieldLookup.popupHelp(viewName, fieldName,viewWidth,viewheight,viewType);
}
function call_helpformlookup(formName, viewName,target,viewWidth,viewheight) {
    var helpFormLookup = new formLookupHelp(formName);  
    if (! viewWidth) viewWidth = 550;
    if (! viewheight) viewheight = 300;
    this.viewType = 'Form';
    helpFormLookup.popupHelp(viewName,formName,viewWidth,viewheight,viewType);
}
// End neogia specific : TODO add comment

function fieldLookup1(obj_target, args) {
    this.args = args;
	// passing methods
	this.popup = lookup_popup1;
	this.popup2 = lookup_popup2;

	// validate input parameters
	if (!obj_target)
		return lookup_error("Error calling the field lookup: no target control specified");
	if (obj_target.value == null)
		return cal_error("Error calling the field lookup: parameter specified is not valid target control");
    //this.target = obj_target; 
    target = obj_target; 
	
	//initialize lookups
	lookups = [];
	// register in global collections
	//this.id = lookups.length;
	//lookups[this.id] = this;
}
function fieldLookup2(obj_target, obj_target2) {
    // passing methods
    this.popup    = lookup_popup1;
    // Begin neogia specific : restore old popup2 behavior until switch to new ofbiz lookup
    /*
    this.popup2    = lookup_popup2;
    */
    this.popup2   = lookup_popup2_neogia;
    // End neogia specific : restore old popup2 behavior until switch to new ofbiz lookup

    // validate input parameters
    if (!obj_target)
        return lookup_error("Error calling the field lookup: no target control specified");
    if (obj_target.value == null)
        return cal_error("Error calling the field lookup: parameter specified is not valid tardet control");
    target = obj_target;
    // validate input parameters
    if (!obj_target2)
        return lookup_error("Error calling the field lookup: no target control specified");
    if (obj_target2.value == null)
        return cal_error("Error calling the field lookup: parameter specified is not valid tardet control");
    target2 = obj_target2;

	//initialize lookups
	lookups = [];
    // register in global collections
    //this.id = lookups.length;
    //lookups[this.id] = this;
}

// FR#1346075 sn add functions with new parameter function_name. this name correspond to the function need to call when the popup close
function call_fieldlookupFunction(target, viewName,function_name) { 
    var fieldLookup = new fieldLookupFunction(target, arguments,function_name);  
    fieldLookup.popup2(viewName);
}
function call_fieldlookupFunction3(target, target2,viewName,function_name) { 
    var fieldLookup = new fieldLookupFunction2(target, target2,function_name);  
    fieldLookup.popup2(viewName);
}

function fieldLookupFunction(obj_target, args,function_name) {
	this.function_name = function_name;
	this.args = args;
	// passing methods
	this.popup = lookup_popup1;
	this.popup2 = lookup_popup2;

	// validate input parameters
	if (!obj_target)
		return lookup_error("Error calling the field lookup: no target control specified");
	if (obj_target.value == null)
		return cal_error("Error calling the field lookup: parameter specified is not valid target control");
    //this.target = obj_target; 
    target = obj_target; 
	
	//initialize lookups
	lookups = [];
	// register in global collections
	//this.id = lookups.length;
	//lookups[this.id] = this;
}

function fieldLookupFunction2(obj_target, obj_target2,function_name) {
	this.function_name = function_name;
	    // passing methods
    this.popup    = lookup_popup1;
    // Begin neogia specific : restore old popup2 behavior until switch to new ofbiz lookup
    /*
    this.popup2    = lookup_popup2;
    */
    this.popup2   = lookup_popup2_neogia;
    // End neogia specific : restore old popup2 behavior until switch to new ofbiz lookup

    // validate input parameters
    if (!obj_target)
        return lookup_error("Error calling the field lookup: no target control specified");
    if (obj_target.value == null)
        return cal_error("Error calling the field lookup: parameter specified is not valid tardet control");
    target = obj_target;
    // validate input parameters
    if (!obj_target2)
        return lookup_error("Error calling the field lookup: no target control specified");
    if (obj_target2.value == null)
        return cal_error("Error calling the field lookup: parameter specified is not valid tardet control");
    target2 = obj_target2;

	//initialize lookups
	lookups = [];
    // register in global collections
    //this.id = lookups.length;
    //lookups[this.id] = this;
}
// FR#1346075 en

function fieldLookupHelp(obj_view) {
        // passing methods
        this.popupHelp = lookup_popupHelp;

        // validate input parameters
        if (!obj_view)
                return lookup_error("Error calling the field lookup: no target control specified");
        if (obj_view.value == null)
                return cal_error("Error calling the field lookup: parameter specified is not valid tardet control");
        this.viewName = obj_view;       
                
        // register in global collections
        this.id = lookups.length;
        lookups[this.id] = this;
}

function formLookupHelp(obj_view) {
        // passing methods
        this.popupHelp = lookup_popupHelp;

        // validate input parameters
        if (!obj_view)
                return lookup_error("Error calling the field lookup: no target control specified");
        //if (obj_view.value == null)
        //      return cal_error("Error calling the field lookup: parameter specified is not valid tardet control");
        this.viewName = obj_view;       
                
        // register in global collections
        this.id = lookups.length;
        lookups[this.id] = this;
}

function lookup_popupHelp (view_name, form_name, viewWidth, viewheight, viewType) {
        var obj_lookupwindow = window.open('/content/control/Help'+ viewType + '?ContentId=' + view_name,'HelpLookup', 'width='+viewWidth+',height='+viewheight+',scrollbars=yes,status=no,resizable=yes,top='+my+',left='+mx+',dependent=yes,alwaysRaised=yes');
        obj_lookupwindow.opener = window;
        obj_lookupwindow.focus();
}

function lookup_popup1 (view_name, form_name, viewWidth, viewheight) {
	var obj_lookupwindow = window.open(view_name + '?formName=' + form_name + '&id=' + this.id,'FieldLookup', 'width='+viewWidth+',height='+viewheight+',scrollbars=yes,status=no,resizable=yes,top='+my+',left='+mx+',dependent=yes,alwaysRaised=yes');
	obj_lookupwindow.opener = window;
	obj_lookupwindow.focus();
}
function lookup_popup2 (view_name) {
    var argString = "";
    if (this.args.length > 2) {
        for(var i=2; i < this.args.length; i++) {
            argString += "&parm" + (i-2) + "=" + this.args[i];
        }
    }
    var sep = "?";
    if (view_name.indexOf("?") >= 0) {
        sep = "&";
    }

	var obj_lookupwindow = window.open(view_name + sep + 'id=' + this.id + argString,'FieldLookup', 'width=700,height=550,scrollbars=yes,status=no,resizable=yes,top='+my+',left='+mx+',dependent=yes,alwaysRaised=yes');
	obj_lookupwindow.opener = window;
	obj_lookupwindow.focus();
}
function lookup_error (str_message) {
	alert (str_message);
	return null;
}

// Begin neogia specific : restore old popup2 behavior until switch to new ofbiz lookup
function lookup_popup2_neogia (view_name) {

        var obj_lookupwindow = window.open(view_name + '?id=' + this.id,'FieldLookup', 'width=700,height=550,scrollbars=yes,status=no,top='+my+',left='+mx+',dependent=yes,alwaysRaised=yes');
        obj_lookupwindow.opener = window;
        obj_lookupwindow.focus();
}
// End neogia specific : restore old popup2 behavior until switch to new ofbiz lookup

//NEOGIA ADD since release 0.8.8
//functions for dynamique variable positionne

function getTargetWithAnyInformation( lookup, input, att1 ) {
   target = lookup + "?" + att1 + "&" + input.name + "_op=like&";
    return target; 
}

function getTargetWithOneAtt( lookup, input, att1 ) {
    target = lookup + "?" + att1.name + "=" + att1.value + "&" + input.name + "_op=like&";
    return target; 
}

function getTargetWithTwoAtt( lookup, input, att1, att2 ) {
    return getTargetWithOneAtt( lookup, input, att1 ) + att2.name + "=" + att2.value + "&";
}

function getTargetWithThreeAtt( lookup, input, att1, att2, att3 ) {
    return getTargetWithTwoAtt( lookup, input, att1, att2 ) + att3.name + "=" + att3.value + "&";
}
// END NEOGIA ADD

//BEGIN C-LIBRE SPECIFIC FR#
function call_fieldlookupSimple(viewName, formName,viewWidth,viewheight) {   
    var fieldLookup = new fieldLookupSimple();  
    if (! viewWidth) viewWidth = 350;
    if (! viewheight) viewheight = 200;
    fieldLookup.popup(viewName, formName, viewWidth, viewheight);
}

function fieldLookupSimple() {	
	// passing methods
	this.popup = lookup_popup1;
	this.popup2 = lookup_popup2;		
	//initialize lookups
	lookups = [];	
}
//END C-LIBRE SPECIFIC FR#
