// Copyright 2000 - 2001 Overhaul Search, Inc. ALL RIGHTS RESERVED

////////////////////////////
// General
////////////////////////////

var content_change = false;

function winOpen(url,name)
{
    // Window vars
    // Size
    var win_height = Math.min(screen.availHeight - 100, 720);
    var win_width = Math.min(screen.availWidth - 50, 720);
    // Position (center)
    var win_position_x = (screen.availWidth - win_width) / 2;
    var win_position_y = (screen.availHeight - win_height) / 4;
    var win = window.open(url,name,'toolbar=1,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width='+win_width+',height='+win_height+',left='+win_position_x+',top='+win_position_y);
    win.focus();
}

// Select or deselect all of the checkboxes
function selectAll(true_false,start,finish) {
    var f = document.rfqform;
    if(finish == 'all')
    {
        var finish = f.elements.length;
    }
    for(var i = start; i < finish; i++)
    {
        if(f.elements[i].type == "checkbox")
        {
            f.elements[i].checked = true_false;
        }
    }
}

function checkboxToggle(form)
{
	with (form)
	{
		var true_false = all.checked;
		for(var i = 0; i < elements.length; i++)
		{
			if(elements[i].type == "checkbox")
			{
				elements[i].checked = true_false;
			}
		}
	}
}

// Online Help
function helpWin(tab,subtab)
{
    var url = '/home.php?tab=help&subtab=view&tab_help=' + tab + '&subtab_help=' + subtab;
    /*if(help_win)
    {
        help_win.location = url;
    }else{*/
        var help_win = open(url,'help','height=550,width=300,scrollbars=yes,resizable=yes,location=no,toolbar=yes,status=yes');
    /*}*/
    help_win.focus();
}

// Update the phone and fax country codes based on the country selected
function countryIDUpdate(f){
    var i = f.country_id.selectedIndex;
    var code = f.country_id.options[i].value;
    code_array = code.split(':');
    f.phone_country_code.value = code_array[1];
    f.fax_country_code.value = code_array[1];
}

// Serve the user a dialog for their email address to send them a new password.
function loginPassword(){
    var email = prompt("Please enter your email address. A new password will be emailed to you.","");
    if(email != "" && email != null){location = 'home.php?tab=login&subtab=password&email=' + escape(email);}
}

function confirmClose(ok,cancel)
{
    if(content_change)
    {
        if(!confirm('You have made changes to the data. Are you sure you want to discard the changes?\r\rClick \"Ok\" to discard changes and close the window.\rClick \"Cancel\" to return to the form.'))
        {
            eval(cancel);
            return;
        }
    }
    eval(ok);
}

///////////////////////
// Directory
///////////////////////

// Pop up a company window
function companyWin(url) {
    winOpen(url,"company");
}

// Pop up a company window
function companyView(cid) {
    winOpen("/home.php?tab=directory&subtab=view&company_id=" + cid,"company" + cid);
}

// Directory index letter click
function directoryLetter(letter){
    document.searchform.search.value = letter;
    document.searchform.submit();
}

// Toggle preferred vendor check boxes
var vendor_add_array = new Array();
var vendor_delete_array = new Array();
function directoryTogglePreferred(company_id)
{
    var f = eval("document.editform.v" + company_id);
    if(f.checked)
    {
        // add to add_array and delete from delete array
        vendor_add_array[vendor_add_array.length] = company_id;
        for(var i = 0; i < vendor_delete_array.length; i ++)
        {
            if(company_id == vendor_delete_array[i]) vendor_delete_array[i] = 0;
        }
    }else{
        // add to delete array and delete from add array
        vendor_delete_array[vendor_delete_array.length] = company_id;
        for(var i = 0; i < vendor_add_array.length; i ++)
        {
            if(company_id == vendor_add_array[i]) vendor_add_array[i] = 0;
        }
    }
    document.editform.add_list.value = vendor_add_array.toString();
    document.editform.delete_list.value = vendor_delete_array.toString();
}

///////////////
// RFQ
///////////////

// Pop up a rfq window
function rfqView(rfq_id) {
    var url = "/air/rfq/view?rfq_id=" + rfq_id;
    winOpen(url,"rfq" + rfq_id);
}

