﻿// JScript File

/* =============================================== Leads & Contacts ===================================================*/
function GoToContact(pContactID) 
{
  llocation = lAppPath+"/Leads_And_Contacts/Common/Index.aspx?RecID="+pContactID;
  window.parent.location.href=llocation;
}

function GoToFindContact(pEmail)
{
    if (pEmail) 
    {
         llocation = lAppPath+"/Leads_And_Contacts/Common/Find.aspx?EmailList=T";
    }
    else 
    {
        llocation = lAppPath+"/Leads_And_Contacts/Common/Find.aspx";
    }
  window.parent.location.href=llocation;
}

function GoToContactIndex(pContactID,pFrom) 
{
  if (pFrom != null)
  {
     llocation = lAppPath+"/Leads_And_Contacts/Common/Index.aspx?RecID="+pContactID+"&FROM="+pFrom;
  } 
  else 
  {
    llocation = lAppPath+"/Leads_And_Contacts/Common/Index.aspx?RecID="+pContactID;
  }
  window.parent.location.href=llocation;
}


function GoToContactOtherAddress(pAddressID) 
{
  llocation = lAppPath+"/Leads_And_Contacts/Common/Other_Addresses.aspx?ThisAddressID="+pAddressID;
  window.parent.location.href=llocation;
}


function GoToHistory(pHistoryID) 
{
  llocation = lAppPath+"/Leads_And_Contacts/Contacts/History/Contact_History.aspx?CHID="+pHistoryID;
  window.parent.location.href=llocation;
}


function GoToMyLead(pContactID) 
{
  llocation = lAppPath+"/Leads_And_Contacts/Leads/MyLeads.aspx?RecID="+pContactID;
  window.parent.location.href=llocation;
}

/* =============================================== Opportunity===================================================*/

function GoToOpportunity(pOppID,pFrom) 
{
 if ( pFrom != null) 
 {
  llocation = lAppPath+"/Leads_And_Contacts/Opportunities/Index.aspx?OPID="+pOppID+"&FROM="+pFrom;
 } 
 else 
 {
  llocation = lAppPath+"/Leads_And_Contacts/Opportunities/Index.aspx?OPID="+pOppID;
 }
  window.location.href=llocation;
}

/* =============================================== Alerts ===================================================*/

function GoToAlert(pAlertID,pParentTable,From) 
{
    if (pParentTable=="Opportunities") 
    {
        llocation = lAppPath+"/Leads_And_Contacts/Opportunities/Index.aspx?OPID="+pAlertID+"&FROM="+From;
    }
    else 
    {
      llocation = lAppPath+"/Followup.aspx?CHID="+pAlertID+"&FROM="+From;
    }
    
    window.parent.location.href=llocation;
}

/* =============================================== Estimating ===================================================*/

function GoToEstimate(pProjectID, pContactID)
{
    llocation = lAppPath+"/Estimating/Common/View.aspx?EstimateID="+pProjectID+"&ContactID="+pContactID;
    window.location.href=llocation;
}

function ViewContactEstimate(pContactID)
{
    llocation = lAppPath+"/Estimating/Index.aspx?ContactID="+pContactID;
    window.location.href =llocation;
}

function SelectEstimate(pEstimateID)
{
  llocation = lAppPath+"/Estimating/Common/View.aspx?EstimateID="+pEstimateID;
  window.location.href=llocation;

}
function GoToEstimatingTask(pTaskID, pResourceType) 
{

  if (pResourceType == 235)
  {
    llocation = lAppPath+"/Estimating/Common/Assign_Inventory.aspx?TaskID="+pTaskID;
  }
  else if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Estimating/Common/NonInventoryItem.aspx?TaskID="+pTaskID;
  }
  else
  {
    llocation = lAppPath+"/Estimating/Common/Assign_Duties.aspx?TaskID="+pTaskID;
  }
  window.parent.location.href=llocation;
}


function SelectEstimateInventoryItem(pItemID, pLocationID)
{
  llocation = lAppPath+"/Estimating/Common/Assign_Inventory.aspx?InvID="+pItemID+"&LocID="+pLocationID;
  window.location.href=llocation;

}


/* =============================================== Project Management ===================================================*/

