﻿// 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,pModuleMenuID) 
{
  if (pFrom != null)
  {
     llocation = lAppPath+"/Leads_And_Contacts/Common/Index.aspx?RecID="+pContactID+"&FROM="+pFrom+"&CM="+pModuleMenuID;
  } 
  else 
  {
    llocation = lAppPath+"/Leads_And_Contacts/Common/Index.aspx?RecID="+pContactID+"&CM="+pModuleMenuID;
  }
  window.parent.location.href=llocation;
}


function GoToContactOtherAddress(pAddressID,pModuleMenuID) 
{
  llocation = lAppPath+"/Leads_And_Contacts/Common/Other_Addresses.aspx?ThisAddressID="+pAddressID+"&SMNTM="+pModuleMenuID;
  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;
}

function ChangeContactStatus( pControl) 
{   
   lControlName = pControl.substring(0, pControl.indexOf("ddl_ContactStatus") );
   lContact_Status = document.getElementById(pControl);
   lContact_Status_ID = document.getElementById(pControl).value;
   lContact_ID = document.getElementById(lControlName+"hf_Contact_ID").value;
   lUserID = document.getElementById(lControlName+"hf_User_ID").value;
   lCompany_ID = document.getElementById(lControlName+"hf_Company_ID").value;
   Leads_And_Contacts_RecordViews_DetailAddressCard.ChangeContactStatus(lContact_ID, lContact_Status_ID, lUserID, lCompany_ID);
}

function ChangeStatusReason( pControl) 
{   
   lControlName = pControl.substring(0, pControl.indexOf("ddl_StatusReason") );
   lContact_Status = document.getElementById(pControl);
   lContact_StatusReason_ID = document.getElementById(pControl).value;
   lContact_ID = document.getElementById(lControlName+"hf_Contact_ID").value;
   lUserID = document.getElementById(lControlName+"hf_User_ID").value;
   lCompany_ID = document.getElementById(lControlName+"hf_Company_ID").value;
   Leads_And_Contacts_RecordViews_DetailAddressCard.ChangeContactStatusReason(lContact_ID, lContact_StatusReason_ID, lUserID, lCompany_ID);
}

function PrintThisContact(pContactID, pFrom) 
{
   window.location.href=lAppPath+"/Reports/Print_Preview_Server.aspx?PrintContactID="+pContactID+"&From="+pFrom;
}



/* =============================================== Opportunity===================================================*/

function GoToOpportunity(pOppID,pFrom) 
{
 if ( pFrom != null) 
 {
  llocation = lAppPath+"/Leads_And_Contacts/Opportunities/Index.aspx?OPID="+pOppID+"&CM=1&FROM="+pFrom;
 } 
 else 
 {
  llocation = lAppPath+"/Leads_And_Contacts/Opportunities/Index.aspx?OPID="+pOppID+"&CM=1";
 }
  window.location.href=llocation;
}

/* =============================================== Alerts ===================================================*/

function GoToAlert(pAlertID,pParentTable,From) 
{
    if (pParentTable=="Opportunities") 
    {
        llocation = lAppPath+"/Leads_And_Contacts/Opportunities/Index.aspx?OPID="+pAlertID+"&CM=1&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+"&CM=4&SMNTM=6";
  window.location.href=llocation;

}
function GotoProject(pProjectID) 
{
  llocation = lAppPath+"/Project_Management/Common/View.aspx?ProjectID="+pProjectID+"&CM=4&SMNTM=6";
  window.location.href=llocation;
}



function GoToLabor(pTaskID) 
{
  llocation = lAppPath+"/Project_Management/Common/Assign_Labor.aspx?TaskID="+pTaskID+"&CM=4&SMNTM=3";
  window.parent.location.href=llocation;
}

function Select_PM_InventoryItem(pItemID, pLocationID)
{
  llocation = lAppPath+"/Project_Management/Common/Assign_Inventory.aspx?InvID="+pItemID+"&LocID="+pLocationID+"&CM=4&SMNTM=4";
  window.location.href=llocation;

}

function GoTo_PM_Material(pTaskID, pResourceType) 
{

  if (pResourceType == 235)
  {
    llocation = lAppPath+"/Project_Management/Common/Assign_Inventory.aspx?TaskID="+pTaskID+"&CM=4&SMNTM=4";
  }
  else if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Project_Management/Common/NonInventoryItem.aspx?TaskID="+pTaskID+"&CM=4&SMNTM=5";
  }
  else
  {
    llocation = lAppPath+"/Project_Management/Common/Assign_Inventory.aspx?TaskID="+pTaskID+"&CM=4&SMNTM=4";
  }
  window.parent.location.href=llocation;
  
}