// Add a comment to an RFQ
function rfqCommentAdd() {
    var current_comment = document.rfqform.rfq_comment.value;
    var comment_on = document.rfqform.add_comment.checked;
    if(comment_on)
    {
        var doc = "<html><head><title>Add a comment to your RFQ</title>";
        doc += "</head><body bgcolor=white><div align=center><form name=cform>";
        doc += "<textarea rows=10 cols=40 name=comment wrap=virtual>" + current_comment + "</textarea><br>";
        doc += "<input type=button value=\"Add Comment\" onclick=\"opener.document.rfqform.rfq_comment.value=document.cform.comment.value;self.close();\"> ";
        doc += "<input type=button value=\"No Comment\" onclick=\"self.close();\">";
        doc += "</form></div></body></html>";
        var win = open('','rfqcomment','height=260,width=400,scrollbars=yes,resizable=yes');
        win.document.open();
        win.document.writeln(doc);
        win.document.close();
        win.focus();
    }
}

function repairStationView(company_id)
{
    var url = '/home/about/location?cid=' + company_id;
    var win = open(url,'co'+company_id,'height=300,width=300,scrollbars=no,resizable=no');
    win.focus();
}

////////////////////
// Work orders
////////////////////

// Pop up a work order window
function woView(woid)
{
    if(woid == 0)
    {
        winOpen("/home.php?tab=work_order&subtab=create");
    }else{
        winOpen("/home.php?tab=work_order&subtab=edit&act=view&work_order_id=" + woid,"wo" + woid);
    }
}

// Confirm deletion of a work order
function woConfirmDelete(woid)
{
    if(confirm('Are you sure you want to delete work order number ' + woid + '?'))
    {
        location = '/home.php?tab=work_order&subtab=edit&act=delete&work_order_id=' + woid;
    }
}

// Pop up a work order window with default values set from the RFQ
function woCreateFromQuote(rfq_id) {
    winOpen("/home.php?tab=work_order&subtab=create&act=create_from_rfq&rfq_id=" + rfq_id,"wo" + rfq_id);
}

// Enter a new company from the select box
function woCompanySelectOther(f,woid,field_name)
{
    var i = f.selectedIndex;
    if(f.options[i].value == 0)
    {
        woCompanySelectForm(woid,field_name)
    }
}


// Enter a new company
function woCompanySelectForm(woid,field_name)
{
    var url = "/home.php?tab=work_order&subtab=edit&act=customer_search&work_order_id=" + woid + "&field_name=" + field_name;
    var win = window.open(url,"customer_search","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=350,height=400");
    win.focus();
}

function woSelectCompany(company_id,cust_name,city,field_name)
{
    f = eval("opener.document.editform." + field_name);
    with(f)
    {
        var i = options.length -1;
        options[i].value = company_id;
        options[i].text = cust_name + " (" + city + ")";
        options[i].selected = true;
    }
    self.close();
}

function woStatusChange(work_order_item_id,key,stage)
{
    // Quality
    if(key == 3)
    {
        var f = eval('document.editform.s' + key + '_' + work_order_item_id);
        var message = '';//escape('Would you like to attach a tag to this work order? Select a template (optional) and click "Create Tag". Otherwise click "Cancel".');
        if(document.editform.get8130)
        {
            if(document.editform.get8130.checked)
            {
                var prompt_for_tag = true;
            }else{
                var prompt_for_tag = false;
            }
        }else{
            var prompt_for_tag = false;
        }
        if(f.type == 'checkbox')
        {
            // Checkbox
            if(f.checked && prompt_for_tag)
            {
                winOpen('/home.php?tab=work_order&subtab=8130&act=work_order_dialog&from=work_order_list&wo_item_id='+work_order_item_id+'&message='+message,'doc8130'+work_order_item_id);
            }
        }else{
            // Text Field
            if(f.value > stage && prompt_for_tag)
            {
                winOpen('/home.php?tab=work_order&subtab=8130&act=work_order_dialog&from=work_order_list&wo_item_id='+work_order_item_id+'&message='+message,'doc8130'+work_order_item_id);
            }
        }
    }
    // Shipped
    if(key == 4)
    {
        var f = eval('document.editform.s' + key + '_' + work_order_item_id);
        if(document.editform.getShipping)
        {
            if(document.editform.getShipping.checked)
            {
                var prompt_for_shipping = true;
            }else{
                var prompt_for_shipping = false;
            }
        }else{
            var prompt_for_shipping = true;
        }
        if(f.type == 'checkbox' && prompt_for_shipping)
        {
            // Checkbox
            if(f.checked)
            {
                var url = '/home.php?tab=work_order&subtab=status&act=shipping_form&wo_item_id='+work_order_item_id+'&quantity=1';
                var win = window.open(url,"shipping","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=220");
                win.focus();
            }
        }else{
            // Text Field
            if(f.value > stage && prompt_for_shipping)
            {
                var url = '/home.php?tab=work_order&subtab=status&act=shipping_form&wo_item_id='+work_order_item_id+'&quantity=1';
                var win = window.open(url,"shipping","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=220");
                win.focus();
            }
        }
    }
}