function SelectProject(pProjectID)
{
  llocation = lAppPath+"/Project_Management/Common/View.aspx?ProjectID="+pProjectID;
  window.location.href=llocation;

}
function GotoProject(pProjectID) 
{
  llocation = lAppPath+"/Project_Management/Common/View.aspx?ProjectID="+pProjectID;
  window.location.href=llocation;
}



function GoToLabor(pTaskID) 
{
  llocation = lAppPath+"/Project_Management/Common/Assign_Duties.aspx?TaskID="+pTaskID;
  window.parent.location.href=llocation;
}

function Select_PM_InventoryItem(pItemID, pLocationID)
{
  llocation = lAppPath+"/Project_Management/Common/Assign_Inventory.aspx?InvID="+pItemID+"&LocID="+pLocationID;
  window.location.href=llocation;

}

function GoTo_PM_Material(pTaskID, pResourceType) 
{

  if (pResourceType == 235)
  {
    llocation = lAppPath+"/Project_Management/Common/Assign_Inventory.aspx?TaskID="+pTaskID;
  }
  else if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Project_Management/Common/NonInventoryItem.aspx?TaskID="+pTaskID;
  }
  else
  {
    llocation = lAppPath+"/Project_Management/Common/Assign_Inventory.aspx?TaskID="+pTaskID;
  }
  window.parent.location.href=llocation;
  
}


function GoTo_PM_Labor(pTaskID, pResourceType) 
{

  if (pResourceType == 235)
  {
    llocation = lAppPath+"/Project_Management/Common/Assign_Duties.aspx?TaskID="+pTaskID;
  }
  else if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Project_Management/Common/NonInventoryItem.aspx?TaskID="+pTaskID;
  }
  else
  {
    llocation = lAppPath+"/Project_Management/Common/Assign_Duties.aspx?TaskID="+pTaskID;
  }
  window.parent.location.href=llocation;
}

/* =============================================== Project Tracker ===================================================*/
function ViewTotalTime(pTaskID)
{
    lLocation = lAppPath+"/Project_Tracker/TotalTime.aspx?TaskID="+pTaskID;
    window.location.href = lLocation;
}

function SelectNote(pThisNoteID)
{
    window.location.href=lAppPath+"/Project_Tracker/Task_Notes.aspx?NoteID="+pThisNoteID;
}

function AdjustThis(pTaskTimeHistoryID)
{
    lLocation = lAppPath+"/Project_Tracker/Manual_Time_Entry.aspx?AdjustThis="+pTaskTimeHistoryID;
    window.location.href = lLocation;
}
function WorkOnThis(pProjectTaskID)
{
    lLocation = lAppPath+"/Project_Tracker/Manual_Time_Entry.aspx?WorkOnThisID="+pProjectTaskID;
    window.location.href = lLocation;
}

/* =============================================== Service ===================================================*/

function GoToResource(pContactID) 
{
  llocation = lAppPath+"/Service/QuickTicket/Service_Requested.aspx?ContactID="+pContactID;
  window.location.href=llocation;
}

function SelectContact(pContactID)
{
  llocation = lAppPath+"/Service/ServiceHistory.aspx?ContactID="+pContactID;
  window.location.href=llocation;

}
function SelectServiceInventoryItem(pItemID, pLocationID)
{
  llocation = lAppPath+"/Service/QuickTicket/Materials.aspx?InvID="+pItemID+"&LocID="+pLocationID;
  window.location.href=llocation;

}

function GoToServiceMaterials(pTaskID, pResourceType)
{
  if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Service/QuickTicket/NonInventoryItem.aspx?TaskID="+pTaskID;
  }
  else
  {
    llocation = lAppPath+"/Service/QuickTicket/Materials.aspx?TaskID="+pTaskID;
  }
  
  
  window.location.href=llocation;

}
function GoToServiceTask(pTaskID, pResourceType, pRepairExisting) 
{
 
  if (pRepairExisting =="T")
  {
    llocation = lAppPath+"/Service/QuickTicket/Repair_Existing_Confirmation.aspx?TaskID="+pTaskID;

  } else if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Service/QuickTicket/NonInventoryItem.aspx?TaskID="+pTaskID;
  }
  else
  {
    llocation = lAppPath+"/Service/QuickTicket/Assign_Labor.aspx?TaskID="+pTaskID;
  }
  window.parent.location.href=llocation;
}


function GoToTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/CloseTicket.aspx?TicketID="+pTicketID;
  window.location.href=llocation;
  
}
function SelectServiceTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/CloseTicket.aspx?TicketID="+pTicketID;
  window.location.href=llocation;
  

}
function PrintServiceTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/Preview.aspx?TicketID="+pTicketID;
  window.location.href=llocation;
  

}
function EditServiceTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/Service_Requested.aspx?TicketID="+pTicketID;
  window.location.href=llocation;
  

}

function SelectSystemID(SystemID)
{
  llocation = lAppPath+"/Service/Systems.aspx?SysID="+SystemID;
  window.location.href=llocation;

}

function GoToServiceWarranty(pWarrantyID) 
{
  llocation = lAppPath+"/Service/Warranties.aspx?WID="+pWarrantyID;
  window.parent.location.href=llocation;
}


function GoToServiceContact(pContactID) 
{
  llocation = lAppPath+"/Service/Index.aspx?RecID="+pContactID;
  window.parent.location.href=llocation;
}

function GoToServiceCustomer(pContactID) 
{
  llocation = lAppPath+"/Service/QuickTicket/QuickAdd_Customer.aspx?ContactID="+pContactID;
  window.parent.location.href=llocation;
  


function GoToPeriodicMaintenanceItem(pPMID) 
{
  llocation = lAppPath+"/Service/PeriodicMaintenance.aspx?PMID="+pPMID;
  window.location.href=llocation;
}

}

/* =============================================== Billing ===================================================*/

function GotoBill(pBillingID)
{
  llocation = lAppPath+"/Billing/Manual_Billing.aspx?BID="+pBillingID;
  window.location.href=llocation;

}

function GoToBillingContact(pContactID) 
{
  llocation = lAppPath+"/Billing/ManualBilling/Resource_Search.aspx?RecID="+pContactID;
  window.parent.location.href=llocation;
}


function GoToMaterialBilling(pItemID, pLocationID)
{
  llocation = lAppPath+"/Billing/ManualBilling/Materials.aspx?InvID="+pItemID+"&LocID="+pLocationID;
  window.location.href=llocation;

}


function GoToBillingTasks(pTaskID)
{
  llocation = lAppPath+"/Billing/ManualBilling/Materials.aspx?TaskID="+pTaskID;
  window.location.href=llocation;

}

function GoToBillingLabor(pTaskID)
{
  llocation = lAppPath+"/Billing/ManualBilling/Assign_Labor.aspx?TaskID="+pTaskID;
  window.location.href=llocation;

}

function GoToReocurringItem(pReocurrID) 
{
  llocation = lAppPath+"/Service/Reocurring_Billing.aspx?ReoCID="+pReocurrID;
  window.parent.location.href=llocation;
}

function GoToReocurringItemBilling(pReocurrID) 
{
  llocation = lAppPath+"/Billing/Reocurring_Billing.aspx?ReoCID="+pReocurrID;
  window.parent.location.href=llocation;
}

function GoToReocurringContact(pContactID) 
{
  llocation = lAppPath+"/Billing/Reocurring_Billing.aspx?ContactID="+pContactID;
  window.parent.location.href=llocation;
}

function GoToCreditStatusContact(pContactID) 
{
  llocation = lAppPath+"/Billing/Terms_CreditStatus_Update.aspx?ContactID="+pContactID;
  window.parent.location.href=llocation;
}

function GoToWarranty(pWarrantyID) 
{
  llocation = lAppPath+"/Service/Warranties.aspx?WRID="+pWarrantyID;
  window.parent.location.href=llocation;
}

function GoToWarrantyItem(pWarrantyID) 
{
  llocation = lAppPath+"/Billing/Warranties.aspx?WRID="+pWarrantyID;
  window.parent.location.href=llocation;
}

function GoToWarrantyContact(pContactID) 
{
  llocation = lAppPath+"/Billing/Warranties.aspx?ContactID="+pContactID;
  window.parent.location.href=llocation;
}

