// Are we using WebKit here?
// This method is recommended by Apple themselves.

var tempStr = navigator.userAgent.toLowerCase();
var pos = tempStr.indexOf("applewebkit/");
var isAppleWebkit = (pos != -1);		
if (isAppleWebkit) { addShadowForWebKit(); }

// This function adds a null shadow to text on WebKit-based browsers which leads
// to better rendered, thinner typography. It is totally harmless CSS, but we
// lead it dynamically as it does not belong to a CSS 2 style sheet and would
// make the CSS 2 document invalid.

function addShadowForWebKit() {
	var cssLink = document.createElement("link")
	cssLink.href = "/css-webkit.css";
	cssLink.rel = "stylesheet";
	cssLink.type = "text/css";
	cssLink.media = 'screen';
	document.getElementsByTagName("head")[0].appendChild(cssLink);
}

// Globals
var yourStreet;
var yourZIP;
var insideCloud;
var weIgnoreLocation;

var map;
var geocoder;

var lastCalled;

// Please edit this list, as it is used for generating the "all markers" version of the big map (locations/network)
theLocations = new Array (
	new GLatLng(42.989544, -71.46305),	// J.W. Hill
	new GLatLng(42.9898, -71.463052), // Digital Prospectors
	new GLatLng(42.990741, -71.463072),	// Elizabeth's on Elm
	new GLatLng(42.990742,-71.463082), // Z Restaurant
	new GLatLng(42.991302, -71.463093),	// Copy Express
	new GLatLng(42.991415, -71.463097),  // Ben and Jerry's
	new GLatLng(42.991937, -71.463097), // Castros
	new GLatLng(42.9925, -71.4638),	// 1000 Crane
	new GLatLng(42.993163, -71.463051), // Caesarios
	new GLatLng(42.9935, -71.4623), // Big Brothers Big Sisters
	new GLatLng(42.993861, -71.463102), // Bridge Cafe
	new GLatLng(42.995941, -71.46348), // Dynamic Network Services Inc.
	new GLatLng(42.991935, -71.461902), // Consuelos Taqueria
	new GLatLng(42.997601, -71.465549), // Milton's Millyard Grill
	new GLatLng(42.9956, -71.4629), // WBC Estates 1235 Elm Street
	new GLatLng(42.9979, -71.4630) // WBC Estates 15-23 Prospect Street
	);
	
theCoverage = new Array (

new GPolygon([
	new GLatLng(42.988949, -71.463121),
	new GLatLng(42.989439, -71.462879),
	new GLatLng(42.990134, -71.462209),
	new GLatLng(42.990952, -71.460420),
	new GLatLng(42.991102, -71.460644),
	new GLatLng(42.991074, -71.462227),
	new GLatLng(42.991511, -71.462544),
	new GLatLng(42.992764, -71.462227),
	new GLatLng(42.993200, -71.462879),
	new GLatLng(42.993623, -71.462357),
	new GLatLng(42.994086, -71.463028),
	new GLatLng(42.995326, -71.463140),
	new GLatLng(42.995067, -71.463904),
	new GLatLng(42.994658, -71.464276),
	new GLatLng(42.993759, -71.464127),
	new GLatLng(42.992832, -71.466344),
	new GLatLng(42.991388, -71.463773),
	new GLatLng(42.990134, -71.464798),
	new GLatLng(42.990271, -71.463438),
	new GLatLng(42.988949, -71.463121)
			  ], "#48B627", 2, 1, "#48B627", 0.2),
			  		  
new GPolygon([
	new GLatLng(42.998158,-71.463142),
	new GLatLng(42.997772,-71.463081),
	new GLatLng(42.997757,-71.462868),
	new GLatLng(42.997765,-71.46266199999999),
	new GLatLng(42.998104,-71.462631),
	new GLatLng(42.998158,-71.463142)
			  ], "#48B627", 2, 1, "#48B627", 0.2)
		  
);

