 /*   Alaska */
 points = [
  {"state":"NP","pos" : [
	{x: 0,   y: 0},
	{x: 0, y: 0},
	{x: 0, y: 0},
	{x: 0, y: 0 }
]},
 {"state":"Alaska","pos" : [
	{x: 18,   y: 345},
	{x: 98, y: 345},
	{x: 101, y: 434},
	{x: 18, y: 434 }
]},
 /* Hawaii */
 {"state":"Hawaii","pos": [
 {x: 134, y: 354},
 {x: 242, y: 354},
 {x: 242, y: 439},
 {x: 134, y: 439}
 ]}
 , 
  /* Washington */
 {"state":"Washington","pos": [
 {x: 223, y: 48},
 {x: 309, y: 48},
 {x: 309, y: 90},
 {x: 223, y: 90}
 ]}
 ,
  /* Oregon */
 {"state":"Oregon","pos": [
 {x: 234, y: 112},
 {x: 297, y: 112},
 {x: 297, y: 146},
 {x: 234, y: 146}
 ]},
   /* california */
 {"state":"california","pos": [
 {x: 225, y: 200},
 {x: 275, y: 200},
 {x: 337, y: 274},
 {x: 248, y: 297}
 ]},
 
    /*5 idaho */
 {"state":"idaho","pos":[
 {x: 320, y: 110},
 {x: 376, y: 110},
 {x: 376, y: 151},
 {x: 320, y: 151} 
 ]},
     /* Nevada */
 {"state":"Nevada","pos": [
 {x: 283, y: 175},
 {x: 340, y: 175},
 {x: 340, y: 231},
 {x: 307, y: 228},
 {x: 282, y: 204} 
 ]},
      /* Arizonaa */
 {"state":"Arizonaa","pos": [
 {x: 350, y: 263},
 {x: 408, y: 263},
 {x: 408, y: 317},
 {x: 350, y: 317} 
 ]},
 
     /* Utah */
 {"state":"Utah","pos": [
 {x: 351, y: 175},
 {x: 405, y: 175},
 {x: 405, y: 233},
 {x: 351, y: 233}
 ]},
      /* wyoming */
 {"state":"wyoming","pos": [
 {x: 391, y: 119},
 {x: 462, y: 119},
 {x: 462, y: 167},
 {x: 391, y: 167} 
 ]},
      /*10 Montana */
 {"state":"Montana","pos": [
 {x: 355, y: 39},
 {x: 463, y: 39},
 {x: 463, y: 102},
 {x: 355, y: 102} 
 ]},
      /* North dakota */
 {"state":"North dakota","pos": [
 {x: 478, y: 39},
 {x: 556, y: 39},
 {x: 556, y: 84},
 {x: 478, y: 84} 
 ]},
 
      /* SOUTH DAKOTA */
 {"state":"SOUTH DAKOTA","pos": [
 {x: 479, y: 97},
 {x: 560, y: 97},
 {x: 560, y: 138},
 {x: 479, y: 138} 
 ]},
      /* NEBRASKA */
 {"state":"NEBRASKA","pos": [
 {x: 485, y: 150},
 {x: 562, y: 150},
 {x: 570, y: 185},
 {x: 506, y: 181}
 ]},
       /* KANSAS */
 {"state":"KANSAS","pos": [
 {x: 505, y: 202},
 {x: 580, y: 202},
 {x: 580, y: 236},
 {x: 505, y: 236}
 ]},
       /*15 Colorado */
 {"state":"Colorado","pos":[
 {x: 418, y: 183},
 {x: 490, y: 183},
 {x: 490, y: 228},
 {x: 418, y: 228}
 ]},
       /* New Mexico */
 {"state":"New Mexico","pos": [
 {x: 417, y: 249},
 {x: 477, y: 249},
 {x: 477, y: 311},
 {x: 417, y: 311} 
 ]},
       /* Texas */
 {"state":"Texas","pos": [
 {x: 494, y: 287},
 {x: 594, y: 287},
 {x: 594, y: 379},
 {x: 494, y: 379}
 ]},
 
     /* Oklahoma */
 {"state":"Oklahoma","pos":[
 {x: 525, y: 246},
 {x: 590, y: 246},
 {x: 590, y: 283},
 {x: 525, y: 283} 
 ]},
        /* Louisiana */
 {"state":"Louisiana","pos": [
 {x: 603, y: 324},
 {x: 665, y: 324},
 {x: 665, y: 360},
 {x: 603, y: 360} 
 ]},
        /*20 Mississippi */
 {"state":"Mississippi","pos":[
 {x: 613, y: 283},
 {x: 690, y: 283},
 {x: 690, y: 299},
 {x: 613, y: 299} 
 ]},
        /* Arkansas */
 {"state":"Arkansas","pos":[
 {x: 594, y: 252},
 {x: 650, y: 252},
 {x: 650, y: 281},
 {x: 594, y: 281} 
 ]},
 
        /* Missouri */
 {"state":"Missouri","pos":[
 {x: 587, y: 198},
 {x: 654, y: 198},
 {x: 654, y: 233},
 {x: 587, y: 233} 
 ]},
 
        /* Iowa */
 {"state":"Iowa","pos": [
 {x: 580, y: 145},
 {x: 627, y: 145},
 {x: 627, y: 179},
 {x: 580, y: 179} 
 ]},
       /* Minnesota */
 {"state":"Minnesota","pos": [
 {x: 565, y: 51},
 {x: 623, y: 51},
 {x: 623, y: 126},
 {x: 565, y: 126} 
 ]}, 
    /*25 Wisconsin */
 {"state":"Wisconsin","pos": [
 {x: 625, y: 59},
 {x: 683, y: 59},
 {x: 683, y: 134},
 {x: 625, y: 134} 
 ]},
    /* illinios */
 {"state":"illinios","pos": [
 {x: 637, y: 171},
 {x: 676, y: 171},
 {x: 676, y: 203},
 {x: 637, y: 203} 
 ]}, 
    /* Michigan */
 {"state":"Michigan","pos": [
 {x: 692, y: 117},
 {x: 744, y: 117},
 {x: 744, y: 157},
 {x: 692, y: 157} 
 ]}, 
    /* Indiana */
 {"state":"Indiana","pos": [
 {x: 677, y: 175},
 {x: 724, y: 175},
 {x: 724, y: 204},
 {x: 677, y: 204} 
 ]} ,
    /* Ohio */
 {"state":"Ohio","pos": [
 {x: 723, y: 166},
 {x: 758, y: 166},
 {x: 758, y: 194},
 {x: 723, y: 194} 
 ]}, 
   /*30 Kentury */
 {"state":"Kentury","pos": [
 {x: 682, y: 222},
 {x: 743, y: 222},
 {x: 743, y: 244},
 {x: 705, y: 244} 
 ]}, 
    /* Tennessee */
 {"state":"Tennessee","pos": [
 {x: 657, y: 248},
 {x: 726, y: 248},
 {x: 726, y: 269},
 {x: 657, y: 269} 
 ]},
    /* Alambama */
 {"state":"Alambama","pos": [
 {x: 662, y: 303},
 {x: 718, y: 303},
 {x: 718, y: 316},
 {x: 662, y: 316} 
 ]},
    /* Georgia */
 {"state":"Georgia","pos":[
 {x: 707, y: 279},
 {x: 736, y: 281},
 {x: 740, y: 302},
 {x: 762, y: 316},
 {x: 764, y: 331},
 {x: 712, y: 330},
 {x: 709, y: 317},
 {x: 726, y: 314} 
 ]},
     /* Florida */
 {"state":"Florida","pos": [
 {x: 708, y: 336},
 {x: 805, y: 336},
 {x: 805, y: 427},
 {x: 708, y: 427} 
 ]},
 
      /*35 South Carolina */
 {"state":"South Carolina","pos": [
  {x: 742, y: 279},
 {x: 801, y: 279},
 {x: 801, y: 308},
 {x: 742, y: 308}  
 ]},
      /* North Carolina */
 {"state":"North Carolina","pos":[
 {x: 763, y: 250},
 {x: 816, y: 250},
 {x: 816, y: 277},
 {x: 763, y: 277} 
 ]},
      /* Virginia */
 {"state":"Virginia","pos": [
 {x: 769, y: 231},
 {x: 820, y: 231},
 {x: 820, y: 246},
 {x: 769, y: 246} 
 ]},
      /* West Virginia */
 {"state":"West Virginia","pos": [
 {x: 738, y: 204},
 {x: 790, y: 204},
 {x: 790, y: 227},
 {x: 738, y: 227} 
 ]},
      /* Pennsylvania */
 {"state":"Pennsylvania","pos": [
 {x: 766, y: 161},
 {x: 847, y: 161},
 {x: 847, y: 196},
 {x: 766, y: 196} 
 ]},
      /*40 New York */
 {"state":"New York","pos": [
 {x: 788, y: 135},
 {x: 835, y: 109},
 {x: 845, y: 165},
 {x: 786, y: 155} 
 ]},
      /* Maine */
 {"state":"Maine","pos": [
 {x: 887, y: 103},
 {x: 914, y: 76},
 {x: 939, y: 110},
 {x: 926, y: 126},
 {x: 891, y: 119} 
 ]},
      /* Vermont */
 {"state":"Vermont","pos": [
 {x: 931, y: 139},
 {x: 997, y: 139},
 {x: 997, y: 156},
 {x: 931, y: 156} 
 ]},
     /* New Hampshire */
 {"state":"New Hampshire","pos": [
 {x: 923, y: 158},
 {x: 1023, y: 158},
 {x: 1023, y: 173},
 {x: 923, y: 173} 
 ]} ,
      /* Massachusetts */
 {"state":"Massachusetts","pos":[
 {x: 912, y: 177},
 {x: 1014, y: 177},
 {x: 1014, y: 191},
 {x: 912, y: 191} 
 ]},
      /*45 Rhode Island */
 {"state":"Rhode Island","pos": [
 {x: 891, y: 192},
 {x: 983, y: 192},
 {x: 983, y: 210},
 {x: 891, y: 210} 
 ]},
   /* Connecticut */
 {"state":"Connecticut","pos": [
 {x: 865, y: 213},
 {x: 954, y: 213},
 {x: 954, y: 231},
 {x: 865, y: 231} 
 ]},
   /* New Jersey */
 {"state":"New Jersey","pos": [
 {x: 876, y: 238},
 {x: 958, y: 238},
 {x: 958, y: 257},
 {x: 876, y: 257} 
 ]},
   /* Delaware */
 {"state":"Delaware","pos": [
 {x: 868, y: 257},
 {x: 936, y: 257},
 {x: 936, y: 273},
 {x: 868, y: 273} 
 ]},
   /* MaryLand */
 {"state":"MaryLand","pos": [
 {x: 845, y: 270},
 {x: 913, y: 270},
 {x: 913, y: 288},
 {x: 845, y: 288} 
 ]},
   /*50 DC */
 {"state":"District of Columbia","pos":[
 {x: 809, y: 289},
 {x: 881, y: 289},
 {x: 881, y: 318},
 {x: 809, y: 318} 
 ]
 }]
 function getEventOffsetXY(evt) {
	if (evt.offsetX != null)
		return [evt.offsetX, evt.offsetY];

	var obj = evt.target || evt.srcElement;
	setPageTopLeft(obj);
	return [(evt.clientX - obj.pageLeft), (evt.clientY-obj.pageTop)];
}
function setPageTopLeft(o) {
	var top = 0,
	left = 0,
	obj = o;

	while(o.offsetParent) {
		left += o.offsetLeft;
		top += o.offsetTop;
		o = o.offsetParent;
	}

	obj.pageTop = top;
	obj.pageLeft = left;
}
 function ck(event){
	 thePos = new Array(); 
	 thePos = getEventOffsetXY(event);
	 pos_x = thePos[0];
	 pos_y = thePos[1]; 
	 document.pointform.form_x.value = pos_x;
	 document.pointform.form_y.value = pos_y;
	 for (x = 0; x < points.length; x++) {
	 	console.log(x);
	 	if(isPointInPoly(points[x].pos, {x: pos_x,y: pos_y}) ){
			document.pointform.state.value=points[x].state;
		//	alert(points[x].state);
			document.pointform.submit()
		}
	 }
 }
 
 function point_it(event){
	 thePos = new Array();
	 thePos = getEventOffsetXY(event);
	var pos_x = thePos[0];
	var pos_y = thePos[1];
	// clearZip();
	// clearState();
	// clearPhrase();
	if(pos_x > 208)
	 document.pointform.form_x.value = pos_x;
	 document.pointform.form_y.value = pos_y;
	
	 // Seattle
	 var s_x = 70; //change this
	 var s_y = 62; //change this
	 var s_lat = 47.45;
	 var s_lng = -122.30;
	 // Miami
	 var m_x = 552; //change this
	 var m_y = 383; //change this
	 var m_lat = 25.65;
	 var m_lng = -80.43;
	 // Kansas City, MO - calculate from here
	 start_x = 381; //change this
	 start_y = 200; //change this
	 start_lat = 39.02;
	 start_lng = -94.58
	 var changein_y = m_y - s_y;
	 var changein_x = m_x - s_x;
	 var changein_lat = m_lat - s_lat;
	 var changein_lng = m_lng - s_lng ;
	// Seattle
	 var s_x = 242; //70; //change this
	 var s_y = 60; //62; //change this
	 var s_lat = 47.45;
	 var s_lng = -122.30;
	
	 // Miami
	 var m_x = 773; //552; //change this
	 var m_y = 406; //383; //change this
	 var m_lat = 25.65;
	 var m_lng = -80.43;
	
	 // Kansas City, MO - calculate from here
	 start_x = 593; //381; //change this
	 start_y = 207; //200; //change this
	 start_lat = 39.02;
	 start_lng = -94.58

	 var changein_y = m_y - s_y;
	 var changein_x = m_x - s_x;
	 var changein_lat = m_lat - s_lat;
	 var changein_lng = m_lng - s_lng ;
	 var d_lat = changein_lat / changein_y;
	 var d_lng = changein_lng / changein_x;
	
	 var lat = start_lat + ((pos_y - start_y) * d_lat);
	 var lng = ((pos_x - start_x) * d_lng) + start_lng;
	
	 document.getElementById('theLong').value = lng;
	 document.pointform.lat.value = lat;

	 function isPointInPoly(poly, pt) {
		 for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
			 ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
			 && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
			 && (c = !c);
		 return c;
	 }

	 /* Alaska */
	 pointsAK = [
	 {x: 18,   y: 345},
		{x: 98, y: 345},
		{x: 101, y: 434},
		{x: 18, y: 434 }
	 ];
	 /* Hawaii */
	 pointsHI = [
	 {x: 134, y: 354},
	 {x: 242, y: 354},
	 {x: 242, y: 439},
	 {x: 134, y: 439}
	 ];
	  /* searchbox */
	 pointsSRH = [
	 {x: 0, y: 0},
	 {x: 205, y: 0},
	 {x: 205, y: 304},
	 {x: 0, y: 304}
	 ];
	 $inAK = (isPointInPoly(pointsAK, {x: pos_x, y: pos_y}) ? "Alaska" : "Out");
	 $inHI = (isPointInPoly(pointsHI, {x: pos_x, y: pos_y}) ? "Hawaii" : "Out");
	 $insrch = (isPointInPoly(pointsSRH, {x: pos_x, y: pos_y}) ? "searchbox" : "Out");
	  if ($inAK == "Alaska") {
	 	document.pointform.state.value='Alaska';
		document.getElementById('theLong').value = '';
	 	document.pointform.lat.value = '';
		document.pointform.submit();
	 } else if ($inHI == "Hawaii") {
		document.pointform.state.value='Hawaii';
		document.getElementById('theLong').value = '';
	 	document.pointform.lat.value = '';
		document.pointform.submit();
	 } else if ($insrch == "searchbox" || pos_y < 0 || pos_x < 0) {
		document.pointform.state.value='';

	 } else {
		 document.pointform.submit();
	 }
} 
 
 function isPointInPoly(poly, pt) {
		for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) {
			var a = (poly[i].y <= pt.y ? true : false);
			var aa = (pt.y < poly[j].y ? true : false);
			var b = (poly[j].y <= pt.y ? true : false);
			var bb = (pt.y < poly[i].y ? true : false);
			var d = (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x;
			if(isNaN(d) ) d=0;
			((a && aa) || (b && bb)) && (pt.x < d) && (c = !c);
		}	
		return c;
	}
