/*Javascript for My-InTouch specific needs*/

var nfiles1 = 1;

function fxSelectNav(theNav){
	l_dontsubmit = true;
   	for (i = 0; i < 5; i++) {
	  theForm = document.forms[i].name;
	  if (theForm.substr(0,2) == "IT"){
//	    alert(theForm);
		break;
	  }
    }
	document.forms[i].action="/fdtcgi/wc.dll?FDProcess~fxRepGen";
	if(isNull(document.forms[i].classno2)){
		strClass = "";
	}else{
		strClass = document.forms[i].classno2.checked;
		if(strClass){
		 	strClass = "and nextaction = ''";
		}else{
		 	strClass = "and nextaction <> 'Closed'";
		}
	}
	var strWhere1 = "userid='"+document.forms[i].userid.value+"' ";
	strWhere = strWhere1 + strClass;
	document.forms[i].cReportType.value='';
	document.forms[i].cpassword.value='nic';

//alert(theNav);
	switch(theNav){
		case "Contact2":
			document.ITContacts.action="/app/New_Contact.asp";
			break;
		case "Profile2":
			document.ITContacts.Form_Title.value="IT_User";
			document.ITContacts.action="/fdtcgi/wc.dll?FDProcess~SaveTokens";
			document.ITContacts.TemplateX1.value="mit\\mit-Profile";
			break;
		case "EmailLog2":
			document.ITContacts.cSelectWhere.value = strWhere1;
			document.ITContacts.cReport.value="IT-Email";
			document.ITContacts.cSelectOrder.disabled = true;
			document.ITContacts.cnextpage.value = "/contactmassemail"+document.ITContacts.cnextpage1.value;
			break;
		case "EmailLog-Contact":
			document.ITContacts.cSelectWhere.value = strWhere1 + "and '"+document.ITContacts.email.value+"' $ recipient";
			document.ITContacts.cReport.value="IT-Email";
			break;
		case "Daily":
			var dDate = new Date();
			var DOW = dDate.getDay()*1;
			var Sunday = 7-DOW
			strDays = document.ITContacts.callreport.value;
			switch(strDays){
				case "today": dtRange = "d_followup = date()"; break;
				case "tomorrow": dtRange = "d_followup = date()+1"; break;
				case "yesterday": dtRange = "d_followup = date()-1"; break;
				case "thisweek": dtRange = "d_followup > date()-"+DOW+" and d_followup < date()+"+Sunday; break;
				case "lastweek": dtRange = "d_followup > date()-7-"+DOW+" and d_followup < date()-7+"+Sunday; break;
				case "lastmonth": dtRange = "d_followup > date()-30 and d_followup < date()"; break;
			}
			strWhere = strWhere + " and " + dtRange;
			document.ITContacts.cSelectWhere.value = strWhere;
			document.ITContacts.cReport.value='ITContacts';
			break;
		case "General":
			document.ITContacts.cSelectWhere.value = strWhere;
			document.ITContacts.cReport.value='ITContacts';
			break;
		case "ReDate":
			strField = document.ITContacts.cField.value;
			strWord = document.ITContacts.cWord.value
			var dDate = new Date();
			var DOW = dDate.getDay()*1;
			var Sunday = 7-DOW
			strDays = document.ITContacts.callreport.value;
			switch(strDays){
				case "today": dtRange = "d_followup = date()"; break;
				case "yesterday": dtRange = "d_followup = date()-1"; break;
				case "thisweek": dtRange = "d_followup > date()-"+DOW+" and d_followup < date()+"+Sunday; break;
				case "lastweek": dtRange = "d_followup > date()-7-"+DOW+" and d_followup < date()-7+"+Sunday; break;
				case "lastmonth": dtRange = "d_followup > date()-30 and d_followup < date()"; break;
			}
			strWhere = strWhere + " and " + dtRange;
			document.ITContacts.cSelectWhere.value = strWhere;
			document.ITContacts.cReport.value='IT-ReDate';
			document.ITContacts.cnextpage.value = "/fdtcgi/maintenance/Go-Back.asp?nPgs=2";
			break;
		case "ResetAR":
			strField = document.ITContacts.cField.value;
			strWord = document.ITContacts.cWord.value
			document.ITContacts.cSelectWhere.value = strWhere + " and lower('" + strWord + "') $ lower(" + strField + ")";
			document.ITContacts.cReport.value='IT-Reset-Autoresponder';
			document.ITContacts.cnextpage.value = "/fdtcgi/maintenance/Go-Back.asp?nPgs=2";
			break;
		case "Advanced":
			strField = document.ITContacts.cField.value;
			strWord = document.ITContacts.cWord.value
			document.ITContacts.cSelectWhere.value = strWhere + " and lower('" + strWord + "') $ lower(" + strField + ")";
			document.ITContacts.cReport.value='ITContacts';
			break;
		case "Mass":
			strField = document.ITContacts.cField.value;
			strWord = document.ITContacts.cWord.value;
			if (strWord == ""){
				document.ITContacts.cSelectWhere.value = strWhere;
			}else{
//				alert(strWord);
				document.ITContacts.cSelectWhere.value = strWhere + " and lower('" + strWord + "') $ lower(" + strField + ")";
			}
			document.ITContacts.cReport.value="ITMassEmail";
			document.ITContacts.cpassword.value="mit";
			document.ITContacts.cSelectWhere.value = document.ITContacts.cSelectWhere.value + " and !empty(email) and !'Removed from E-mail' $ notes01 and !'Undeliverable' $ notes02";
			break;
		case "Export2":
			strField = document.ITContacts.cField.value;
			strWord = document.ITContacts.cWord.value
			if (strWord == ""){
				document.ITContacts.cSelectWhere.value = strWhere;
			}else{
				document.ITContacts.cSelectWhere.value = strWhere + " and lower('" + strWord + "') $ lower(" + strField + ")";
			}
			document.ITContacts.cReport.value='ITExport';
			document.ITContacts.cReportType.value='CommaDelimited';
			break;
		case "BizCards2":
			document.ITContacts.action="/app/BizCards.asp";
			break;
		case "EditBizCards":
			strWhere1 = "username='"+document.ITContacts.userid.value+"' ";
			document.ITContacts.cSelectWhere.value = strWhere1;
			document.ITContacts.cReport.value='IT_BizCards';
			document.ITContacts.cSelectOrder.value = "bc_company,bc_name";
			break;
		case "AR":
			document.ITContacts.cSelectWhere.value = strWhere1;
			document.ITContacts.cReport.value="MITResponder";
			document.ITContacts.cpassword.value="mork";
			break;
		case "AR2":
			document.ITContacts.action="/app/AutoResponder.asp";
			break;
		case "Expense":
			document.ITContacts.action="/app/Expense.asp";
			break;
		case "Expense-Report":
			strWhere = strWhere1 + "and ctod(d_today) >= ctod('"+document.ITExpenses.rep_start.value+"') and ctod(d_today) <= ctod('" + document.ITExpenses.rep_end.value +"')";
			document.ITExpenses.cSelectWhere.value = strWhere;
			document.ITExpenses.cReport.value="ITExpenses";
			document.ITExpenses.cnextpage.value = "/fdtcgi/maintenance/Go-Back.asp?nPgs=2";
			document.ITExpenses.cpassword.value="mit";
			document.ITExpenses.destination.value="reports";
			break;
		case "Invoice":
			document.ITContacts.action="/fdtcgi/wc.dll?FDProcess~SaveTokens";
			document.ITContacts.TemplateX1.value="mit\\mit-inv_new";
			document.ITContacts.Form_Title.value="IT-Invoice";
			fxITInvoice();
			document.forms[i].inv_num.value = document.forms[i].inv_last.value;
			break;
		case "Invoice-Report":
//			strWhere = strWhere1 + "and d_today >= '"+document.forms[i].rep_start.value+"' and d_today <= '" + document.forms[i].rep_end.value +"'";
			strWhere = strWhere1;
			document.forms[i].cSelectWhere.value = strWhere;
			document.forms[i].cSelectOrder.disabled = true;
			document.forms[i].cReport.value="IT-Invoice";
			document.forms[i].cnextpage.value = "/fdtcgi/maintenance/Go-Back.asp?nPgs=2";
			document.forms[i].cpassword.value="mit";
			break;
		case "Envelope":
			document.ITContacts.action="/app/Envelopes10.asp";
			break;
		case "Labels":
			document.ITContacts.TemplateX1.value="mit\\mit-labels";
			document.ITContacts.action="/fdtcgi/wc.dll?FDProcess~SaveTokens";
			strWhere = strWhere1;
			break;
		case "Edit-Labels":
			strWhere1 = "recno() > 0";
			document.forms[i].cSelectWhere.value = strWhere1;
			document.forms[i].cReport.value='IT_Labels';
			document.forms[i].cSelectOrder.value = "lbl_type,label_id";
			document.forms[i].cpassword.value="fdt";
			break;
	}
	document.forms[i].submit();
}


