﻿// JScript File
// quick new window centered on screen function
function NewWindowQuick(theURL, myWidth, myHeight) { 
	var winName = "newWindowQuick";
	var isCenter = 'true';
	var features = 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes';
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/4;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  }
  window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
}

function disableMe(theItem) {
  theItem.disabled = true
  GetPostBackEventReference(theItem);
}

function setPaymentMessage(theText) {
    var theSpan = document.getElementById("paymentMessage");
    theSpan.innerHTML = theText;
}

function resetPaymentMessageLabel() {
    var theSpan = document.getElementById("ctl00_body_Receipt1_lblError");
    theSpan.innerHTML = "";
}

function printPage() {
    window.print();
}


function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
   window.external.AddFavorite(location.href, title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}

function toggleDisplay(elementId, state) {
    var ele = document.getElementById(elementId);
    if (!ele) {
        alert("ERROR - Element ID " + elementId + " Not Found On Page!")
        return false;
    }
    
    ele.style.display = state;
}

function overrideTab(e, changeTo) {
	var checkCode;
	if (document.all){
        var evnt = window.event;
        checkCode = evnt.keyCode;
  	}else{
        checkCode = e.keyCode;
  	}
  
	if (checkCode == 9)
	{
		 document.getElementById(changeTo).focus();
		 return false;
	}
}

 function maskPhone(fldValue)
{
   
    var str = fldValue.value;
        if(str != "") {
        var tmpStr = "(";
        keyCount = str.length;
        keyEntered = str.substring(keyCount-1,keyCount);
        keyCount++;
        switch (keyCount)
        {
        case 2: 
          tmpStr += fldValue.value;
          fldValue.value = tmpStr;
          break;
        case 5:
          fldValue.value += ") ";
          break;
        case 10:
          fldValue.value += "-";
          break;
        }
        }
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function advOneYear(startVal) {
    var dateItems = ""
    dateItems = startVal.split("/");
    var tempYear = dateItems[2];
    var nextYear = String(parseInt(tempYear) + 1);
    
    if (nextYear.length == 1) {
        nextYear = "0" + nextYear;
    }
    
    var returnVal = dateItems[0] + "/" + dateItems[1] + "/" + nextYear
    return returnVal
}

function updatePolicy1EndDate(policy1StartDateBox, policy1EndDateBox) {
    if (document.getElementById(policy1StartDateBox).value != "" && document.getElementById(policy1EndDateBox).value == "") {
        document.getElementById(policy1EndDateBox).value = advOneYear(document.getElementById(policy1StartDateBox).value)
    }
}

function updatePolicy2EndDate(policy2StartDateBox, policy2EndDateBox) {
    if (document.getElementById(policy2StartDateBox).value != "" && document.getElementById(policy2EndDateBox).value == "") {
        document.getElementById(policy2EndDateBox).value = advOneYear(document.getElementById(policy2StartDateBox).value)
    }
}

function forceCalcDueDate(dueDateBox, policy1EndDateBox, policy2EndDateBox, assignmentDateBox, insuranceCoBox) {
    document.getElementById(dueDateBox).value = "";
   
    calcDueDate(dueDateBox, policy1EndDateBox, policy2EndDateBox, assignmentDateBox, insuranceCoBox) 
}

function calcDueDate(dueDateBox, policy1EndDateBox, policy2EndDateBox, assignmentDateBox, insuranceCoBox) {
    if (document.getElementById(dueDateBox).value == "") {

        // calculate due date if there is a value in the policy1EndDate AND assignmentDateBox
        var date1 = document.getElementById(policy1EndDateBox).value;
        var date2 = document.getElementById(policy2EndDateBox).value;
        var assignDate = document.getElementById(assignmentDateBox).value;
        var insObj = document.getElementById(insuranceCoBox);
        var insId = insObj.options[insObj.selectedIndex].value;
        var daysOut;
        
        if (insId == "State Farm Insurance" || insId =="Navigators Insurance Co." || insId == "State Farm Ins Co." || insId == "State Farm Insurnace") {
            daysOut = 45;
        } else {
            daysOut = 60;
        }

        if (date2 != "") {
            //see if date 2 is older, if so, use it as date1
            if (calcDays(date1, date2) < 0) {
                date1 = date2;
            }
        }
       
        
        if (date1 != "" && assignDate != "") {
            if (calcDays(date1, assignDate) < 0) {
                 var newDate = new Date(assignDate);
                 
            } else {
                var newDate = new Date(date1);
            }
            
         
                       
            newDate.setDate(newDate.getDate() + daysOut);
            var adjNewDate = formatDate(newDate, 'MM/dd/y');
            document.getElementById(dueDateBox).value = adjNewDate;
           
        }
    }
}

function calcDays(date1, date2){
	date1 = date1.split("/");
	date2 = date2.split("/");
	var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
	var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
	var daysApart = (Math.round((sDate-eDate)/86400000));
	return daysApart;
}

function checkStatus(assignedTo, statusBox) {
   var adjBox = document.getElementById(statusBox)
   if (assignedTo.selectedIndex >= 0 && adjBox.value == "Ordered") {
        adjBox.value = "Open";
   }
}

function ConfirmClose() {
    /*
	if (event.clientY < 0)
	{
		event.returnValue = 'You MUST logout before leaving the Premium Audit website - Please select Cancel below to stay on the website and then click the logout tab.';
	}
	*/
}