﻿function showCustomModalDialog(strURL, strWindowFormat, bShowScrollBars)
{
	//window.open(strURL, 'myModalDialogWindow');
	if (strWindowFormat=='')
	{
		strWindowFormat="dialogHeight:400px;dialogWidth:600px";
	}
	strWindowFormat = strWindowFormat+";status:off";
	strWindowFormat = strWindowFormat+";help:off";
	if (bShowScrollBars = 'true')
	{
		strWindowFormat = strWindowFormat+";scroll:on";
	}
	else
	{
		strWindowFormat = strWindowFormat+";scroll:off";
	}
	return showModalDialog(strURL, '', strWindowFormat);
}

function showYesNoDialog(strURL)
{
	// window.open(strURL, 'myYesNoWindow');
	strWindowFormat="dialogHeight:250px;dialogWidth:350px";
	//switch for status bar
	strWindowFormat = strWindowFormat+";status:off";
	//switch for scroll bars
	strWindowFormat = strWindowFormat+";scroll:off"
	//switch for help icon on the windows tool bar
	strWindowFormat = strWindowFormat+";help:off"

	var WindowReturnData = '';
	WindowReturnData=showModalDialog(strURL, '', strWindowFormat);
	return WindowReturnData;
}

function reloadUserView(UserID)
{
	window.location.href='view.mth?userid='+UserID;
}

function LaunchProductLookup()
{
    var URL = "ProductLookup.mth";
    var searchstring = document.getElementById("productcode");
    if (searchstring)
    {
        //only mimimic search when it looks like an account number
        if (searchstring.value.length > 0 && searchstring.value.length < 6)
        {
            //mimic search
            var SearchURL = "?a.convertto.productcode=&a.searchtermtype.productcode=prefix&a.searchfieldpath.productcode=&a.searchfieldname.productcode=productcode&a.searchdeclaringtype.productcode=&a.sqlparamidx.productcode=&a.sqltype.productcode=&a.validateas.productcode=&a.searchfieldvalue.productcode=" + searchstring.value + "&a.convertto.pldescription=&a.searchtermtype.pldescription=prefix&a.searchfieldpath.pldescription=&a.searchfieldname.pldescription=pldescription&a.searchdeclaringtype.pldescription=&a.sqlparamidx.pldescription=&a.sqltype.pldescription=&a.validateas.pldescription=&a.searchfieldvalue.pldescription=&pagestate.search.a=%26a.pagesize%3D10%26a.firstrow%3D0%26a.sortfield0%3D%5CProductcode%26a.sortdirection0%3Dascending%26a.rowselections%3D%3Crowselections%2F%3E&a.searchsearch=Search&a.rowcount=349&a.firstrow=0&a.pagesize=10&pagestate.datapager.a=%26a.pagesize%3D10%26a.firstrow%3D0%26a.sortfield0%3D%5CProductcode%26a.sortdirection0%3Dascending%26a.rowselections%3D%3Crowselections%2F%3E&a.submitinformation=";
            URL = URL + SearchURL;
        }
    } 
	var data = showCustomModalDialog(URL, 'dialogHeight:500px;dialogWidth:500px;resizable:yes', true);
	if (data)
    {
        document.getElementById("productcode").value = data;
        ajaxcall1();
    }  
}

function JackMaintenanceLoad()
{
    CalculateMidCapacityValues();
    ajaxcall1();
}

function LaunchCustomerLookup()
{
    ajaxCustomerCancelSearch();
    var URL = "CustomerLookup.mth";
    var searchstring = document.getElementById("AccCode");
    if (searchstring)
    {
        //only mimimic search when it looks like an account number
        if (searchstring.value.length > 0 && searchstring.value.length < 6)
        {
            //mimic search
            var SearchURL = "?a.searchtermtype.accountnumberstring=like&a.searchfieldpath.accountnumberstring=&a.searchfieldname.accountnumberstring=accountnumberstring&a.searchdeclaringtype.accountnumberstring=&a.sqlparamidx.accountnumberstring=&a.sqltype.accountnumberstring=&a.validateas.accountnumberstring=&a.searchfieldvalue.accountnumberstring=" + searchstring.value + "%25&a.searchtermtype.dealername=like&a.searchfieldpath.dealername=&a.searchfieldname.dealername=dealername&a.searchdeclaringtype.dealername=&a.sqlparamidx.dealername=&a.sqltype.dealername=&a.validateas.dealername=&a.searchfieldvalue.dealername=&pagestate.search.a=%26a.pagesize%3D10%26a.firstrow%3D0%26a.sortfield0%3D%5CAccountNumber%26a.sortdirection0%3Dascending%26a.rowselections%3D%3Crowselections%2F%3E&a.searchsearch=Search&a.rowcount=7576&a.firstrow=0&a.pagesize=10&pagestate.datapager.a=%26a.pagesize%3D10%26a.firstrow%3D0%26a.sortfield0%3D%5CAccountNumber%26a.sortdirection0%3Dascending%26a.rowselections%3D%3Crowselections%2F%3E&a.submitinformation=";
            URL = URL + SearchURL;
        }
    }
    var data = showCustomModalDialog(URL, 'dialogHeight:650px;dialogWidth:650px;resizable:yes', true); 
    if (data)
    {
        var values=data.split('~', 4);
        document.getElementById("customertypeid").value = values[0];
        document.getElementById("dealerid").value = values[1];
        document.getElementById("AccCode").value = values[2];
        if (values.length > 3) {document.getElementById("a.edit.customerphonenumber").value = values[3];}
        ajaxCustomerCancelSearch();
    }   
}

