// Generated on 2011-06-09 by Alatoel's AlaGen
// DON'T CHANGE THIS FILE AS THE CONTENT IS GENERATED BY ALAGEN

// Form functions
// - Functions used in forms
function add_option(dropdownid,optionid,optionvalue,selected) {
// Input:
//  dropdownid: form id of the dropdown form element
//  optionid: value id of the new dropdown option
//  optionvalue: value of the new dropdown option
//  selected: if true, the new option will be selected
// Description function "add_option":
//  Adds a new option to a form. Used by the popups to add a new dropdown option value (for example when adding a new address)
    var selectBox = document.getElementById(dropdownid);
    selectBox.options[selectBox.options.length] = new Option(optionvalue, optionid, false, selected);

}
function changefile(formname, variablename, path) {
// Input:
//  formname: name of the form that contains the form element that defines the new file
//  variablename: name of the form element that contains the new file
//  path: path to the new file
// Description function "changefile":
//  Changes the file in the link. The user can change the file by selecting a new file from the dropdown or via the popup.
    var newfile=document.forms[formname].elements[variablename].value;
    var filelink=variablename+"_filelink";
    document.getElementById(filelink).href=path+newfile;

}
function changepic(formname, variablename, path, imagewidth) {
// Input:
//  formname: name of the form that contains the image that needs changing
//  variablename: name of the image field
//  path: path to the new image
//  imagewidth: the width of the new image
// Description function "changepic":
//  Changes the image of [variablename]_image to the image defined by [variablename]. Used in image fields where the user can change the image by selecting an image from the dropdown or via the popup.
    var newimage=document.forms[formname].elements[variablename].value;
    var imagevar=variablename+"_image";
    newimage=path+newimage;
    document.images[imagevar].src=newimage;
    document.images[imagevar].alt=newimage;
    document.images[imagevar].title=newimage;
    document.images[imagevar].width=imagewidth;
    var imagelink=variablename+"_imagelink";
    document.getElementById(imagelink).href=newimage;    
}
function change_variablevalue(formname,variablename,gotoscreen,defaultscreen,savescreen) {
// Input:
//  formname: name of the form that is submitted
//  variablename: name of the variable that contains where the screen must go to
//  gotoscreen: add a new screen to the list of screens that need to be processed
//  defaultscreen: 
//  savescreen: 
// Description function "change_variablevalue":
//  Function used during the save process. In case the screen needs to continue to another defined screen, this function will make this happen.
    var alavariable=document.forms[formname].elements[variablename];
    if(alavariable.value==defaultscreen) {
        alavariable.value=gotoscreen;
        document.forms[formname].action = savescreen;
        document.forms[formname].submit();
    } else {
        alavariable.value+='@'+gotoscreen;
        document.forms[formname].action = savescreen;
        document.forms[formname].submit();
    }
}
function check(message, formname, url) {
// Input:
//  message: question to be asked in the popop
//  formname: name of the form that will be submitted in case the user selects yes
//  url: location of the page that will be loaded in case the user selects yes
// Description function "check":
//  Generates a popup with the message to allow the user to confirm to continue. Used to confirm deletions.
    var retval=confirm(message);
    if(retval) {
        gotourl=url;
        document.forms[formname].action = gotourl;
        document.forms[formname].submit();
    }

}
function checkarrow(keycode,formname,formfield) {
// Input:
//  keycode: code of the key pressed
//  formname: name of the form
//  formfield: current field
// Description function "checkarrow":
//  Moves the focus up or down one field
    var arrow_type="";
    var arrow_pressed=false;
    if(keycode==38) {
    <!-- up -->
        arrow_type="up";
        arrow_pressed=true;
    } else if(keycode==39  || keycode==13) {
    <!-- right -->
    } else if(keycode==40) {
    <!-- down -->
        arrow_type="down";
        arrow_pressed=true;
    } else if(keycode==37) {
    <!-- left -->
    } else if(keycode==35) {
        arrow_type="end";
        arrow_pressed=true;
    } else if(keycode==36) {
        arrow_type="home";
        arrow_pressed=true;
    } 
    if(arrow_pressed) { 
        var currenttabindex=document.forms[formname].elements[formfield].tabIndex;
        var formelements=document.forms[formname];
        switch(arrow_type) {
            case("up") : { 
                var prevfieldtab=currenttabindex-1;
                if(prevfieldtab<0) { 
                    gotofield=formfield;
                } else {    
                    for(i=0;i<formelements.length;i++) {
                        var fieldtab=formelements[i].tabIndex;
                        if(fieldtab==prevfieldtab) {
                            gotofield=formelements[i].name;   
                            break;
                        }
                    }
                }    
                break;
            }
            case("down") : {
                var nextfieldtab=currenttabindex+1;
                for(i=0;i<formelements.length;i++) {
                    var fieldtab=formelements[i].tabIndex;
                    if(fieldtab==nextfieldtab) {
                        gotofield=formelements[i].name;   
                        break;
                    }
                }
                break;
            }    
        }       
        if(gotofield!="") {
            document.forms[formname].elements[gotofield].focus();   
        }
    }    
}
function checkrequiredfields(formname, buttontype) {
// Input:
//  formname: name of the form to be checked
//  buttontype: [ text / graphics ]
// Description function "checkrequiredfields":
//  Checks if all required field, defined in the array requiredfields, contain a value.
    var allfilledin=true;
    var REQUIRED_FIELD="&nbsp;";
    var numrequiredfields=requiredfields.length;
	for(i=0;i<requiredfields.length;i++) {
        fieldid=document.getElementById(requiredfields[i]);
        if(document.forms[formname].elements[requiredfields[i]].value!="") {
            document.getElementById(requiredfields[i]+"_label").className="field_label";
            if(document.getElementById("inf_"+requiredfields[i]).innerHTML==REQUIRED_FIELD) {
                document.getElementById("inf_"+requiredfields[i]).innerHTML="&nbsp;";
            } 
        } else {
            allfilledin=false;
            document.getElementById(requiredfields[i]+"_label").className="required_field";
            if(document.getElementById("inf_"+requiredfields[i]).innerHTML==" ") {
                document.getElementById("inf_"+requiredfields[i]).innerHTML=REQUIRED_FIELD;
            }
        }
    } 
    if(buttontype=="text") {
        if(allfilledin) {
            checkvalidform(formname);
        } else {
            disable_button("button1");
        }
    }    
}
function checkrequiredsearchfields(formname, buttontype) {
// Input:
//  formname : id of the form
//  buttontype : text / graphics
// Description function "checkrequiredsearchfields":
//  checks if all required fields are filled in for a search screen.
    var allfilledin=true;
    for(i=0;i<requiredfields.length;i++) {
        requiredfield=document.getElementById(requiredfields[i]);
        if(requiredfield.value=="") {
            allfilledin=false;
        }
    } 
    if(buttontype=="text") {
        if(allfilledin) {
            enable_button("button1");
        } else {
            disable_button("button1");
        }
    }    

}
function checkvalidform(formname) {
// Input:
//  formname: name of the form to be checked
// Description function "checkvalidform":
//  Checks if there are no error messages (inf_ values) on the form
  var inputfields = document.getElementsByTagName("input");
  var validform=true;
  for (var i = 0; i < inputfields.length; ++i) {
    var fieldname=inputfields[i].id;
    if(typeof document.forms[formname].elements[fieldname] != 'undefined') {
        if(document.getElementById("inf_"+fieldname)!= null) {
            var fieldvalue=document.getElementById("inf_"+fieldname).innerHTML;
            if(fieldvalue == ' ' || fieldvalue == '' || fieldvalue == '&nbsp;') {
            } else {
                disable_button("button1");
                validform=false;
            }    
        }
    }    
  }   
  if(validform) { if(document.getElementById("button1")) { enable_button("button1"); } }
}
function delete_option(dropdownid,optionid) {
// Input:
//  dropdownid: id of the dropdown form element
//  optionid: value id of the option that needs to be removed
// Description function "delete_option":
//  Removes an option from a dropdown form element.
    var dropdownBox = document.getElementById(dropdownid);
    var i;
    for (i = dropdownBox.length - 1; i>=0; i--) {
        if (dropdownBox.options[i].value==optionid) {
            dropdownBox.remove(i);
        }
    }

}
function disableEnterKey(e) {
// Input:
//  
// Description function "disableEnterKey":
//  Prevents saving of forms when the user presses the enter key
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     
     return (key != 13);
}
function disable_button(buttonname) {
// Input:
//  buttonname: id of the button that needs to be disabled.
// Description function "disable_button":
//  Disables a button on the form
    if(document.getElementById(buttonname)!=='undefined') {
        document.getElementById(buttonname).disabled=true;
        document.getElementById(buttonname).className= "formbuttondisabled";
    }
}
function enable_button(buttonname) {
// Input:
//  buttonname: id of a button on a form
// Description function "enable_button":
//  Enables the button on a form.
    if(document.getElementById(buttonname)!=='undefined') {
        document.getElementById(buttonname).disabled=false;
        document.getElementById(buttonname).className="formbutton";
    }
}
function formsubmit(formname) {
// Input:
//  formname: name of the form to be submitted
// Description function "formsubmit":
//  Submits form [formname]
    document.forms[formname].submit();

}
function formsubmitgoto(formname, gotourl) {
// Input:
//  formname: form to be submitted
//  gotourl: url to be loaded
// Description function "formsubmitgoto":
//  Submits the form and loads [gotourl]
    document.forms[formname].action=gotourl;    
    document.forms[formname].submit();
}
function popup(rooturl,type, value) {
// Input:
//  rooturl: path to 'alaweb_popup.php'
//  type: type of popup (defined in 'alaweb_popup.php')
//     f = load file defined by $_REQUEST[f]
//     i = display image defined by $_REQUEST[i]
//     t = display text defined by $_REQUEST[t]
// Description function "popup":
//  Generates a popup
    var href=rooturl+'alaweb_popup.php?'+type+"="+value;
    if(type=="f") {
        var title="Upload";
        var alapopup=window.open(href, title, "width=400,height=200,scrollbars=yes,resizable=yes,top=200,left=400");
        alapopup.focus();
    } else if(type=="i") {
        var title="Upload";
        var alapopup=window.open(href, title, "width=400,height=200,scrollbars=yes,resizable=yes,top=200,left=400");
        alapopup.focus();            
    } else {
        var title="Text";
        var alapopup=window.open(href, title, "width=400,height=200,scrollbars=yes,resizable=yes,top=200,left=400");
        alapopup.focus();
    }
}
function printpage(rooturl, completeurl) {
// Input:
//  rooturl: path to the file 'alaweb_print.php' 
//  completeurl: 
// Description function "printpage":
//  Generates a popup defined in 'alaweb_print'.
    var printurl=rooturl+"alaweb_print.php";
    window.open(printurl, "Printpage", "width=1000,height=560,top=600,scrollbars=yes,resizable=yes");

}
function selectcheckboxvalue(variablename, url) {
// Input:
//  variablename: id of the form element containing the checkbox
//  url: url where the checkbox value needs to be appended to and that will be loaded
// Description function "selectcheckboxvalue":
//  Reloads [url] with the value of the checkbox defined by [variablename]
    var cbobject=document.getElementById(variablename);
    if(cbobject.checked==1) {
    	window.location.href=url+"&"+variablename+"="+1;
    } else {
	    window.location.href=url+"&"+variablename+"="+0;
    }

}
function selectdropdownvalue(variablename, gotourl) {
// Input:
//  variablename: id of the form element that contains the dropdown
//  gotourl: 
// Description function "selectdropdownvalue":
//  Passes the value of a dropdown to the url and reloads the page.
    var dpobject=document.getElementById(variablename);
    selInd = dpobject.selectedIndex; 
    // get value of the selected option
    value = dpobject.options[selInd].value;
    if(value=="NULL") {
    	window.location.href=gotourl;
    } else if(value=="") {
        window.location.href=gotourl+"&"+variablename+"=empty";
    } else {
	    window.location.href=gotourl+"&"+variablename+"="+value;
    }
}
function selectvalue(variablename, url) {
// Input:
//  variablename: id of the form element 
//  url: page that will be loaded with the value of [variablename] appended
// Description function "selectvalue":
//  Loads page [url] with the value of [variablename] appended to the url.
    // grab index number of the selected option
    var value=document.getElementById(variablename).value;
    var delimiter="?";
    if(url.indexOf("?")!=-1) { var delimiter='&'; }
    window.location.href=url+delimiter+variablename+"="+value;
}

