// --------------------------------------------------------------------------------
// This file contains all of the general javascript functions used by both the 
// Arctic Sea Ice and Domestic gfl map services
// --------------------------------------------------------------------------------

// These are the general global variables used in the maps
// Parameters for browser identification
var browser;
var b_version;

var ipath;

function detectBrowser() {
  browser = navigator.appName;
  b_version =  navigator.appVersion;
  if (browser == "Microsoft Internet Explorer") {
    if (b_version.indexOf("MSIE 6.0") != -1) {b_version = "MSIE 6.0"}
  }  
  //alert('browser is: ' + browser + b_version);
} // end detectBrowser

function showCoordinates(evt) {
   //get mapPoint from event
   var mp = evt.mapPoint;
   //display mouse coordinates
   //dojo.byId("coords").innerHTML = "<font color=black>X: " + mp.x + ", Y: " + mp.y + "</font>"
} 

function ZoomInitialExtent(){
    map.setExtent(startExtent);
}

function changeGalleryDiv(the_div,the_change){
	var the_style = getStyleObject(the_div);
	if (the_style != false) {
		the_style.display = the_change;
	}
} //changeDiv

function changeDiv(the_div){
    var the_style = getStyleObject(the_div);
    if (the_style != false) {
     if (the_style.display == "none")
      the_style.display = "block";
    else
      the_style.display = "none";
    }
}

function getStyleObject(objectId) {
	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId).style;
	} else {
		return false;
	}
} //getStyleObject	

function showLoading() {
  var loading = dojo.byId("loadingImg");
  esri.show(loading);
} //end showLoading
	
function hideLoading() {
  var loading = dojo.byId("loadingImg");
  esri.hide(loading);
} //end hideLoading



function getCookie(c_name) {
  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 "";
}

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());
}

function checkCookie(imagepath) {
  //alert('in checkCookie with: \n' + imagepath);
  ipath = imagepath;
  var gflusername=getCookie('gflusername');
  var gfluserintent=getCookie('gfluserintent');
  var gflaffil=getCookie('gflaffiliation');
  if (gflusername!=null && gflusername!="") {
    //alert('Welcome again '+gflusername+'!');
    var url = imagepath + '?gfluser=' + gflusername + '&gfluse=' + gfluserintent + '&gflaffil=' + gflaffil;
    //alert('registered already: \n' + url);
    //window.location.href = url;
	window.open(url);
  } else {
    //dijit.byId('userinfo').show();
	showDialog('userinfo');
	jQuery('#userinfo').dialog('open');
	//document.userinfo.imagepath.value = imagepath;	

  }
}

function createCookie() {
  //alert('in createCookie');
  var username = document.userinfo.usercountry.value;
  var userintent = document.userinfo.userintent.value;
  var affiliation = document.userinfo.affiliation.value;  
  //var ipath = document.forms.userinfo.imagepath.value;
  if (username == 0 || userintent == 0 || affiliation == 0) { 
    alert('Please complete Questionaire'); 
    //dijit.byId('userinfo').show();
	showDialog('userinfo');
	jQuery('#userinfo').dialog('open');
  } else {
    setCookie('gflusername', username, 30);
    setCookie('gfluserintent', userintent, 30);
	setCookie('gflaffiliation', affiliation, 30);
    //dijit.byId('userinfo').hide();
	jQuery('#userinfo').dialog('close');
    var url = ipath + '?gfluser=' + username + '&gfluse=' + userintent + '&gflaffil=' + affiliation;
    //alert('new registration: \n' + url);
    window.location.href = url;
  }    
}

function deleteCookie (cookiename) {
  alert(document.cookie);
  var delcookie = getCookie(cookiename);
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookiename += "=" + delcookie +"; expires=" + cookie_date.toGMTString();
}

  //create jQuery Dialog box
  function showDialog(boxID) {
  var openBox = "#" + boxID;
  //alert(openBox);
    if (boxID == "help") {
      jQuery(document).ready(function() {
	   jQuery(openBox).load('../downloadhelp.html');
       jQuery(openBox).dialog({
         bgiframe: true, autoOpen: false, height: 400, width: 500, modal: true
        });
      });
    } else if (boxID == "userinfo") {
      jQuery(document).ready(function() {
	   jQuery(openBox).load('../userinfo.html');
       jQuery(openBox).dialog({
         bgiframe: true, autoOpen: false, height: 300, width: 400, modal: true
        });
      });	
	  //alert('ipath: ' + ipath);
	  //document.userinfo.imagepath.value = ipath;
	}else {
      jQuery(document).ready(function() {
       jQuery(openBox).dialog({
         bgiframe: true, autoOpen: false, height: 100, modal: true
        });
      });
    }
  }

  
function activate(link){
  if(document.getElementById)
    document.getElementById(link).focus();
  else if (document.all)
  document.all(link).focus();
}

  