function ViewBillingItem(pIsReoccuring,pInventoryID,pProjectID,pProjectTaskID,pIsWorkOrder)
{
    if (pIsReoccuring =="T") 
    {
        llocation = lAppPath+"/Service/Reocurring_Billing.aspx?ReoCID="+pProjectTaskID;
        window.parent.location.href=llocation;
    } else 
    {
       if (pIsWorkOrder =="T") 
       {
            if (pInventoryID > 0) 
            {
                llocation = lAppPath+"/Service/QuickTicket/Materials.aspx?TaskID="+pProjectTaskID;
            }
            else
            {
                llocation = lAppPath+"/Service/QuickTicket/Assign_Labor.aspx?TaskID="+pProjectTaskID;
            }
            window.parent.location.href=llocation;
       
       } else 
       {
       
            if (pInventoryID > 0) 
            {
                llocation = lAppPath+"/Project_Management/Common/Assign_Inventory.aspx?TaskID="+pProjectTaskID;
            }
            else
            {
                llocation = lAppPath+"/Project_Management/Common/Assign_Duties.aspx?TaskID="+pProjectTaskID;
            }
            
            window.parent.location.href=llocation;
       }
    
    }
}







/* =============================================== Inventory ===================================================*/

function GoToInventoryItem (pItem_Location)
{
    llocation = lAppPath+"/Inventory/QuickAdd_Inventory.aspx?ItemID="+pItem_Location;
    window.parent.location.href=llocation;
} 


/* =============================================== System Administration ===================================================*/

function GoToLookup(pLookupID) 
{
  llocation = lAppPath+"/Administration/Lookup_Maintenance.aspx?RecID="+pLookupID;
  window.location.href=llocation;
}

function GoToUser(pUserID) 
{
  llocation = lAppPath+"/Administration/System_Users.aspx?UID="+pUserID;
  window.location.href=llocation;
}



function GoToResourceType(pUserID,pResourceTypeID)
{
    window.parent.location.href=lAppPath+"/Administration/UserResourceTypes.aspx?UID="+pUserID+"&RTID="+pResourceTypeID;
}


/* =============================================== Globally Shared ===================================================*/


function sethighlighted(pThisRow)
{
    pThisRow.style.color ="black";
    pThisRow.style.backgroundColor="#50ad4d";
}

function resethighlight(pThisRow)
{
    pThisRow.style.color ="black";
    pThisRow.style.backgroundColor="";
}