function GoTo_PM_Labor(pTaskID, pResourceType) 
{

  if (pResourceType == 235)
  {
    llocation = lAppPath+"/Project_Management/Common/Assign_Duties.aspx?TaskID="+pTaskID+"&CM=4&SMNTM=3";
  }
  else if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Project_Management/Common/NonInventoryItem.aspx?TaskID="+pTaskID+"&CM=4&SMNTM=3";
  }
  else
  {
    llocation = lAppPath+"/Project_Management/Common/Assign_Duties.aspx?TaskID="+pTaskID+"&CM=4&SMNTM=3";
  }
  window.parent.location.href=llocation;
}

/* =============================================== Project Tracker ===================================================*/
function ViewTotalTime(pTaskID)
{
    lLocation = lAppPath+"/Project_Tracker/TotalTime.aspx?TaskID="+pTaskID+"&CM=5";
    window.location.href = lLocation;
}

function SelectNote(pThisNoteID)
{
    window.location.href=lAppPath+"/Project_Tracker/Task_Notes.aspx?NoteID="+pThisNoteID+"&CM=5";
}

function AdjustThis(pTaskTimeHistoryID)
{
    lLocation = lAppPath+"/Project_Tracker/Manual_Time_Entry.aspx?AdjustThis="+pTaskTimeHistoryID+"&CM=5";
    window.location.href = lLocation;
}
function WorkOnThis(pProjectTaskID)
{
    lLocation = lAppPath+"/Project_Tracker/Manual_Time_Entry.aspx?WorkOnThisID="+pProjectTaskID+"&CM=5";
    window.location.href = lLocation;
}

function EditTasks(pProjectTaskID)
{
    lLocation = lAppPath+"/Project_Tracker/EditTask.aspx?TaskID="+pProjectTaskID+"&CM=5";
    window.location.href = lLocation;
}


function SetReminder (pControl)
{
   
   lControlName = pControl.substring(0, pControl.indexOf("Reminder_on") );
   lreminderstatus = document.getElementById(pControl);
   lStatus = lreminderstatus.value;
   hf_ID = document.getElementById(lControlName+"TaskID");
   lID = hf_ID.value;
   Project_Tracker_Index.SetReminder(lStatus,parseInt(lID));
   
}

function SetReminder_Dashboard (pControl)
{
   
   lControlName = pControl.substring(0, pControl.indexOf("Reminder_on") );
   lreminderstatus = document.getElementById(pControl);
   lStatus = lreminderstatus.value;
   hf_ID = document.getElementById(lControlName+"TaskID");
   lID = hf_ID.value;
   DashBoards_RL_ProjectTracker_Dashboard.SetReminder(lStatus,parseInt(lID));
   
}


function SetPriority (pControl)
{
  
   lControlName = pControl.substring(0, pControl.indexOf("Task_Priority") );
   lPriorityControl = document.getElementById(pControl);
   hf_ID = document.getElementById(lControlName+"TaskID");
   lID = hf_ID.value;
   lPriorityID = lPriorityControl.value;
   Project_Tracker_Index.SetPriority(parseInt(lPriorityID),parseInt(lID));

}

