﻿// JScript File
function showProjectDetails(p)
	{
	var winWidth = 600;
	var winHeight = 700;
	var leftPos = 0;
	var topPos = 0;

	if(screen){leftPos = (screen.availWidth - winWidth)/2}
	if(screen){topPos = (screen.availHeight - winHeight)/2}

	window.open("../Projects/ProjectDetails.aspx?p=00000000-0000-0000-0000-000000000000","prjDtls", "width="+winWidth+", height="+winHeight+", left="+leftPos+", top="+topPos+",scrollbars=no");

	}
	
	function showNewCompany()
	{
		var winWidth = 550;
		var winHeight = 310;
		var leftPos = 0;
		var topPos = 0;

		if(screen){leftPos = (screen.availWidth - winWidth)/2}
		if(screen){topPos = (screen.availHeight - winHeight)/2}
		window.open("../AddressBook/NewCompany.aspx","newContact", "width="+winWidth+", height="+winHeight+", left="+leftPos+", top="+topPos+",scrollbars=yes,resizable=yes,status=yes");
	}
	
	function showNewContact()
	{
		var winWidth = 550;
		var winHeight = 310;
		var leftPos = 0;
		var topPos = 0;

		if(screen){leftPos = (screen.availWidth - winWidth)/2}
		if(screen){topPos = (screen.availHeight - winHeight)/2}

		window.open("../AddressBook/NewContact.aspx","newContact", "width="+winWidth+", height="+winHeight+", left="+leftPos+", top="+topPos+",scrollbars=yes,resizable=yes,status=yes");

	}
	
	function showAddressBook()
	{
	var winWidth = 830;
	var winHeight = 650;
	var leftPos = 0;
	var topPos = 0;

	if(screen){leftPos = (screen.availWidth - winWidth)/2}
	if(screen){topPos = (screen.availHeight - winHeight)/2}

	window.open("../AddressBook/Search.aspx","addressBk", "width="+winWidth+", height="+winHeight+", left="+leftPos+", top="+topPos+",scrollbars=yes,resizable=yes,status=yes");

	}
	
	function showAddressBookContact(c)
	{
	var winWidth = 830;
	var winHeight = 650;
	var leftPos = 0;
	var topPos = 0;

	if(screen){leftPos = (screen.availWidth - winWidth)/2}
	if(screen){topPos = (screen.availHeight - winHeight)/2}

	window.open("../AddressBook/Details.aspx?c="+c,"addressBk", "width="+winWidth+", height="+winHeight+", left="+leftPos+", top="+topPos+",scrollbars=yes,resizable=yes,status=yes");

	}


function downloadDocument(i, t)
	{
	var winWidth = 10;
	var winHeight = 10;
	var leftPos = 0;
	var topPos = 0;

	if(screen){leftPos = (screen.availWidth - winWidth)/2}
	if(screen){topPos = (screen.availHeight - winHeight)/2}
	
	topPos = topPos - 40;
	leftPos = leftPos - 80;
	
	window.open("../documents/ViewDownload.aspx?i="+i+"&t="+t,"doc"+i, "width="+winWidth+", height="+winHeight+", left="+leftPos+", top="+topPos+",location=no,status=no,scrollbars=no,resizable=no");
	}
	


// Returns query string parameter value
function getQuerystring(key, default_)
	{
		if (default_==null) default_=""; 
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		var qs = regex.exec(window.location.href);
		if(qs == null)
			return default_;
		else
			return qs[1];
	}
	
function calendarPicker(lvl, strField)
	{
		// lvl - Gets the date picker back to the top level
		var winWidth = 250;
		var winHeight = 180;
		var leftPos = 0;
		var topPos = 0;

		if(screen){leftPos = (screen.availWidth - winWidth)/2}
		if(screen){topPos = (screen.availHeight - winHeight)/2}
	
		window.open(lvl+"DatePicker.aspx?field="+strField,"calendarPopup","width="+winWidth+", height="+winHeight+", left="+leftPos+", top="+topPos+",scrollbars=no");
	}
	
	function stripNonNumeric(input)
	{
		filteredValues = "1234567890.-";     // Characters that are kepted
		var i;
		var returnString = "";
		for (i = 0; i < input.length; i++) 
		{  
			var c = input.charAt(i);
			if (filteredValues.indexOf(c) > -1) returnString += c;
		}
		return returnString;
	}
	
	function cleanPhone(f)
	{
		
		var ph = stripNonNumericPh(f.value);
		if(ph.length > 15){ph = ph.substring(0,15)};
		
		if(ph.length < 11)
		{
		f.value = ph.substring(0,3) + "." + ph.substring(3,6) + "." + ph.substring(6,10)
		}
		else
		{
		f.value = ph.substring(0,3) + "." + ph.substring(3,6) + "." + ph.substring(6,10) + "." + ph.substring(10, ph.length)
		}				
	}
	
	function stripNonNumericPh(input)
	{
			filteredValues = "1234567890";     // Characters that are kepted
			var i;
			var returnString = "";
			for (i = 0; i < input.length; i++) 
			{  
				var c = input.charAt(i);
				if (filteredValues.indexOf(c) > -1) returnString += c;
			}
			return returnString;
		}	
	
	function formatCurrency(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 isValidDate(dateStr) 
		{
		// Checks for the following valid date formats:
		// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

		var matchArray = dateStr.match(datePat); // is the format ok?
		if (matchArray == null) {
		alert(dateStr + " Date is not in a valid format.")
		return false;
		}
		month = matchArray[1]; // parse date into variables
		day = matchArray[3];
		year = matchArray[4];
		if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
		}
		if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days!")
		return false;
		}
		if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
		alert("February " + year + " doesn't have " + day + " days!");
		return false;
			 }
		}
		return true;
		}

	function dateDiff(d1, d2) 
		{
		
		date1 = new Date();
		date2 = new Date();
		diff  = new Date();

		if (isValidDate(d1)) { // Validates first date 
		date1temp = new Date(d1 + " 00:00:00AM");
		date1.setTime(date1temp.getTime());
		}
		else return false; // otherwise exits

		if (isValidDate(d2)) { // Validates second date 
		date2temp = new Date(d2 + " 00:00:00AM");
		date2.setTime(date2temp.getTime());
		}
		else return false; // otherwise exits

		diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
		timediff = diff.getTime();
		
		return Math.floor(timediff / (1000 * 60 * 60 * 24));
		
		}

	function getCurrentDate()
	{
		var now = new Date();
		var cur = "";
		
		if (now.getMonth() < 10){cur = "0";}
		cur = cur + now.getMonth() + "/";
		
		if (now.getDate() < 10){cur = cur + "0";}
		cur = cur + now.getDate() + "/";
		cur = cur + now.getFullYear();
		return cur;
	}
	
	function showImage(p,w,h)
	{
	var leftPos = 0;
	var topPos = 0;
	
	p = "../Overview/" + p;
	
	if (p.indexOf("Edit.aspx") >2)
		{
		// Add a little space if there is editing involved
		h = parseFloat(h)+180;
		}
	
	// Center the new window
	if(screen){leftPos = (screen.availWidth - w)/2}
	if(screen){topPos = (screen.availHeight - h)/2}

	var newWindow = window.open(p, "projectImage","width="+w+",height="+h+",left="+leftPos+",top="+topPos+",scrollbars=no,location=no,resizable=no,menubar=no,status=no");			
	newWindow.focus();
	}

	