function fxApprove(){
	if(document.ITBizCard.l_approve.checked)
	{
		document.ITBizCard.Done.disabled=false;
	}else{
		document.ITBizCard.Done.disabled=true;
	}
}

function fxRegister(){
	var a = "Hello "+document.InTouchReg.agnt_name.value+",";
	var u = document.InTouchReg.strMsgu.value+document.InTouchReg.username.value;
	var p = "Your Password is: "+document.InTouchReg.password.value;
	var c = document.InTouchReg.strMsgc.value;
	document.InTouchReg.strMessage.value = a+u+"\n"+p+c;

	var s1 = "Sincerely,"
	var s2 = document.InTouchReg.agnt_company.value;
	var s3 = document.InTouchReg.agnt_name.value;
	var s4 = document.InTouchReg.agnt_address.value;
	var s5 = document.InTouchReg.agnt_city.value + ", " + document.InTouchReg.agnt_state.value;
	var s6 = document.InTouchReg.agnt_phone.value;
	var s7 = document.InTouchReg.agnt_email.value;
	var s8 = document.InTouchReg.agnt_website.value;

	document.InTouchReg.signature.value = "\n"+s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5+"\n"+s6+"\n"+s7+"\n"+s8+"\n"
	document.InTouchReg.recipient.value = document.InTouchReg.agnt_email.value;

}