function SetPriority_Dashboard (pControl)
{
  
   lControlName = pControl.substring(0, pControl.indexOf("Task_Priority") );
   lPriorityControl = document.getElementById(pControl);
   hf_ID = document.getElementById(lControlName+"TaskID");
   lID = hf_ID.value;
   lPriorityID = lPriorityControl.value;
   DashBoards_RL_ProjectTracker_Dashboard.SetPriority(parseInt(lPriorityID),parseInt(lID));

}

function AddTimeToThisTask(pProjectTaskID)
{
    lLocation = lAppPath+"/Project_Tracker/Manual_Time_Entry.aspx?WorkOnThisID="+pProjectTaskID+"&CM=5";
    window.location.href = lLocation;
}


/* =============================================== Service ===================================================*/

function GoToResource(pContactID) 
{
  llocation = lAppPath+"/Service/QuickTicket/Service_Requested.aspx?ContactID="+pContactID+"&CM=3&SMNTM=1";
  window.location.href=llocation;
}

function SelectContact(pContactID)
{
  llocation = lAppPath+"/Service/ServiceHistory.aspx?ContactID="+pContactID+"&CM=3";
  window.location.href=llocation;

}
function SelectServiceInventoryItem(pItemID, pLocationID)
{
  llocation = lAppPath+"/Service/QuickTicket/Materials.aspx?InvID="+pItemID+"&LocID="+pLocationID+"&CM=3&SMNTM=4";
  window.location.href=llocation;

}

function GoToServiceMaterials(pTaskID, pResourceType)
{
  if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Service/QuickTicket/NonInventoryItem.aspx?TaskID="+pTaskID+"&CM=3&SMNTM=5";
  }
  else
  {
    llocation = lAppPath+"/Service/QuickTicket/Materials.aspx?TaskID="+pTaskID+"&CM=3&SMNTM=4";
  }
  
  
  window.location.href=llocation;

}
function GoToServiceTask(pTaskID, pResourceType, pRepairExisting) 
{
 
  if (pRepairExisting =="T")
  {
    llocation = lAppPath+"/Service/QuickTicket/Repair_Existing_Confirmation.aspx?TaskID="+pTaskID+"&CM=3&SMNTM=7";

  } else if (pResourceType ==0) 
  {
    llocation = lAppPath+"/Service/QuickTicket/NonInventoryItem.aspx?TaskID="+pTaskID+"&CM=3&SMNTM=5";
  }
  else
  {
    llocation = lAppPath+"/Service/QuickTicket/Assign_Labor.aspx?TaskID="+pTaskID+"&CM=3&SMNTM=3";
  }
  window.parent.location.href=llocation;
}


function GoToTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/CloseTicket.aspx?TicketID="+pTicketID+"&CM=3&SMNTM=9";
  window.location.href=llocation;
  
}
function SelectServiceTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/CloseTicket.aspx?TicketID="+pTicketID+"&CM=3&SMNTM=9";
  window.location.href=llocation;
  

}

function CloseServiceTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/CloseTicket.aspx?TicketID="+pTicketID+"&CM=3&SMNTM=9";
  window.location.href=llocation;
  

}

function PrintServiceTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/Preview.aspx?TicketID="+pTicketID+"&CM=3&SMNTM=8";
  window.location.href=llocation;
  

}
function EditServiceTicket(pTicketID)
{
  llocation = lAppPath+"/Service/QuickTicket/Service_Requested.aspx?TicketID="+pTicketID+"&CM=3&SMNTM=1";
  window.location.href=llocation;
  

}

function SelectSystemID(SystemID)
{
  llocation = lAppPath+"/Service/Systems.aspx?SysID="+SystemID+"&CM=3";
  window.location.href=llocation;

}

function GoToServiceWarranty(pWarrantyID) 
{
  llocation = lAppPath+"/Service/Warranties.aspx?WID="+pWarrantyID+"&CM=3";
  window.parent.location.href=llocation;
}


