// --------------------------------------------------------------------------------
// 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;

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);
  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;
  } else {
    document.userinfo.imagepath.value = imagepath;	
    dijit.byId('userinfo').show();   
  }
}

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) { 
    alert('Please complete Questionaire'); 
    dijit.byId('userinfo').show();
  } else {
    setCookie('gflusername', username, 30);
    setCookie('gfluserintent', userintent, 30);
	setCookie('gflaffiliation', affiliation, 30);
    dijit.byId('userinfo').hide();
    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();
}