function fxAction(theField){
	switch(theField){
		case "Remove":
			fxDT(document.ITContacts.notes01,"Removed from E-mail List"); break;
		case "Undeliverable":
			fxDT(document.ITContacts.notes02,"E-mail Undeliverable: "+document.ITContacts.email.value);
			document.ITContacts.email.value = "";
			document.ITContacts.NextPage.value = "fdtcgi/maintenance/Go-Back.asp?nPgs=3";
			break;
		case "Closed":
			fxDT(document.ITContacts.notes02,"Record Closed"); 
			document.ITContacts.l_delete[0].checked = true;
			break;
	}
}

function fxDeltaDate(date1,theField,date2,numDays) {
//alert(isNull(numDays));
	var date1x =new Date(date1.value);
	if(isNull(date2)){
		var date2x=new Date();
	}else{
		var date2x=new Date(date2.value);
	}
     // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = date1x.getTime();
    var date2_ms = date2x.getTime();


    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);
    // Convert back to days and return
    var nDays1 = Math.round(difference_ms/ONE_DAY)*1;
	var nDays = nDays1;
	nDays = fxRound2(nDays,3);
	theField.value = (nDays < numDays*1) ? "true" : "false";

	if(document.ITContacts.l_paid.value == "Yes"){
		theField.value = "true";
		showSection("showDays",false);
	}else{
		document.MITLeft.nDays3.value = (numDays*1-nDays*1);
		if(document.MITLeft.nDays3.value < 1){
			showSection("showDays",false);
		}
	}
	showSection("paid",theField.value);
    return
}


var SpecialWord = "stnick007";
    SpecialLetter = 0;
function getKey(keyStroke) {
var isNetscape=(document.layers);
// Cross-browser key capture routine couresty
// of Randy Bennett (rbennett@thezone.net)
var eventChooser = (isNetscape) ? keyStroke.which : event.keyCode;
var which = String.fromCharCode(eventChooser).toLowerCase();
if (which == SpecialWord.charAt(SpecialLetter)) {
   SpecialLetter++;
   if (SpecialLetter == SpecialWord.length) showSection("wdtt",true);
   }else SpecialLetter = 0;
}
document.onkeypress = getKey;

function fxWebsiteID(theField,theField2){
//alert("got Here?");
  strfdtPath = document.InTouchReg.fdtPath1.value;
  theField2.value="";
  theValue = theField.value;
  theTemp = theField.value;
 if (theValue != ""){
   	for (i = 0; i < theValue.length; i++) {
	  if (theValue.substr(i,2) == "//"){
	     theTemp = theValue.substr(i+2,100);
	  }
   	}
   if (theTemp.indexOf("/") > -1) {
	  theTemp = theTemp.substr(theTemp.indexOf("/")+1,theTemp.length);
   }
	theField.value = theTemp;
  }
  theField2.value = 'http://www.m-i-t.us/' + theField.value;
  document.InTouchReg.fdtPath.value = strfdtPath + "\\" + theField.value + "\\";
}