function setMeUp()
{
	$("SignUpForm").focusFirstElement();
	disablePayment();
	insideCloud = false;
	$("iJoinError").style.display = 'none';
	$("iJoinError").removeClassName('warning');
	$("iJoin").enable();
	$("iJoinLabel").removeClassName('notActive');
	$('iJoin').checked = true;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function disablePayment()
{
	$("yourCardNumber").disable();
	$("yourCardType").disable();
	$("yourCardExpMonth").disable();
	$("yourCardExpYear").disable();
	
	if (!insideCloud) {
		$("iJoin").disable();
		$("iJoinError").style.display = 'block';
		$("iJoinError").addClassName('warning');
		$("iJoinLabel").addClassName('notActive');	
	}
		
	$("introPayment").addClassName('notActive');

	$("cardTypeLabel").removeClassName('mandatory');
	$("cardTypeLabel").addClassName('notActive');
	$("ccLabel").removeClassName('mandatory');
	$("ccLabel").addClassName('notActive');
	$("ccExpire").removeClassName('mandatory');
	$("ccExpire").addClassName('notActive');
	
	$("paymentBox").style.color = "#999";
}

function enablePayment()
{
	$("yourCardNumber").enable();
	$("yourCardType").enable();
	$("yourCardExpMonth").enable();
	$("yourCardExpYear").enable();
	$("iJoin").enable();
	$("iJoinError").style.display = 'none';
	$("iJoinError").removeClassName('warning');
		
	$("introPayment").removeClassName('notActive');
	
	$("iJoinLabel").removeClassName('notActive');
	$("cardTypeLabel").addClassName('mandatory');
	$("cardTypeLabel").removeClassName('notActive');
	$("ccLabel").addClassName('mandatory');
	$("ccLabel").removeClassName('notActive');
	$("ccExpire").addClassName('mandatory');
	$("ccExpire").removeClassName('notActive');
	
	$("paymentBox").style.color = "#000";
}

function checkName()
{
	if ($("SignUpForm").yourName.value != "") {
  		$("NameError").style.display = 'none';
		$("NameLabel").removeClassName('warning');
		$("yourName").removeClassName('warning');
		return false;
  	}
}

function checkStreet()
{
	if (yourZIP) showLocation();
}

function checkZIP()
{
	var theValue = $("SignUpForm").yourZIP.value;
	if (theValue.length == 5 && theValue.charAt(0) == "0") theValue = theValue.slice(1,5);
	var ZIPvalue = parseInt(theValue) - 3101;
	if (( ZIPvalue  > 10) || ( ZIPvalue  < 0) ) {
		$("ZIPError").style.display = 'block';
		$("ZIPLabel").addClassName('warning');
		$("yourZIP").addClassName('warning');	
		yourZIP = false;
	}
	else {
		$("ZIPError").style.display = 'none';
		$("ZIPLabel").removeClassName('warning');
		$("yourZIP").removeClassName('warning');
		yourZIP = true;
		showLocation();
	}
}

function checkRadioState()
{
	if (insideCloud) {
		if ($('iJoin').checked == true) {
			enablePayment();
		}
		else if ($('iJoin').checked == false) {
			disablePayment();
		}
	}
}

function CheckCCNumber() {  
	var ccNumb = $("SignUpForm").yourCardNumber.value;
	var valid = "0123456789"  
	var len = ccNumb.length;  
	var iCCN = parseInt(ccNumb);
	var sCCN = ccNumb.toString();
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');
	var iTotal = 0;  
	var bNum = true; 
	var bResult = false;
	var temp;  
	var calc;  
	
	for (var j=0; j<len; j++) {
	  temp = "" + sCCN.substring(j, j+1);
	  if (valid.indexOf(temp) == "-1"){bNum = false;}
	}
	
	if(!bNum){
	 bResult = false;
	}
	
	
	if((len == 0)&&(bResult)){  
	  bResult = false;
	} else{
	  if(len >= 15){  
		for(var i=len;i>0;i--){  
		  calc = parseInt(iCCN) % 10;
		  calc = parseInt(calc);  
		  iTotal += calc;  
		  i--;  
		  iCCN = iCCN / 10;
		  calc = parseInt(iCCN) % 10 ;
		  calc = calc *2;             
		  switch(calc){
			case 10: calc = 1; break;       
			case 12: calc = 3; break;       
			case 14: calc = 5; break;       
			case 16: calc = 7; break;      
			case 18: calc = 9; break;      
			default: calc = calc;           
		  }                                               
		iCCN = iCCN / 10;  
		iTotal += calc;  
	  } 
	  if ((iTotal%10)==0){ 
		bResult = true;  
	  } else {
		bResult = false; 
		}
	  }
	}

	if(bResult) {
		$("CCError").style.display = 'none';
		$("ccLabel").removeClassName('warning');
		$("yourCardNumber").removeClassName('warning');
	}
	if(!bResult){
	  $("CCError").style.display = 'inline';
	  $("ccLabel").addClassName('warning');
	  $("yourCardNumber").addClassName('warning');
	}
}

function submitMe()
{
	if ($("SignUpForm").yourName.value == "") {
  		$("NameError").style.display = 'inline';
		$("NameLabel").addClassName('warning');
		$("yourName").addClassName('warning');
		$("SignUpForm").yourName.focus();
		return false;
  	}
  	
	if ($("SignUpForm").yourStreet.value == "") {
  		$("StreetError").style.display = 'inline';
		$("StreetLabel").addClassName('warning');
		$("yourStreet").addClassName('warning');
		$("SignUpForm").yourStreet.focus();
		return false;
  	}
  
  if (yourZIP == false || $("SignUpForm").yourZIP.value == "") {
		$("ZIPError").style.display = 'inline';
		$("ZIPLabel").addClassName('warning');
		$("yourZIP").addClassName('warning');
		return false;
  	}
  	eraseCookie('nocheck');
	return true;
}

function ignoreLocation() {
	var query = window.location.search.substring(1);
    if (query == "nocheck") {
		createCookie("nocheck","1",2);
		return true;
	}
	return false;
}

function load() {
	weIgnoreLocation = ignoreLocation()
	var x = readCookie('nocheck')
	if (x) {
		if (x == "1") weIgnoreLocation = true;
	}
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(42.9935,-71.46425), 15);
		map.addControl(new GLargeMapControl());
	    geocoder = new GClientGeocoder();
	    
	    for (var j = 0; j < theCoverage.length; j++) {
       		map.addOverlay(theCoverage[j]);
       	}
  }
}

