// catawba county javascript functions called from various web pages
// r. dell 04/07/06

function GetScreenDimensions() {
// get the dimensions of the screen 
	var myWidth, myHeight;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
//  alert( 'Width = ' + myWidth );
//  alert( 'Height = ' + myHeight );
  return [myWidth,myHeight];

}

function printPage() {
// open the browsers file print box
  if (window.print) {
    window.print();
  }
  return;
}

function go(url) {
// this will replace the current url in the history
// in the case of voter locator, default.asp will not go in the history
// and the back button will not end up looping on itself
	if (document.images) {
        location.replace(url);
    }else{
        location.href = url;
	}
	return;	
}

function disableZoomBoxSettings() {
// when the select options box is over the map and a option in the box is clicked
// it is interpertated as a mouse click on the map. This code disables that behavior
// so a mouse click on a select option sets the option.
// active.js function setZoomBoxSettings() assigns functions to these mouse actions.
	document.onmousemove = '';
	document.onmousedown = '';
	document.onmouseup = '';
	return;
}

function setFocus(id) {
// sets the focus on the element specified by the id
// usually this will be a form text box

//	document.PincForm.pinc.focus();	//works

//	var id = "pinc_id";		//these three lines work,form id
//	var frm=document.getElementById(id);
//	frm.pinc.focus();

//	var id = "xxx";			//these three lines work, text box id
//	var frm=document.getElementById(id);
//	frm.focus();

	var element=document.getElementById(id);
	element.focus();
	return;
}

//function clearString_one() {
// clear the value of the first input box on the first form
// and set the focus to the input box
//	window.document.forms[0][0].value = "";
//	window.document.forms[0][0].focus();	
//}

function ccgis_calc_length(x1,y1,x2,y2) {
	// caculate length of a line segment
	// a squared + b squared = c squared
	// 10/23/07
	var len = 0;
	var lenX = x2 - x1;
	lenX = lenX * lenX;
	var lenY = y2 - y1;
	lenY = lenY * lenY;
	len = lenX + lenY;
	len = Math.sqrt(len);
	len = Math.round(len);
	//alert(len);
	return len;
}

function ccgis_calc_area_triangle(a,b,c){
	// area of a triangle
	// http://www.onlineconversion.com/shape_area_scalene_triangle.htm
	// 10/23/07
	var s = ( a + b + c)/2;			//half of perimiter
	ar = Math.sqrt(s * (s-a) * (s-b) * (s-c));
	ar = Math.round(ar);
	//alert(ar);
	return ar;
}

function ccgis_calc_area_poly(N,aX,aY){
// area of polygon, N is the number of vertices
// aX and aY are arrays of the x,y coordinates
// http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
// 10/23/07
	var ar = 0;
	for ( i=0; i<N; i++) {
		j= (i + 1) % N;
		ar += (aX[i] * aY[j]);
		ar -= (aY[i] * aX[j]);
	}
	ar = ar / 2;
	ar = Math.abs(Math.round(ar));
	return ar;
}	