function fxReportDates(){
	var strPeriod = document.ITExpenses.rep_period.value;
	var dDate = new Date();
	var d  = dDate.getDate();
	var m  = dDate.getMonth() + 1;
	var yy = dDate.getYear();
	var DOW = dDate.getDay()*1;
	var month = (m < 10) ? '0' + m : m;
	var year = (yy < 1900) ? yy + 1900 : yy;
	var r1 = "";
	var r2 = "";
	var today = (d < 10) ? '0' + d : d;

	switch(strPeriod){
		case "all": r1="01/01/2000";break;
		case "mtd":	
			var r1 = month + "/01/" + year;
			break;
		case "lastmonth":	
			m  = m - 1;
				if (m == 0){
					m=12;
					year = (yy-1 < 1900) ? yy-1 + 1900 : yy-1;
				}
			month = (m < 10) ? '0' + m : m;
			var r1 = month + "/01/" + year;
			var d1a = getDaysInMonth(m,yy);
			var r2 = month + "/" + d1a + "/" + year;
			break;
		case "lastqtr":	
			m = m - 3;
			if(m < 0){
				m = 12;
				year = (yy-1 < 1900) ? yy-1 + 1900 : yy-1;
			}
		case "qtd":
			switch(m){
				case 1:
					var r1 = "01/01/" + year;
					var r2 = "03/31/" + year;
					break;
				case 2:
					var r1 = "01/01/" + year;
					var r2 = "03/31/" + year;
					break;
				case 3:
					var r1 = "01/01/" + year;
					var r2 = "03/31/" + year;
					break;
				case 4:
					var r1 = "04/01/" + year;
					var r2 = "06/30/" + year;
					break;
				case 5:
					var r1 = "04/01/" + year;
					var r2 = "06/30/" + year;
					break;
				case 6:
					var r1 = "04/01/" + year;
					var r2 = "06/30/" + year;
					break;
				case 7:
					var r1 = "07/01/" + year;
					var r2 = "09/30/" + year;
					break;
				case 8:
					var r1 = "07/01/" + year;
					var r2 = "09/30/" + year;
					break;
				case 9:
					var r1 = "07/01/" + year;
					var r2 = "09/30/" + year;
					break;
				case 10:
					var r1 = "10/01/" + year;
					var r2 = "12/31/" + year;
					break;
				case 11:
					var r1 = "10/01/" + year;
					var r2 = "12/31/" + year;
					break;
				case 12:
					var r1 = "10/01/" + year;
					var r2 = "12/31/" + year;
					break;
			}
			break;
		case "lastyear":	
			year = (yy-1 < 1900) ? yy-1 + 1900 : yy-1;
			r2 = "12/31/"+ year;
		case "ytd":	
			var r1 = "01/01/" + year;
			break;
		case "dates":	
			document.ITExpenses.rep_start.focus();
			r1 = "";
			break;
	}

	document.ITExpenses.rep_start.value = r1;
	if(r2 == ""){
		r2 = month + "/" + today + "/" + year;}
	document.ITExpenses.rep_end.value = r2;
}


function fxTotals(){
	var amt1 = document.forms[0].qty01.value*fxPlain(document.forms[0].unitprice01.value)*1;
	document.forms[0].amt01.value = amt1;
	document.forms[0].amt01.value = fxCurrency(document.forms[0].amt01.value);

	var amt2 = document.forms[0].qty02.value*fxPlain(document.forms[0].unitprice02.value)*1;
	document.forms[0].amt02.value = amt2;
	document.forms[0].amt02.value = fxCurrency(document.forms[0].amt02.value);
	var amt3 = document.forms[0].qty03.value*fxPlain(document.forms[0].unitprice03.value)*1;
	document.forms[0].amt03.value = amt3;
	document.forms[0].amt03.value = fxCurrency(document.forms[0].amt03.value);
	var amt4 = document.forms[0].qty04.value*fxPlain(document.forms[0].unitprice04.value)*1;
	document.forms[0].amt04.value = amt4;
	document.forms[0].amt04.value = fxCurrency(document.forms[0].amt04.value);
	var amt5 = document.forms[0].qty05.value*fxPlain(document.forms[0].unitprice05.value)*1;
	document.forms[0].amt05.value = amt5;
	document.forms[0].amt05.value = fxCurrency(document.forms[0].amt05.value);
	var amt6 = document.forms[0].qty06.value*fxPlain(document.forms[0].unitprice06.value)*1;
	document.forms[0].amt06.value = amt6;
	document.forms[0].amt06.value = fxCurrency(document.forms[0].amt06.value);

	document.forms[0].subtotal.value = amt1+amt2+amt3+amt4+amt5+amt6;
	document.forms[0].subtotal.value = fxCurrency(document.forms[0].subtotal.value);
	var nRate = fxPlain(document.forms[0].taxrate.value)*1;
	if (nRate > 1) nRate=nRate/100;
	var nSubtotal = fxPlain(document.forms[0].subtotal.value)*1;
	var nTax = nRate * nSubtotal;
	var nShipping = fxPlain(document.forms[0].shipping.value)*1;
	document.forms[0].tax.value = nTax;
	document.forms[0].tax.value = fxCurrency(document.forms[0].tax.value);
	document.forms[0].taxrate.value = nRate;
	document.forms[0].total.value=nSubtotal+nShipping+nTax;
	document.forms[0].total.value=fxCurrency(document.forms[0].total.value);
	return;
}