function GoToServiceContact(pContactID) 
{
  llocation = lAppPath+"/Service/Index.aspx?RecID="+pContactID+"&CM=3";
  window.parent.location.href=llocation;
}

function GoToServiceCustomer(pContactID) 
{
  llocation = lAppPath+"/Service/QuickTicket/QuickAdd_Customer.aspx?ContactID="+pContactID+"&CM=3";
  window.parent.location.href=llocation;
 } 


function GoToPeriodicMaintenanceItem(pPMID) 
{
  llocation = lAppPath+"/Service/PeriodicMaintenance.aspx?PMID="+pPMID+"&CM=3";
  window.location.href=llocation;
}


function AddToServiceRequest() 
{
    lNote = document.getElementById("ctl00_MainContent_PH_hf_ServiceNotes").value;
    if ( lNote != null) 
    {
        lServiceRequest = document.getElementById("ctl00_MainContent_PH_edt_ServicesRequested");
       
        lCurrentText = lServiceRequest.value;
        lCurrentText = lCurrentText+"Service Notes:\r\n"+lNote;
        lServiceRequest.value = lCurrentText;
        
    }
}0

function SetEndTime(pStartTimeValue)
{
    lEndTimeField = document.getElementById("ctl00$MainContent_PH$ddl_EndTime");
    for(index = 0; index < lEndTimeField.length; index++) 
    {
        if(lEndTimeField[index].value == pStartTimeValue)
        lEndTimeField.selectedIndex = index;
   }
}
function SetEndDate(pStartDateValue)
{
    lEndDateField = document.getElementById("ctl00$MainContent_PH$edt_taskEnddate");
    lEndDateField.value = pStartDateValue;
}

function chk_AddSalesTax_onclick(pCheckBox,pZipCode) 
{
    if (pCheckBox.checked) 
    {
       lSalesTax= Service_QuickTicket_CloseTicket.GetSalesTaxThisZip(pZipCode);
        document.getElementById("ctl00$MainContent_PH$edt_SalesTax").value = lSalesTax.value;
    } 
    else 
    {
        document.getElementById("ctl00$MainContent_PH$edt_SalesTax").value ="";
    }

}
function NonBillable(pControl)
{
    if (pControl.checked) 
    {
        if (document.getElementById("ctl00_MainContent_PH_edt_Price").value =="") 
        {
            
            lPrice = igedit_getById("ctl00_MainContent_PH_edt_Price");
            lPrice.setValue(document.getElementById("ctl00_MainContent_PH_hf_Price").value);
            lBillableAmount = igedit_getById("ctl00_MainContent_PH_edt_BillableAmount");
            lBillableAmount.setValue(document.getElementById("ctl00_MainContent_PH_hf_BillableAmount").value);
            document.getElementById("ctl00_MainContent_PH_chk_Taxable").checked= true;             
        } 
        else 
        {
            lPrice = igedit_getById("ctl00_MainContent_PH_edt_Price");
            lPrice.setValue(lPrice.value);
            lBillableAmount = igedit_getById("ctl00_MainContent_PH_edt_BillableAmount");
            lBillableAmount.setValue(lBillableAmount.value);
            document.getElementById("ctl00_MainContent_PH_chk_Taxable").checked= true;      
        }
    }
    else 
    {
       lPrice = igedit_getById("ctl00_MainContent_PH_edt_Price");
       document.getElementById("ctl00_MainContent_PH_hf_Price").value=lPrice.value;
       lPrice.setValue("");
       lBillableAmount = igedit_getById("ctl00_MainContent_PH_edt_BillableAmount");
       document.getElementById("ctl00_MainContent_PH_hf_BillableAmount").value = lBillableAmount.value;
       lBillableAmount.setValue("");
       document.getElementById("ctl00_MainContent_PH_chk_Taxable").checked= false;
    }
}


 function GetMaterialCharge(pItemLocationID) 
 { 
    lInventoryID = document.getElementById("ctl00_MainContent_PH_ddl_Inventory_Item");
    lMaterialsPrice = Service_Reocurring_Billing.GetMaterialCharge(lInventoryID.value, pItemLocationID);
    ledt_MaterialsCharge = igedit_getById("ctl00_MainContent_PH_edt_MaterialsCharge");
    lBillableAmount.setValue(lMaterialsPrice.value);
 }
 
 function GetInventoryItemLocation(pInventoryID)
{
    lOptions = Service_Reocurring_Billing.GetItemLocations( pInventoryID);
    lDropDown = document.getElementById("ctl00_MainContent_PH_ddl_ItemLocations");
    if ( lDropDown != null)
    {   
        // Empty options just in case new drop down is shorter";
        for (var i = 0; i < lDropDown.options.length; i++)
        {
            lDropDown.options[i] = null; 
        } 
        lDropDown.options.length=null; 
        lDropDown.options[0] = new Option('Select',''); 
        lOptionsStr = lOptions.value; 
        lResultItemsArray = lOptionsStr.split('|');

        for (var loop = 0; loop < lResultItemsArray.length -1; loop++) 
        { 
            lOptionStr = lProjectItemsArray[loop];
            lOptionArray = lOptionStr.split('~'); 
            lDropDown.options[loop+1] = new Option(lOptionArray[1],lOptionArray[0]); 
        }
    }
   
    lDescription = Service_Reocurring_Billing.GetInventoryItemDescription( pInventoryID);
    document.getElementById("ctl00_MainContent_PH_edt_InvoiceDescription").value = lDescription; 
    lIsTaxable = Service_Reocurring_Billing.GetInventoryItemIsTaxable( pInventoryID).value;
    document.getElementById("ctl00_MainContent_PH_chk_IsTaxable").checked = (lIsTaxable =='T');
   
    
}

