//<SCRIPT language="javascript">
// This line exists to make Interdev Happy and color code the program
/******************************************************************************
	Name   : default.js
	Purpose: Functions for map navigation and commands
*******************************************************************************/
/******************************************************************************
	Name   : GetMap
	Purpose: Returns a pointer to the displayed map

	Parameter                     Description
	============================ ================================================


	Author          Date       Modification
	=============== ========== ==================================================
	BKlima					12-08-2001 Initial release
******************************************************************************/
function GetMap()
{
	var map;

	try	{

		map = top.frmMap?top.frmMap.MapSVG:document.MapSVG;

	}
	catch(e)
	{
		return;
	}
	finally {
		return map;
	}
}

function GetMapExtents()
{
	var charPos = 0;                  // character position within string
	var extents;					// string of map extents
	var extent;						// map extent
    var mapExtents = new Array();   // array of map extents
	var map = GetMap();            // pointer to main map
	var sep;	
			
    // make sure a map exists  
    if(typeof(map) == "undefined")
    {
		alert('This function requires a map to be displayed.');
		mapExtents = "";
	} else {

		// get extents of selected map based on display area
        extents = map.getViewRange();
	    sep = map.getListSeparator();	   
        // parse extent information create the extent array
        for (i=0; i < extents.length; i++)
        {
            if (extents.charAt(i) == sep)
            {
                extent = (extents.substring(charPos,i));
                if (extent != null && extent.length != 0)
                {
                    mapExtents[mapExtents.length] = parseFloat(extent);
                }
                charPos = i + 1;
            }
        }
    }
	extent = (extents.substring(charPos));
	mapExtents[mapExtents.length] = parseFloat(extent);
	
	return mapExtents;
}

function openMap(queryString) 
{
	var url = "Map.asp?";
	if (queryString.length != 0 ) url+= queryString + "&";
	url+="width=" + window.screen.availWidth + "&height=" + window.screen.availHeight;	
	//url+="width=" + top.document.body.offsetWidth + "&height=" + top.document.body.offsetHeight;	
	window.open(url,"frmMap");
	return;
}
/******************************************************************************
	Name   : GetMapParameters
	Purpose: Returns a array of storage parameters (scale, xoffset,yoffset) values
	         for the displayed map 
	
	Parameter                    Description
	============================ ================================================
	

	Author          Date       Modification
	=============== ========== ==================================================
	BKlima					12-08-2001 Initial release
******************************************************************************/
function GetMapParameters(){
    
    var map = GetMap();                   // pointer to map object
    var sheets = "";                      // list of all sheets from map
	var sheet = "";														// individual sheet
	var sheetsArray = new Array();        // array of sheets
	var parameterArray = new Array();     // array of map parameters
	var modParameterArray = new Array();  // array of parameters wanted

		// map object must exist
    if(typeof(map) == "undefined") {
			modParameterArray="";
		}
		else {
     	// get sheet list and parse out sheets into array
			sheets = map.MLCommand ("querysheets");  
			// Char Code 10 is a line feed
			sheetsArray = ParseString(sheets, 10); 

			// only interested in first sheet
			sheet = sheetsArray[0];
			
			// parse out map parameters 
			// Char Code 59 is a ;
			parameterArray = ParseString(sheet, 59);

			// only interested in scale and xy offsets
			//modParameterArray = new Array(parameterArray[2],parameterArray[3],parameterArray[4]);
			
			//Modified by Marcel to include additional parameters
			// only interested in ScaleModeVal, scale and xy offsets, and MeasureFactor
			// Array(0)= MapOffsetX Array(1)=MapOffsetX Array(2)=MapScale Array(3)=ScaleModeVal Array(4)=MeasureFactor
			modParameterArray = new Array(parameterArray[2],parameterArray[3],parameterArray[4],parameterArray[1],parameterArray[10]);
    }
		return modParameterArray;
}
/******************************************************************************
	Name   : ParseString
	Purpose: Returns a array of strings from an input string delimited by a
	         specified charater
	
	Parameter                    Description
	============================ ================================================
	stringValue                  string to be parsed
	charCode                     acii value of delimiting character

	Author          Date       Modification
	=============== ========== ==================================================
	BKlima					12-08-2001 Initial release
******************************************************************************/
function ParseString(stringValue, charCode){

	var parseArray = new Array();  // array of parsed values
  var charPos = 0;               // position within parsed string
	var parsed = "";               // the parsed value

	// parse string for delimiting character and get value
  for (i=0; i<stringValue.length; i++){
		if (stringValue.charCodeAt(i) == charCode){
			parsed = stringValue.substring(charPos,i);
      if (parsed.length != 0 && parsed != null){
				parseArray[parseArray.length] = parsed;
      }
      charPos = i + 1;
    }
  }
  
  if (stringValue.length > charPos) 
		parseArray[parseArray.length] = stringValue.substring(charPos,stringValue.length);
  
  return parseArray;
}

function OpenThematicDialog(iId , strFeature ,wHeight, range, controlWidth, controlHeight)
{
		window.showModelessDialog('thematic.asp?id=' + iId + '&name=' + strFeature + '&rng=' + range + '&c_width=' + controlWidth + '&c_height=' + controlHeight,'Thematic','status:no;resizable:yes;help:no;dialogWidth:180px;dialogHeight:' + wHeight + 'px' );
}