function ccgis_convertSP(uX,uY) {
// Code to convert from map units to decimal degrees.
// Code based on asp code from Gerry Daumiller, Montana State Library 8-23-02 email.
// ASP code on web at http://nris.state.mt.us/gis/projection/projection.html.
// Translated to javascript by Jeff Miller, Jefferson County, WA.
// The formulae this program is based on are from "Map Projections,
// A Working Manual" by John P. Snyder, U.S. GeoLogical Survey
// Professional Paper 1395, 1987, pages 295-298
// 01/29/08 modified for Catawba County NC, r. dell


	a = 20925604.48;									//major radius of ellipsoid, map units (NAD 83)
	ec = 0.08181905782;								//eccentricity of ellipsoid (NAD 83)
	angRad = 0.01745329252;						//number of radians in a degree
	//pi4 = Math.PI / 4;								//Pi / 4
	//p0 = 33.75 * angRad;							//latitude of origin for NC
	//p1 = 34.33333333  * angRad;				//latitude of first standard parallel for NC
	//p2 = 36.16666667 * angRad;				//latitude of second standard parallel for NC
	//m0 = -79   * angRad;							//central meridian for NC
	//alert(pi4 + "," + p0 + "," + p1 + "," + p2 + "," + m0);
	
	pi4 = .7853981633974483;					// set the constants rather than do the math over and over and over 
	//p0 = .58904862255;
	//p1= .5992297097951558;
	//p2 = .6312274128648443;
	m0 = -1.37881010908;
	x0 =  2000000.00261667						//False easting of central meridian, map units for NC
	
	// Calculate the coordinate system constants.
	with (Math) {
		//m1 = Math.cos(p1) / Math.sqrt(1 - (Math.pow(ec,2)) * Math.pow(Math.sin(p1),2));
		//m2 = cos(p2) / sqrt(1 - (pow(ec,2)) * pow(sin(p2),2));
		//t0 = tan(pi4 - (p0 / 2));
		//t1 = tan(pi4 - (p1 / 2));
		//t2 = tan(pi4 - (p2 / 2));
		//alert(m1 + "," + m2 + "," + t0 + "," + t1 + "," + t2);
		
		//m1 = .8266509534758518;				// set the constants rather than do the math over and over and over 
		//m2 = .8082464911302531;
		//t0 = .5345111359495613;
		//t1 = .5279839169165926;
		//t2 = .507694767361522;
		
		//t0 = t0 / pow(((1 - (ec * (sin(p0)))) / (1 + (ec * (sin(p0))))),ec/2);  
		//t1 = t1 / pow(((1 - (ec * (sin(p1)))) / (1 + (ec * (sin(p1))))),ec/2);
		//t2 = t2 / pow(((1 - (ec * (sin(p2)))) / (1 + (ec * (sin(p2))))),ec/2);
		//n = log(m1 / m2) / log(t1 / t2);
		//f = m1 / (n * pow(t1,n));
		//rho0 = a * f * pow(t0,n);
		
		//t0 = .5365041597468421;			// set the constants rather than do the math over and over and over 
		//t1 = .5299825960227652;
		//t2 = .5097059914770569;
		n = .5771702552416648;
		f = 2.0661708379798207;
		rho0 = 30182963.14039404;
		//alert(t0 + "," + t1 + "," + t2 + "," + n + "," + f + "," + rho0);
		
		// Convert the coordinate to Latitude/Longitude.

		// Calculate the Longitude.
		uX = uX - x0;
		pi2 = pi4 * 2;

		rho = sqrt(pow(uX,2) + pow((rho0 - uY),2));  
		theta = atan(uX / (rho0 - uY));
		txy = pow((rho / (a * f)),(1 / n));
		lon = (theta / n) + m0;
		uX = uX + x0;

		// Estimate the Latitude
		lat0 = pi2 - (2 * atan(txy));

		// Substitute the estimate into the iterative calculation that
		// converges on the correct Latitude value.
		part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
		lat1 = pi2 - (2 * atan(txy * pow(part1,(ec/2))));

		while ((abs(lat1 - lat0)) > 0.000000002) {
  		lat0 = lat1;
  		part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
  		lat1 = pi2 - (2 * atan(txy * pow(part1,(ec/2))));
  	}

		// Convert from radians to degrees. 
		Lat = lat1 / angRad;
		Lon = lon / angRad;
	}
	
	return Lon + "|" + Lat;		//(DEC, DDD.DDDDD, Decimal Degrees)

}

function ccgis_convertDMS(Lon,Lat1,fmt) {
// Calcuate degrees, minutes, and seconds.
// added 1/29/08
	var dLat = parseInt(Lat1);
	var mLat = 60 * (Lat1 - dLat);
	var sLat = 60 * (mLat - parseInt(mLat));
	var sLat = ccgis_round(sLat,2);
	mLat = parseInt(mLat);

	Lon = Math.abs(Lon);
	var dLon = parseInt(Lon);
	var mLon = 60 * (Lon - dLon);
	var sLon = 60 * (mLon - parseInt(mLon));
	var sLon = ccgis_round(sLon,2);
	mLon = parseInt(mLon);
	
// Adjust minutes and seconds, in case they have rounded to 60.
	if (sLat == 60.00) {
		sLat = 0;
		mLat = mLat + 1;
	}
	if (mLat >= 60) {
		mLat = mLat - 60;
		dLat = dLat + 1;
	}
	if (sLon == 60.00) {
		sLon = 0;
		mLon = mLon + 1;
	}
	if (mLon >= 60) {
		mLon = mLon - 60;
		dLon = dLon - 1;
	}
	var LLdms;
	if (fmt){
		LLdms = "-" + dLon + " " + mLon + "' " + sLon + "''|" + dLat + " " + mLat + "' " + sLat + "''";
	}else{
		LLdms = "-" + dLon + "!" + mLon + "!" + sLon + "|" + dLat + "!" + mLat + "!" + sLat;
	}	
	return LLdms;
}

	function ccgis_round(number,X) {
// rounds number to X decimal places, defaults to 2
// added 1/29/08
    X = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function hello() {
	alert("hello world");
	return;
}

function check_host() {
// alert users that are linking to gis with 204.211.226.33 that the 
// link is going to stop working soon
// added 02/26/08
	if (location.host == '204.211.226.33') {
		var val = getCookie('ip_alert');
		if (val != 'ip2') {
			var ip = window.open("ip_alert.html","ip_alert","height=400,width=600,scrollbars=0,resizable=0");
			ip.focus();
			setCookie('ip_alert','ip2',1);
		}	
	}
	return;
}	

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	return true;
}

function getCookie(c_name) {
// return the value of the cookie in the name=value	pair
	if (document.cookie.length>0) {
  	c_start=document.cookie.indexOf(c_name + "=");
  	if (c_start!=-1) { 
    	c_start=c_start + c_name.length+1;
    	c_end=document.cookie.indexOf(";",c_start);
    	if (c_end==-1) c_end=document.cookie.length;
    	return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
	return "";
}