function GetServiceInfo(pInventoryID) 
 { 
    lDescription = Service_Reocurring_Billing.GetInventoryItemDescription(pInventoryID);
    document.getElementById("ctl00_MainContent_PH_edt_InvoiceDescription").value = lDescription.value; 
    lIsTaxable = Service_Reocurring_Billing.GetInventoryItemIsTaxable(pInventoryID).value;
    document.getElementById("ctl00_MainContent_PH_chk_IsTaxable").checked = (lIsTaxable =='T');
   
 }
 
 function ServiceGoToReocurringItem(pReocurrID) 
{
  llocation = lAppPath+"/Service/Reocurring_Billing.aspx?ReoCID="+pReocurrID+"&CM=3";
  window.parent.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?CT=2&RecID="+pLookupID;
  window.location.href=llocation;
}

function GoToUser(pUserID) 
{
  llocation = lAppPath+"/Administration/System_Users.aspx?CT=2&UID="+pUserID;
  window.location.href=llocation;
}



function GoToResourceType(pUserID,pResourceTypeID)
{
    window.parent.location.href=lAppPath+"/Administration/UserResourceTypes.aspx?CT=2&UID="+pUserID+"&RTID="+pResourceTypeID;
}


/* =============================================== Globally Shared ===================================================*/

function buildfileas() {
	lfirst = document.getElementById("ctl00_MainContent_PH_FirstName").value; 
	llast =  document.getElementById("ctl00_MainContent_PH_LastName").value;
	lcompany = document.getElementById("ctl00_MainContent_PH_Company").value; 
	lFileAs = document.getElementById("ctl00_MainContent_PH_FileAs");
    if (lFileAs != null) 
    {
	    for (i = lFileAs.options.length; i >= 0; i--) {
            lFileAs.options[i] = null; 
        }
        if (lfirst !="" && llast != "") {
            lFileAs.options[0] = new Option(llast+", "+ lfirst);
            lFileAs.options[1] = new Option(lfirst+" "+ llast);
            
        }
        if ( lcompany !="" ) {
            lFileAs.options[2] = new Option(lcompany);
            lFileAs.options[3] = new Option(llast+", "+ lfirst+" ("+lcompany+")");
            lFileAs.options[4] = new Option(lcompany+" ("+llast+", "+ lfirst+")");
        }
    }
}