function addField (form, fieldType, fieldName, fieldValue, style, eHnd, mouseOverHandler, mouseOutHandler) {
  
    var input = parent.frmApply.document.createElement('INPUT');
        input.setAttribute('type', fieldType);
        input.setAttribute('name', fieldName);
		input.setAttribute('id', fieldName);
        input.setAttribute('value', fieldValue);
        
              
        if (style.indexOf(":") > -1)
		    input.style.cssText = style;  
		else  		
		    input.className = style;
		
		if (typeof(mouseOverHandler) != "undefined")
		    if (typeof(mouseOverHandler) == "function")
		        input.onmouseover = mouseOverHandler;
		
		if (typeof(mouseOutHandler) != "undefined")
		    if (typeof(mouseOutHandler) == "function")
		        input.onmouseout = mouseOutHandler;
							   
		if (typeof(eHnd) != 'undefined')
			input.attachEvent("onclick", eHnd);
    	
		form.appendChild(input);  
}

function addButton (form, fieldType, fieldName, fieldValue, style, clickHandler)
{     
    var container = parent.frmApply.document.createElement("DIV");
    var leftSide = parent.frmApply.document.createElement("DIV");
    var button = parent.frmApply.document.createElement("INPUT");
    var lagendArrow = parent.frmApply.document.createElement("DIV");
    var rightSide = parent.frmApply.document.createElement("DIV"); 
    
    container.style.marginBottom = "3px";
    container.style.width = "220px";
    container.style.clear = "all";
    container.onmouseover = function()
    {
       leftSide.style.backgroundImage = "url('images/mainButton_leftOver.png')"; 
       button.style.backgroundImage = "url('images/mainButton_centerOver.png')";
       rightSide.style.backgroundImage = "url('images/mainButton_rightOver.png')";
    }
    
    container.onmouseout = function()
    {
        leftSide.style.backgroundImage = "url('images/mainButton_left.png')"; 
        button.style.backgroundImage = "url('images/mainButton_center.png')";
        rightSide.style.backgroundImage = "url('images/mainButton_right.png')";
    }
        
    button.setAttribute("type", fieldType);
    button.setAttribute("name", fieldName);
    button.setAttribute("id", fieldName);
    button.setAttribute("value", fieldValue);
    
    if (style.indexOf(":") > -1)
        button.style.cssText = style;
    else 
        button.className = style;
        
    if (typeof(clickHandler) != "undefined")
        button.attachEvent("onclick", clickHandler);
            
    leftSide.style.styleFloat = "left";          
    leftSide.style.backgroundImage = "url('images/mainButton_left.png')";
    leftSide.style.width = "9px";
    leftSide.style.height = "20px";
    
    rightSide.style.styleFloat = "left";   
    rightSide.style.backgroundImage = "url('images/mainButton_right.png')";
    rightSide.style.width = "9px";
    rightSide.style.height = "20px";
        
    container.appendChild(leftSide);
    container.appendChild(button);
    container.appendChild(rightSide);
    
    form.appendChild(container);  
}

function getField (form, fieldName) {
    for (var e = 0; e < form.elements.length; e++)
	{
      if (form.elements[e].name == fieldName)
        return form.elements[e];
	}
  return null;
}

function removeField (form, fieldName) {
  var field = getField (form, fieldName);
  if (field && !field.length)
    field.parentNode.removeChild(field);
}

function toggleField (form, fieldName, value) {
  var field = getField (form, fieldName);
  if (field)
    removeField (form, fieldName);
  else
    addField (form, 'hidden', 'button', 'click');
}
function reloadSearchFrame(){
	parent.frames["frmSearch"].location.reload();
}
function feloadApplyFrame(){
	parent.frames["frmApply"].location.reload(); //= 'searches_apply.asp?sType=' + sTypesArr[3];
}
function clearForm(form){
	var tempArray = new Array(form.elements.length);		
	var eNum = form.elements.length;
	
	for(var index = 0; index < eNum; index++)
	{
	    var child = form.childNodes[0];
	    if (child != "undefined")
	        form.removeChild(child);
	}
	
	//get field names
	/*
	for (var e = 0; e < eNum; e++)
		tempArray[e] = form.elements[e].name;
	//remove fields	
	for (var i = 0; i < eNum; i++)
		removeField (form, tempArray[i]);
	*/
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function IsNumeric(PossibleNumber)
{
	var PNum = new String(PossibleNumber);
	var regex = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	return regex.test(PNum);
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
//</SCRIPT>

function httpRequest(method, url, sync)
{
    var xmlHttp;
    
    try
    {   //For FireFox and Safari
        xmlHttp = new XMLHttpRequest();
    } 
    catch(e)
    {
        try 
        {   //For Internet Explorer
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            alert("Your browser doesn't support AJAX");
            return;
        }
    }
               
    xmlHttp.onreadystatechange = function()
    {       
        if (xmlHttp.readyState == 4)
            AjaxDetails(xmlHttp.responseText);
    }
    
    xmlHttp.open(method, url, sync);
    xmlHttp.send(null);
}

function AjaxDetails(details)
{
    alert(details); return;
    if (details != null)
        if (details.length > 0)
            window.location = details;
}

function mouseOver(left, middle, right)
{
    var dvLeft = document.getElementById(left);
    var dvMiddle = document.getElementById(middle);
    var dvRight = document.getElementById(right);
            
    dvLeft.style.background = "url('images/mainButton_leftOver.png')";
    dvMiddle.style.background = "url('images/mainButton_centerOver.png')";
    dvRight.style.background = "url('images/mainButton_rightOver.png')";	    
}

function mouseOut(left, middle, right)
{
    var dvLeft = document.getElementById(left);
    var dvMiddle = document.getElementById(middle);
    var dvRight = document.getElementById(right);
    
    dvLeft.style.background = "url('images/mainButton_left.png')";
    dvMiddle.style.background = "url('images/mainButton_center.png')";
    dvRight.style.background = "url('images/mainButton_right.png')";	    	    
}


