// Javascript utility functions for Wizards of the Coast Exam Center
// Copyright(c) 2005-2006 Wizards of the Coast
// Author: Collin Jackson

function iePNG(id) {
  if(window.clipboardData) {
    var elt = document.getElementById(id);
    elt.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" 
      + elt.src + "',sizingMethod='image');";
    elt.src = "images/spacer.gif";
  }
}

// For standard Gatherer popup windows
function WizardsAutoCard(cardname) {
  windowName = "WotCWindow";
  params = "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, width=800, height=500";
  win = window.open("http://gatherer.wizards.com/Pages/Card/Details.aspx?name="+cardname, windowName , params);
}

function DreamcatcherWindow(mininame) {
  windowName = "WotCWindow";
  params = "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=1, width=700, height=625";
  win = window.open("http://dreamcatcher.wizards.com/mini.aspx?name="+mininame, windowName , params);
}

function GetStatCardForLink(link) {
  var parent = link;
  for (var i = 0; i < 10; i++) {
    if (parent.className == 'examgriditem') break;
    else parent = parent.parentNode;
  }
  var divs = parent.getElementsByTagName('div');
  for (var i = 0; i < divs.length; i++) {
    if (divs[i].className == 'statcard') {
      return divs[i];
    }
  }
}

// Called whenever a stat card link is clicked
function ShowStatCard(link) {
  var div = GetStatCardForLink(link);
  var imgs = div.getElementsByTagName("img");
  var isVisible = false;
  for (var j = 0; j < imgs.length; j++) {
    if (imgs[j].alt == link.title && !isVisible) {
      imgs[j].style.position = 'static';
      imgs[j].style.visibility = 'visible';
      isVisible = true;
    } else {
      imgs[j].style.position = 'absolute';
      imgs[j].style.visibility = 'hidden';
    }
  }
  var parent = link;
  for (var i = 0; i < 10; i++) {
    if (parent.className == 'examgriditem') break;
    else parent = parent.parentNode;
  }
  var as = parent.getElementsByTagName("a");
  for (var i = 0; i < as.length; i++) {
    if (as[i].className.indexOf("statcardlink") >= 0) {
      as[i].className = as[i] == link ? "selectedstatcardlink" : "statcardlink";
    }
  }
}
 
// Called to set the stat card link to something
function InitStatCard(id) {
  var link = document.getElementById(id);
  var div = GetStatCardForLink(link);
  var img = document.createElement("img");
  img.alt = link.title;
  img.src = link.href;
  img.style.top = '0px';
  img.style.left = '0px';
  if (div.innerHTML != '') {
    img.style.position = 'absolute';
    img.style.visibility = 'hidden';
    link.className = 'statcardlink';
  } else {
    link.className = 'selectedstatcardlink';
  }
  div.appendChild(img);
}

// Determine local time zone offset
function localDate(d) {
  return new Date((new Date(d)).getTime() - (new Date()).getTimezoneOffset() * 60 * 1000);
}

// Output a long date (for the view tab)
function longDate(d) {
  document.write(localDate(d).toLocaleString());
}

// Output a locale date with no time zone offset (generally for static dates)
function localeDate(d) {
  document.write((new Date(d)).toLocaleString());
}

// Output a short date (for the select tab)
function shortDate(d) {
  d = localDate(d);
  //  Here's another way to do it, but the dates are too long to display in a table
  // document.write(d.toLocaleDateString());
  document.write(d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate());
}

// Ensure that all radio buttons are used, and save their values
// into the hidden form variable
function validateExam(askForConfirmation)
{
	var complete = true;
	var radiobuttons = new Object();
	var controls;
	try {
	  controls = document.Form1.getElementsByTagName("INPUT");
	} catch(ex) { 
	  alert("Your browser is not supported. You cannot submit this exam."); 
	}
	var selection = new Array();
	for(var i = 0; i < controls.length; i++) {
		if(controls[i].type == "radio") {
			var name = controls[i].name;
			if(typeof(radiobuttons[name]) == "undefined")
				radiobuttons[name] = 0;
			if(controls[i].checked) {
				radiobuttons[name] = controls[i].value;
				selection.push(controls[i].value);
			}
		} else if(controls[i].type == "checkbox") {
			if(controls[i].checked) {
				selection.push(controls[i].value);
			}
		}
	}
	for(var key in radiobuttons) {
		if(radiobuttons[key] == 0) complete = false;
	}
	if(selection.length > 0) 
		document.Form1.ExamAnswers.value = selection.join(",");
	if(askForConfirmation && !complete) {
		return confirm("You have not answered some of the single-answer questions. \n" + 
			"Are you sure you want to submit the exam?"
			 // + "\nSelections: " + selection.join(",")  // Optional for debugging
			);
	} else return complete;
}

// Creates a popupwindow of the summary
function printpopup(title) {
	try {
		var win = window.open("javascript:window.opener.document.getElementById('summary').innerHTML", title, 'width=640,height=300,status=no,scrollbars=yes');
		setTimeout(function() { win.print() }, 500);
	} catch(e) { alert(e); }
}

function expanderclick(expander, id, hidden) {
  var node = document.getElementById(id);
  switch(expander.className) {
    case 'collapser':
      node.style.display = 'none';
      expander.className = 'expander';
      document.Form1[hidden].value = "True";
      break;
    case 'expander':
      node.style.display = '';
      expander.className = 'collapser';
      document.Form1[hidden].value = "False";
      break;
  }
}

function scroll(amount)
{
    return;  // auto-scrolling rulebook is no longer used
    sstchur_SmartScroller_GetCoords();
    document.forms["Form1"].scrollTop.value = 
        parseInt(document.forms["Form1"].scrollTop.value) + amount;
    sstchur_SmartScroller_Scroll();
}

function AddDreamcatcherSearchProvider()
{
  try {
    window.external.AddSearchProvider('http://dreamcatcher.wizards.com/webservice/dreamcatcher-search-description.xml');
  } catch(e) {
    alert("Sorry, your browser does not support this function.");
  }
}

function getQueryStringArray() {
  var qsParm = {};
  var query = window.location.search.substring(1);
  var parms = query.split('&');
  for (var i=0; i<parms.length; i++) {
    var pos = parms[i].indexOf('=');
    if (pos > 0) {
      var key = parms[i].substring(0,pos);
      var val = parms[i].substring(pos+1);
      qsParm[key] = val;
    }
  }
  return qsParm;
}