function buildAdminfileas() 
{
 	
	lfirst = document.getElementById("ctl00_MainContent_PH_edt_FirstName").value; 
	llast =  document.getElementById("ctl00_MainContent_PH_edt_LastName").value;
	lcompany = document.getElementById("ctl00_MainContent_PH_edt_CompanyName").value; 
	lFileAs = document.getElementById("ctl00_MainContent_PH_FileAs");
	
    if (lFileAs != null) 
    {
	    for (i = lFileAs.options.length; i >= 0; i--) {
            lFileAs.options[i] = null; 
        }
        if (lfirst !="" && llast != "") {
            lFileAs.options[0] = new Option(llast+", "+ lfirst);
            lFileAs.options[1] = new Option(lfirst+" "+ llast);
            
        }
        if ( lcompany !="" ) {
            lFileAs.options[2] = new Option(lcompany);
            lFileAs.options[3] = new Option(llast+", "+ lfirst+" ("+lcompany+")");
            lFileAs.options[4] = new Option(lcompany+" ("+llast+", "+ lfirst+")");
        }
    }
}

function ValidateStartEndDate(source, arguments)
 {
 
   lStartDate = document.getElementById("ctl00$MainContent_PH$edt_StartDate");
   lEndDate = document.getElementById("ctl00$MainContent_PH$edt_EndDate");
  
   if (lStartDate != null && lEndDate != null )
   {

        lStartDateValue = lStartDate.value;
        lEndDateValue = lEndDate.value;
 
        if ( Date.parse(lStartDateValue) > Date.parse(lEndDateValue)  ) 
        {

           arguments.IsValid=false;
           return false;
        } 
        else 
        {

            arguments.IsValid=true;
            return true;
        }
   }
 }


function sethighlighted(pThisRow)
{
    pThisRow.style.color ="black";
    pThisRow.style.backgroundColor="#FF0000";
}

function resethighlight(pThisRow)
{
    pThisRow.style.color ="black";
    pThisRow.style.backgroundColor="";
}

function GotoPage(pURL)
{
    window.location.href=pURL;
}

