/*
***************************************************************************
*
* Copyright (C) Telegraph Media Group Ltd.
* All Rights Reserved. No use, copying or distribution of this work may be
* made. This notice must be included on all copies, modifications and
* derivatives of this work.
*
****************************************************************************
* $Id: common.js,v 1.15 2008/02/26 16:32:38 dysonl Exp $
*/

/* USED SO THIRD PARTIES CAN CONTROL OUR PAGE (FOR IFRAME CLICK THROUGHS) */
document.domain = 'longerwaydown.com';

/*********************************************************************/
/*****   The below is used to instantiate JavaScript functions   *****/
/*****  on the loading of the window. Functions can be added to  *****/
/*****       this listener by using the following example:       *****/
/*****                                                           *****/
/*****                 addDOMLoadEvent(function);                *****/
/*****                                                           *****/
/*****        Whereby 'function' is the function you want        *****/
/*****                        instantiated                       *****/
/*****                                                           *****/
/*****  This overcomes any issues with window.onload methods of  *****/
/*****                  instantiating functions                  *****/
/*****                                                           *****/
/*****           See this website for more information           *****/
/*****   http://www.thefutureoftheweb.com/blog/adddomloadevent   *****/
/*********************************************************************/

function addDOMLoadEvent(func) {
   if (!window.__load_events) {
      var init = function () {
          // quit if this function has already been called
          if (arguments.callee.done) return;

          // flag this function so we don't do the same thing twice
          arguments.callee.done = true;

          // kill the timer
          if (window.__load_timer) {
              clearInterval(window.__load_timer);
              window.__load_timer = null;
          }

          // execute each function in the stack in the order they were added
          for (var i=0;i < window.__load_events.length;i++) {
              window.__load_events[i]();
          }
          window.__load_events = null;
      };

      // for Mozilla/Opera9
      if (document.addEventListener) {
          document.addEventListener("DOMContentLoaded", init, false);
      }

      // for Internet Explorer
      /*@cc_on @*/
      /*@if (@_win32)
          // Only do this once (old code did this on every addDomLoadEvent)
          if (!document.getElementById("__ie_onload")) {
              document.write("<scr"+"ipt id=__ie_onload defer><\/scr"+"ipt>");
	      }
          var script = document.getElementById("__ie_onload");
          script.onreadystatechange = function() {
              if (this.readyState == "complete") {
                  init(); // call the onload handler
              }
          };
      /*@end @*/

      // for Safari
      if (/WebKit/i.test(navigator.userAgent)) { // sniff
          window.__load_timer = setInterval(function() {
              if (/loaded|complete/.test(document.readyState)) {
                  init(); // call the onload handler
              }
          }, 10);
      }

      // for other browsers
      window.onload = init;

      // create event function stack
      window.__load_events = [];
   }

   // add function to event stack
   window.__load_events.push(func);
}

/********************************************************************/
/*****  trimField function to validate and trim text fields     *****/
/********************************************************************/

function trimField(formField, fieldLimit){
  if(formField.value.length >= fieldLimit){
  	alert('You have exceeded the specified maximum length of ' + fieldLimit + ' characters for this field.\nYou will not be able to enter any more text!');
  	formField.value = formField.value.substring(0, fieldLimit);
  	return false;
  }
}

/********************************************************************/
/*****         Global code to handle popups on our site         *****/
/********************************************************************/

function newWindow(url, name, features) {
	var newWin = window.open(url,name,features);
}

/********************************************************************/
/*****                   Search Box layer code                  *****/
/********************************************************************/

var myBrow = ((navigator.appName) + parseInt(navigator.appVersion));
var slides = 2;
//IE and NETSCAPE 5+
function on(current) {
	var loop = "";
	document.getElementById(current).style.visibility='visible';
	for (count=1; count<slides+1; count++) {
		loop = "wsform"+count.toString();
		if (loop != current) {
			document.getElementById(loop).style.visibility='hidden';
		}
	}
}
//NETSCAPE 4
function onn4(current) {
	var loopn4 = "";
	document.layers[current].visibility='visible';
	for (count=1; count<slides+1; count++) {
		loopn4 = "wsform"+count.toString();
		if (loopn4 != current) {
			document.layers[loopn4].visibility='hidden';
		}
	}
}

function show (layer) {
	var thislayer = "wsform"+layer.toString();
	if (myBrow=="Netscape4") {
		onn4(thislayer);
	} else {
		on(thislayer);
	}
}

//IE FORM SUBMIT SUPPORT
function onEnter( evt, frm ) {
	var keyCode = null;
	if( evt.which ) {
		keyCode = evt.which;
	} else if( evt.keyCode ) {
		keyCode = evt.keyCode;
	}
	if(13 == keyCode) {
		document.getElementById("go").click();
		return false;
	}
	return true;
}

/********************************************************************/
/*****                   Article dropper code                   *****/
/********************************************************************/

function dropperInit() {
	var dropper = document.getElementById("dropper");
	var sharedropdown = document.getElementById("sharedropdown");
	var shareLink = document.getElementById("funcShareLink");

	// Dont break everything
	if (shareLink!=null && dropper!=null) {

		// Define all the styles for the share dropper
		shareLink.innerHTML = "<a href=\"#\" id=\"dropperLink\">Share this story</a>";
		dropper.className="share";
		sharedropdown.style.display="none";

		// this will only work after the shareLink has been written
		var dropperLink = document.getElementById("dropperLink");

		var articleClick=0;

		dropperLink.onclick = function() {
			if (articleClick < 1) {
				sharedropdown.style.display="block";
				dropper.className="sharedown";
				articleClick++;
				return false;
			} else {
				sharedropdown.style.display="none";
				dropper.className="share";
				articleClick = 0;
				return false;
			}
		};
	}
}

addDOMLoadEvent(dropperInit);

/********************************************************************/
/*****                   Dropdown Link Box code                 *****/
/*****  This will open a link from a dropdown into the window   *****/
/*****  when the user selects the appropriate link and clicks   *****/
/*****                            "GO"                          *****/
/********************************************************************/

function mainFormHandler_DROP_DOWN(){
	var URL = document.fmain_DROP_DOWN.ddlist.options[document.fmain_DROP_DOWN.ddlist.selectedIndex].value;
	if (URL != "empty") {
		window.location.href = URL;
	}
}

function ddLinkBoxInit() {
	var ddboxlinks = document.getElementsByName("ddboxgolink");

	if (dropper!=null) {
		for (i=1; i<ddboxlinks.length; i++) {
			ddboxlinks[i].onclick = function(){mainFormHandler_DROP_DOWN();};
		}
	}
}