function fxTaxRate(nRate){
	if (nRate > 1) nRate=nRate/100;
	document.forms[0].taxrate.value = nRate;
	return;
}


function fxST_Same(){
	document.forms[0].st_business.value = document.forms[0].business.value;
	document.forms[0].st_name.value = document.forms[0].first_name.value + " " + document.forms[0].last_name.value;
	document.forms[0].st_address.value = document.forms[0].street.value;
	document.forms[0].st_city.value = document.forms[0].city.value;
	document.forms[0].st_state.selectedIndex = document.forms[0].state.selectedIndex;
	document.forms[0].st_zipcode.value = document.forms[0].zipcode.value;
}

function fxITInvoice(){
	var ddate = new Date();
	var yr = ddate.getYear();
	yr = yr + "a"; // to get it to act as a character instead of a numeric
	var m = ddate.getMonth()*1+1;
	var d = ddate.getDate()*1;
	var dd = (d < 10) ? '0' + d : d;
	var mm = (m < 10) ? '0' + m : m;
	var yy = (yr.length > 3) ? yr.substr(2,2) : yr;

	var nLast = document.forms[0].inv_last.value;
	var nSequence = "01";
	if(nLast != ""){
		var nLastM = nLast.substr(3,4);
		if(nLastM == mm+dd){
			nSequence = (nLast.length > 2) ? nLast.substr(8,2)*1+1 : nLast*1+1;
			nSequence = (nSequence < 10) ? '0' + nSequence : nSequence;
		}
	}
	var nInvoice = yy+"-"+mm+dd+"-"+nSequence;
	document.forms[0].inv_last.value = nInvoice;
}


function fxProfile(aff){
	var tUser = document.forms[0].userid.value;
	switch (aff){
		case "aff":
			onlyAlphaNumeric(document.forms[0].website_id);
			document.forms[0].fdtPath.value = "\\hshome\\formdriver2\\m-i-t.us\\";
			document.forms[0].website_id.value=switchSpaces(document.forms[0].website_id,'',20);
			fxWebsiteID(document.forms[0].website_id,document.forms[0].dlr_website);
			document.forms[0].TemplateX1.disabled=true;
			document.forms[0].NextPage.disabled=false;
			document.forms[0].FDFunctions.value = "355000001";
			break;
		case "logo":
			document.forms[0].fdtPath.value = "\\hshome\\formdriver2\\images\\"+tUser.substr(0,5)+"\\";
			fxNameOnly(document.forms[0].uFile2,document.forms[0].File1);
			document.forms[0].inv_logo.value = "/images/"+tUser.substr(0,5)+"/"+document.forms[0].File1.value;
			document.forms[0].TemplateX1.disabled=false;
			document.forms[0].NextPage.value="anythingnottodisplayfilesuploaded";
			document.forms[0].FDFunctions.value = "30000002";
			break;
		case "inv":
			document.forms[0].fdtPath.value = "\\hshome\\formdriver2\\images\\"+tUser.substr(0,5)+"\\";
			fxNameOnly(document.forms[0].File2,document.forms[0].File1);
			document.forms[0].TemplateX1.disabled=false;
			document.forms[0].NextPage.disabled=true;
			document.forms[0].FDFunctions.value = "30500002";
			break;
	}
}

function fxNameOnly(the1stField,the2ndField){
  theValue = the1stField.value;
  if (theValue != ""){
   	for (i = 0; i < theValue.length; i++) {
	  if (theValue.substr(i,1) == "\\"){
	     theTemp = theValue.substr(i+1,100);
	  }
   	}
	the2ndField.value = theTemp;
  }
}