function loadBig() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("mapBig"));
		map.setCenter(new GLatLng(42.993991,-71.46), 15);
		map.addControl(new GLargeMapControl());
		map.enableContinuousZoom();
	    geocoder = new GClientGeocoder();
	    
       for (var j = 0; j < theCoverage.length; j++) {
       		map.addOverlay(theCoverage[j]);
       	}
	  	
	  	for (var i = 0; i < theLocations.length; i++) {
		  	localMarker = new GMarker(theLocations[i]);
			map.addOverlay(localMarker);
		}
		
		$$('.showOnMap').each(function(item) {
			item.style.display = 'inline';
		});
		
		scrollStop = gy();
		
  }
}
    
function addAddressToMap(response) {
  if (!response || response.Status.code != 200) {
		$("StreetError").style.display = 'inline';
		$("StreetLabel").addClassName('warning');
		$("yourStreet").addClassName('warning');	
		yourStreet = false;
  } else {
	place = response.Placemark[0];
	point = new GLatLng(place.Point.coordinates[1],
						place.Point.coordinates[0]);

	map.clearOverlays();
	for (var j = 0; j < theCoverage.length; j++) {
       		map.addOverlay(theCoverage[j]);
    }
	
	marker = new GMarker(point);
	map.addOverlay(marker);
	
	var insideOneOfUs = false;
	
	for (j = 0; j < theCoverage.length; j++) {
		if (theCoverage[j].Contains(point)) insideOneOfUs = true;
	}
	
	if (insideOneOfUs || weIgnoreLocation) {
		insideCloud = true;
		enablePayment();
		$("SignUpForm").getStarted[0].checked = true;
		$('iJoin').checked = true;
	}
	else {
		insideCloud = false;
		disablePayment();
		$("SignUpForm").getStarted[1].checked = true;
		$('contactMe').checked = true;
	}
  }
}