function ExpandCollapse(container)
{
     
    var obj = document.getElementById(container + '_collapsible');
    var img = document.getElementById(container + '_button');
     if (obj == null) 
    {
        obj = document.getElementById(container);//this is here for when the ClientID is used to assign the panel name
    }
    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 RecalculateIF(oEdit, oldValue, oEvent)
 {
    lCost = igedit_getById("ctl00_MainContent_PH_edt_Cost").value;
    lMarkup = igedit_getById("ctl00_MainContent_PH_edt_Markup").value;
    lQuantity = igedit_getById("ctl00_MainContent_PH_edt_Quantity").value;
    lPrice = igedit_getById("ctl00_MainContent_PH_edt_Price").value;
    lBillableAmount = igedit_getById("ctl00_MainContent_PH_edt_BillableAmount").value;

    if (lMarkup ==null) 
    {
        if (lPrice ==null) 
        {
            igedit_getById("ctl00_MainContent_PH_edt_Price").setValue(lCost);
        }
        if (lQuantity != null) 
        {
            lPrice_F = parseFloat(lPrice);
            lQuantity_Ft = parseFloat(lQuantity);
            lTotalPrice_F = ( lPrice_F * lQuantity_Ft );
            igedit_getById("ctl00_MainContent_PH_edt_BillableAmount").setValue(lTotalPrice_F);
        }
    }
    else 
    {
        if (lMarkup != null) 
        {
          
          igedit_getById("ctl00_MainContent_PH_edt_Price").setValue(CalculatePrice_ig(lCost,lMarkup) );
          lPriceA = CalculatePrice_ig(lCost,lMarkup);
          //alert(lQuantity);
           if (lQuantity != null && lQuantity != "0") 
           {
            lPrice_F = parseFloat(lPrice);
            lQuantity_Int = parseInt(lQuantity);
            lBillable = igedit_getById("ctl00_MainContent_PH_edt_BillableAmount");
            //alert(lBillable);
            lTotal = (lPrice_F * lQuantity_Int);
            //alert(lTotal);
            lBillable.setValue(lTotal);
           }
           
        }
    }
 }
 function Recalculate()
{
    lCost = igedit_getById("ctl00_MainContent_PH_edt_Cost");
    lMarkup = igedit_getById("ctl00_MainContent_PH_edt_Markup");
    lQuantity = igedit_getById("ctl00_MainContent_PH_edt_Quantity");
    lPrice = igedit_getById("ctl00_MainContent_PH_edt_Price");
    lBillableAmount = igedit_getById("ctl00_MainContent_PH_edt_BillableAmount");
    lCost = lCost.replace(",","");
    lPrice = lPrice.replace(",","");
    
    
    if (lMarkup =="") 
    {
        if (lPrice =="") 
        {
            lPrice.setValue(lCost);
        }
        if (lQuantity != "") 
        {
            lPrice_F = parseFloat(lPrice);
            lQuantity_Ft = parseFloat(lQuantity);
            lTotalPrice_F = ( lPrice_F * lQuantity_Ft );
            lBillableAmountlTotalPrice_F.setValue(lTotalPrice_F);
        }
    }
    else 
    {
        if (lMarkup != "") 
        {

           lPrice.setValue(CalculatePrice(lCost,lMarkup) );
           lPriceA = CalculatePrice(lCost,lMarkup);
           lPriceA = lPrice.replace(",","");
           if (lQuantity != "") 
           {
            lPrice_F = parseFloat(lPrice);
            lQuantity_Int = parseInt(lQuantity);
            lBillableAmountlTotalPrice_F.setValue(lPrice_F * lQuantity_Int);
           }
           
        }
    }
    
    
}

function RecalcLaborTotal()
{
    lQuantity = igedit_getById("ctl00_MainContent_PH_edt_Quantity");
    lPrice = igedit_getById("ctl00_MainContent_PH_edt_Price");
    lTotal = igedit_getById("ctl00_MainContent_PH_edt_BillableAmount");
    lTotal.setValue( lPrice.getValue() * lQuantity.getValue() );
   
}
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 != "") 
    {

       document.getElementById("ctl00_MainContent_PH_edt_Markup").value =CalculateMarkUp(lCost,lPrice);
       if (lQuantity != "") 
       {
        lPrice_F = parseFloat(lPrice);
        lQuantity_Int = parseInt(lQuantity);
        document.getElementById("ctl00_MainContent_PH_edt_BillableAmount").value = formatDecimal( lPrice_F * lQuantity_Int );
       }
       
    }
    
    
}

function MarkupChanged_ig()
{
    lCost = igedit_getById("ctl00_MainContent_PH_edt_Cost").value;
    lMarkup = igedit_getById("ctl00_MainContent_PH_edt_Markup").value;
    lPrice = igedit_getById("ctl00_MainContent_PH_edt_Price").setValue(CalculatePrice_ig(lCost,lMarkup));
}


function PriceChangedIF()
{
    lCost = igedit_getById("ctl00_MainContent_PH_edt_Cost").value;
    lMarkup = igedit_getById("ctl00_MainContent_PH_edt_Markup").value;
    lQuantity = igedit_getById("ctl00_MainContent_PH_edt_Quantity").value;
    lPrice = igedit_getById("ctl00_MainContent_PH_edt_Price").value;
    if (lCost != null && lPrice != null){
        if (lCost > 0 && lPrice > 0) 
        {
            igedit_getById("ctl00_MainContent_PH_edt_Markup").setValue( CalculateMarkUp_ig(lCost,lPrice));
        }
    }
    lPrice_F = parseFloat(lPrice);
    lQuantity_Int = parseInt(lQuantity);
    igedit_getById("ctl00_MainContent_PH_edt_BillableAmount").setValue(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);
}