function LaunchCustomerInsert()
{
    var URL = "CustomerAdd.mth";
    var data = showCustomModalDialog(URL, 'dialogHeight:400px;dialogWidth:400px;resizable:yes', true);    
    
    //new customer added!
    if (data)
    {
        var values=data.split('~', 4);
        document.getElementById("customertypeid").value = values[0];
        document.getElementById("customeraddressid").value = values[1];
        document.getElementById("AccCode").value = values[2];
        if (values.length > 3) {document.getElementById("a.edit.customerphonenumber").value = values[3];}
    }
}

function CalculateQuotePrice()
{
    // automatically calculate the quote price using the formula
    // SA Cost Price/1-GM %, set to blank if cost price or % is not a number
    var SealeyGMPercentage = document.getElementById("a.edit.sealeygmpercentage").value;
    var CostPrice          = document.getElementById("a.edit.sacostprice").value;
    var QuotePrice         = Number(CostPrice)/(1-(Number(SealeyGMPercentage)/100));
    
    // make sure the percentage and cost price fields to two decimals and set them
    // back into the fields, eg. 123.45678*100 = 12345.678 = (as int) 12345/100 = 123.45
    SealeyGMPercentage = parseInt(Number(SealeyGMPercentage)*100)/100;
    document.getElementById("a.edit.sealeygmpercentage").value = SealeyGMPercentage;
    CostPrice = parseInt(Number(CostPrice)*100)/100;
    document.getElementById("a.edit.sacostprice").value = CostPrice;
    
    // make sure the user doesn't enter 100% or more
    if( Number(SealeyGMPercentage) > 99 )
    {
        alert('100% Markup Not Allowed');
        
        // in order to stay in the % field we need to set the focus elsewhere and then back again
        // it appears you can't set focus to a field you are already in
        SetFocus("a.edit.sajobnumber");
        SetFocus("a.edit.sealeygmpercentage");
        return;
    }
    
    // check to see if we have NAN
    if(isNaN(Number(SealeyGMPercentage)) || isNaN(Number(CostPrice)))
    {
        // if not a number then set to blank
        document.getElementById("a.edit.quoteprice").value = '';
        return;
    }   
    // round and set the calculated value
    QuotePrice = Math.round(Number(QuotePrice)*100)/100;
    document.getElementById("a.edit.quoteprice").value = QuotePrice;
}

function CalculateMidCapacityValues()
{
    var TestPeriod = document.getElementById("a.edit.testperiod").value;
    var MaxCapacity = document.getElementById("a.edit.maxcapacity").value;
    var MaxCapacityDrop = document.getElementById("a.edit.maxcapacitydrop").value;
    var MidCapacity = document.getElementById("a.edit.midcapacity").value;
    var MidCapacityDrop = document.getElementById("a.edit.midcapacitydrop").value;
    
    var MaxFault = false;
    var MinFault = false;
    if (TestPeriod && MaxCapacityDrop)
    {
        var Result = MaxCapacityDrop * (10/TestPeriod);
        MaxFault = (Result > 5); 
    }
   
    if (MaxCapacity)
    {
        var MidCapacity = document.getElementById("midcapacity");
        MidCapacity.value = parseInt(MaxCapacity/2);
    }
    
     if (TestPeriod && MidCapacityDrop)
    {
        var Result = MidCapacityDrop * (10/TestPeriod);
        MinFault = (Result > 5);   
    }
   
    SetMinMaxDropValues(MaxFault, MinFault);

}

function SetMinMaxDropValues(MaxFault, MinFault)
{
    var MaxCheckLabel = document.getElementById("maxcapacityfaultlabel");
    var MaxCheckValue = document.getElementById("maxcapacityfault");
    var MidCheckLabel = document.getElementById("midcapacityfaultlabel");
    var MinCheckValue = document.getElementById("midcapacityfault");

    if (MaxFault)
    {    
        MaxCheckLabel.innerHTML = " Rapid";
        MaxCheckValue.checked = true;
    }
    else
    {
        MaxCheckLabel.innerHTML = "";
        MaxCheckValue.checked = false; 
    }
    
    
    if (MinFault)
    {
        MidCheckLabel.innerHTML = " Rapid";
        MinCheckValue.checked = true;
    }
    else
    {
        MidCheckLabel.innerHTML = "";
        MinCheckValue.checked = false;
    }
    
    if (MinFault || MaxFault)
    {
         var OutComeSelect=document.getElementById("a.edit.jacktestoutcomeid")
         //set to "send repair agent"
         OutComeSelect.selectedIndex=0;
    }
}

function showProgress(ElementToHide, ElementToShow)
{
	document.getElementById(ElementToHide).style.display='none';
	document.getElementById(ElementToShow).style.display='block';
}

function deleteUser(UserID)
{
	var url = 'Delete.mth?UserID='+UserID;
	var data = showYesNoDialog(url);
	if (data!='NO')
	{
		reloadUserView(UserID);
	}
}

function showLetter(LetterID)
{
	window.open('ViewLetter.mth?jacktestletterid=' + LetterID, 'myLetter');
}

function SetFocus(ControlName)
{
    document.getElementById(ControlName).focus();
}