function ExpandCollapse(container)
{

    var obj = document.getElementById(container + '_collapsible');
    if (obj == null) 
    {
        obj = document.getElementById(container);
    }
    var img = document.getElementById(container + '_button');
    if (obj!=null)
    {   
        
        obj.style.display = (obj.style.display=="block") ? "none" : "block";
        lThisStyle =  obj.style.display;
        collapse =lAppPath+'/Images/ToggleUp.gif';
        expand = lAppPath+'/Images/ToggleDown.gif';
     
       if (img!= null) 
        {
            img.src = (lThisStyle=="block")? collapse : expand;
        }

        
    }
}



 function SelectAllCheckboxes(spanChk)
 {

   // Added as ASPX uses SPAN for checkbox
   var oItem = spanChk.children;
   var theBox= (spanChk.type=="checkbox") ? 
        spanChk : spanChk.children.item[0];
   xState=theBox.checked;
   elm=theBox.form.elements;

   for(i=0;i<elm.length;i++)
     if(elm[i].type=="checkbox" && 
              elm[i].id!=theBox.id)
     {
       //elm[i].click();
       if(elm[i].checked!=xState)
         elm[i].click();
       //elm[i].checked=xState;
     }
 }
 
 function Recalculate()
{
    lCost = document.getElementById("ctl00_MainContent_PH_edt_Cost").value;
    lMarkup = document.getElementById("ctl00_MainContent_PH_edt_Markup").value;
    lQuantity = document.getElementById("ctl00_MainContent_PH_edt_Quantity").value;
    lPrice = document.getElementById("ctl00_MainContent_PH_edt_Price").value;
    lCost = lCost.replace(",","");
    lPrice = lPrice.replace(",","");
    if (lMarkup =="") 
    {
        if (lPrice =="") 
        {
            document.getElementById("ctl00_MainContent_PH_edt_Price").value = formatDecimal(lCost);
        }
        if (lQuantity != "") 
        {
            lPrice_F = parseFloat(lPrice);
            lQuantity_Ft = parseFloat(lQuantity);
            lTotalPrice_F = ( lPrice_F * lQuantity_Ft );
            
            document.getElementById("ctl00_MainContent_PH_edt_BillableAmount").value = formatDecimal(lTotalPrice_F)  ;
        }
    }
    else 
    {
        if (lMarkup != "") 
        {

           document.getElementById("ctl00_MainContent_PH_edt_Price").value = CalculatePrice(lCost,lMarkup);
           lPrice = CalculatePrice(lCost,lMarkup);
           lPrice = lPrice.replace(",","");
           if (lQuantity != "") 
           {
            lPrice_F = parseFloat(lPrice);
            lQuantity_Int = parseInt(lQuantity);
            document.getElementById("ctl00_MainContent_PH_edt_BillableAmount").value = formatDecimal( lPrice_F * lQuantity_Int );
           }
           
        }
    }
    
    
}
function PriceChanged()
{
    lCost = document.getElementById("ctl00_MainContent_PH_edt_Cost").value;
    lMarkup = document.getElementById("ctl00_MainContent_PH_edt_Markup").value;
    lQuantity = document.getElementById("ctl00_MainContent_PH_edt_Quantity").value;
    lPrice = document.getElementById("ctl00_MainContent_PH_edt_Price").value;
    lCost = lCost.replace(",","");
    lPrice = lPrice.replace(",","");
    if (lPrice != "") 
    {
       if (lQuantity != "") 
       {
        lPrice_F = parseFloat(lPrice);
        lQuantity_Int = parseFloat(lQuantity);
        document.getElementById("ctl00_MainContent_PH_edt_BillableAmount").value = formatDecimal( lPrice_F * lQuantity_Int );
       }
       
    }
    
    
}

 function formatDecimal(num) 
 {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') +  num + '.' + cents);
}

 function CalculatePrice(pCost,pMarkUp)
{
    
    if (pMarkUp != "" && pCost != "" ) 
    {
       lCost = pCost.replace(",","");
       lMarkup = pMarkUp.replace("%","");
       lCost_F = parseFloat(lCost);
       lMarkup_F = parseFloat(pMarkUp);
       lTotal = lCost_F + ( lCost_F *(lMarkup_F/100) );
       return formatDecimal(lTotal);   
    }
    else
    {
        return "";
    }
    
    
}
function CalculateMarkUp(pCost,pPrice)
{
    
    if (pCost != "" && pPrice != "") 
    {

       lCost = pCost.replace(",","");
       lPrice = pPrice.replace(",","");
       lCost_F = parseFloat(lCost);
       lPrice_F = parseFloat(lPrice);
       lMarkup_F = ((lPrice_F /lCost_F) -1);
       lResult = parseFloat(lMarkup_F)*100;
       return formatDecimal(lResult);
              
    } else 
    {
        return "";
    }
}


function MM_findObj(n, d) { //v4.01
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function GetDirectionsByID(pThisAddressID) 
{
  llocation = lAppPath+"/Service/QuickTicket/MapQuest_Directions_Mapping.aspx?RecID="+pThisAddressID;
  window.open(llocation,"Directions","width=800,height=600,resizable=yes,scrollbars=yes");

}



/* =============================================== Dots Email ===================================================*/
function GoToMessage(pUser_EmailMessage_ID,pFolderName)
{
    llocation = lAppPath+"/3c_DotsMail/View_Email.aspx?MSGID="+pUser_EmailMessage_ID+"&FLDR="+pFolderName;
    window.parent.location.href=llocation;
}


function GoToFolder(pFolderID)
{
    llocation = lAppPath+"/3c_DotsMail/3cDotsMail.aspx?GTFID="+pFolderID;
    window.location.href=llocation;
}

/* =============================================== Dots Reporting ===================================================*/

function PreviewReport(pReportID, pPreviewForm) {

    llocation = lAppPath+"/Reports/"+pPreviewForm+"?RPID="+pReportID
    window.location.href=llocation;
 
}