function showLocation() {
  
  if ($("SignUpForm").yourStreet.value == "") {
  		$("StreetError").style.display = 'inline';
		$("StreetLabel").addClassName('warning');
		$("yourStreet").addClassName('warning');
		$("SignUpForm").yourStreet.focus();
		return false;
  }
  
  if (yourZIP == false || $("SignUpForm").yourZIP.value == "") {
		$("ZIPError").style.display = 'inline';
		$("ZIPLabel").addClassName('warning');
		$("yourZIP").addClassName('warning');
		return false;
  }
  
  $("StreetError").style.display = 'none';
  $("StreetLabel").removeClassName('warning');
  $("yourStreet").removeClassName('warning');
  
  $("ZIPError").style.display = 'none';
  $("ZIPLabel").removeClassName('warning');
  $("yourZIP").removeClassName('warning');
  
  var address = $("SignUpForm").yourStreet.value + "," + $("SignUpForm").yourZIP.value + ", Manchester, NH, US";
  geocoder.getLocations(address, addAddressToMap);
}


GPolygon.prototype.Contains = function(point) {
	var j=0;
	var oddNodes = false;
	var x = point.lng();
	var y = point.lat();
	for (var i=0; i < this.getVertexCount(); i++) {
	  j++;
	  if (j == this.getVertexCount()) {j = 0;}
	  if (((this.getVertex(i).lat() < y) && (this.getVertex(j).lat() >= y))
	  || ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y))) {
		if ( this.getVertex(i).lng() + (y - this.getVertex(i).lat())
		/  (this.getVertex(j).lat()-this.getVertex(i).lat())
		*  (this.getVertex(j).lng() - this.getVertex(i).lng())<x ) {
		  oddNodes = !oddNodes
		}
	  }
	}
	return oddNodes;
 }
 
function showOnMap(response) {
	place = response.Placemark[0];
	point = new GLatLng(place.Point.coordinates[1],
						place.Point.coordinates[0]);

	map.clearOverlays();
	for (var j = 0; j < theCoverage.length; j++) {
    	map.addOverlay(theCoverage[j]);
    }
	
	marker = new GMarker(point);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(lastCalled);
	myInterval = window.setInterval('scroll('+scrollStop+')',10);
}

function mark(me,theName, theAddress) {
      lastCalled = "<p><b>" + theName + "</b> <br /> " + theAddress + "</p>";
      geocoder.getLocations(theAddress, showOnMap);
}

var offsetTop;
var speed = 10;
var myInterval;
var scrollStop;

// returns the Y position of the div
function gy() {
		d = $('mapBig');
		gy = d.offsetTop;
		if (d.offsetParent) while (d = d.offsetParent) gy += d.offsetTop;
		gy -= 20;
		return gy;
}
	
// returns the current scroll position
function scrollTop() {
		body=document.body;
	    d=document.documentElement;
	    if (body && body.scrollTop) return body.scrollTop;
	    if (d && d.scrollTop) return d.scrollTop;
	    if (window.pageYOffset) return window.pageYOffset;
	    return 0;
}

 // move the scroll bar to the particular div.
function scroll(d){
		i = window.innerHeight || document.documentElement.clientHeight;
		h=document.body.scrollHeight;
		a = scrollTop();
		if(d>a)
			if(h-d>i)
				a+=Math.ceil((d-a)/speed)
			else
				a+=Math.ceil((d-a-(h-d))/speed)
		else
			a = a+(d-a)/speed;
		window.scrollTo(0,a);
	  	if(a==d || offsetTop==a) {
	  		window.clearInterval(myInterval);
	  	}
	  	offsetTop=a;
}