/*
If the cost for an item is $500 and you want a 30% margin:

$500 / (100%-30%)
$500 / (70%)
$500 / .70 = $714.29

COST / (100%-GM%) = SELLING PRICE
*/

function CalculatePrice_ig(pCost,pMarkUp)
{

   lCost_F = parseFloat(lCost);
   lMarkup_F = parseFloat(pMarkUp);
  
   if (pMarkUp >= 100)
   {
      lTotal = lCost_F/ (pMarkUp/100)
   }
   else
   {
     lTotal = lCost_F /((100 - pMarkUp)/100)
   }
   return lTotal;   
    
}

 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_ig(pCost,pPrice)
{
    
    if (pCost != "" && pPrice != "") 
    {
       lCost_F = parseFloat(lCost);
       lPrice_F = parseFloat(lPrice);
       lMarkup_F = ((lCost_F /lPrice_F)-1);
       if( lMarkup_F < 0) 
       {
        lMarkup_F = lMarkup_F * -1;
       }
//       alert(lMarkup_F);
       lResult = parseFloat(lMarkup_F)*100;
       return lResult;
              
    } 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,pReturnPath) 
{

    llocation = lAppPath+"/Reports/"+pPreviewForm+"?RPID="+pReportID+"&From="+pReturnPath+"?CM=16&CT=1";
    window.location.href=llocation;
 
}

/* =============================================== Document Management===================================================*/

function ViewDocument(pDocumentID)
{
    //Documents_Related is defined by Ajax
    lDocument = Documents_Related.ViewDocument(pDocumentID);
    if (lDocument.value != "") 
    {
        window.open(lDocument.value,"Document","width=800,height=600,resizable=yes");
    }
    
}

function OpenDocument(pDocumentID)
{
    //Documentation_Management_Index is defined by Ajax
    lDocument = Documentation_Management_Index.OpenDocument(pDocumentID);
    if (lDocument.value != "") 
    {
        window.open(lDocument.value,"Document","width=800,height=600,resizable=yes");
    }
    
}
function OpenDocumentURL(pURL)
{
    window.open(pURL,"Document","width=800,height=600,resizable=yes");
}
       
       
function EditDocument (pDocumentID, pDocumentTitle,FilePathFileName)
{
    showPopWin(lAppPath+"/QuickAdds/AddDocument.aspx?DID="+pDocumentID+"&DT="+pDocumentTitle+"&FPFN="+FilePathFileName, 648, 553, null);
}

function NewDocument ()
{
    showPopWin(lAppPath+"/QuickAdds/AddDocument.aspx", 648, 553, null);
}

/* ================================================= Client Space ======================================================*/

function CS_PrintServiceTicket(pTicketID)
{
  llocation = lAppPath+"/Reports/Print_Preview_Server.aspx?FROM="+lAppPath+"/3c_ClientSpace/ClientSpace_ViewServiceHistory.aspx"+"&CM=13&SRVRQST=T&PROID="+pTicketID;
  window.location.href=llocation;

  

}
function CS_EditServiceTicket(pTicketID)
{
  llocation = lAppPath+"/3c_ClientSpace/ClientSpace_ServiceTicket.aspx?TicketID="+pTicketID+"&CM=13";
  window.location.href=llocation;
  

}

function OpenClientSpaceDocument(pDocumentID)
{
   llocation = lAppPath+"/3c_ClientSpace/ClientSpace_CustomContent_Display.aspx?DocID="+pDocumentID;
  window.location.href=llocation;

    
}
function OpenClientSpaceDocumentURL(pURL)
{
    window.open(pURL,"Document","width=800,height=600,resizable=yes");
}