function woCommentView(woid,comment_id)
{
    var url = "/home.php?tab=work_order&subtab=comment&act=edit&comment_id=" + comment_id + "&work_order_id=" + woid;
    var win = window.open(url,"comment" + comment_id,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=450,height=300");
    win.focus();
}

// Pop up an 8130 dialog
function woAttach8130(woid)
{
    winOpen("/home.php?tab=work_order&subtab=8130&act=work_order_dialog&work_order_id=" + woid + "&from=work_order","wo8130" + woid);
}

///////////////////////
// Docs
///////////////////////

// Enter a new company from the select box
function docCompanySelect(f,field_name)
{
    var i = f.selectedIndex;
    if(f.options[i].value == 0)
    {
        docCompanySelectForm(field_name)
    }
}


// Enter a new company
function docCompanySelectForm(field_name)
{
    var url = "/home.php?tab=work_order&subtab=8130&act=customer_search&field_name=" + field_name;
    var win = window.open(url,"customer_search","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=350,height=400");
    win.focus();
}

// Select a company
function docSelectCompany(company_id,cust_name,city,field_name)
{
    f = eval("opener.document.editform." + field_name);
    with(f)
    {
        var i = options.length -1;
        options[i].value = company_id;
        options[i].text = cust_name + " (" + city + ")";
        options[i].selected = true;
    }
    self.close();
}

function docConfirmSupersede(doc_id)
{
    if(confirm('Supersede this document?\rOnce you supersede a document, you can not restore it.\rClick Ok to confirm that you would like to supersede this document.'))
    {
        location = '/home.php?tab=work_order&subtab=8130&act=supersede&doc_id=' + doc_id;
    }
}

function docTemplateUnset(doc_id,name)
{
    if(confirm('Remove '+name+' as a template?\rClick Ok to confirm that you would like to delete this template. (The document itself will not be deleted.)'))
    {
        location = '/home.php?tab=work_order&subtab=8130&act=delete_template&doc_id=' + doc_id;
    }
}

function docTemplateSet(doc_id)
{
    var template_name = prompt('Enter the name for the template. You will choose the template by it\'s name when you create a new document.','');
    if(name.length > 0)
    {
        location = '/home.php?tab=work_order&subtab=8130&act=make_template&doc_id=' + doc_id + '&template_name=' + escape(template_name);
    }
}

function docConfirmPublish(doc_id)
{
    if(confirm('Publish Document OSI-'+doc_id+'?\rOnce a document is published, it can not be edited.\rClick Ok to confirm that you would like to publish this document.'))
        return true;
    return false;
}

function docPublish(doc_id)
{
    if(docConfirmPublish(doc_id))
    {
        location = '/home.php?tab=work_order&subtab=8130&act=publish&doc_id=' + doc_id;
    }
}

///////////////////////
// Password
///////////////////////

function password()
{
    pass_win = open('/admin/password','pw','height=200,width=300,scrollbars=no,resizable=yes,location=no,toolbar=no,status=yes');
    pass_win.focus();
}


