/***************************************************************************
KMcG 17/05/2006: Login to manage cookie and display edit bar functions
****************************************************************************/

var cpmenu = "<div style='position: absolute; right: 0; background: white; text-align: left; border: 1 1 1 1;'><ul><li><a href='http://www.yawconline.com/html2word_ref/ye_doc.php?mode=referer&version=" + GetCookie('version') + "&name=" + GetCookie('username') + "&email=" + GetCookie('email') + "' class='hlink'>Edit this page</a></li><li><a href='http://www.yawconline.com/html2word_ref/ye_doc.php?mode=referer&amp;newFile=ok&version=" + GetCookie('version') + "&name=" + GetCookie('username') + "&email=" + GetCookie('email') + "' class='hlink'>New page in this folder</a></li><li><span class='showuser'><b>" + GetCookie('username') + "</b> logged in</span></li></ul></div>";
var notloggedin = "";
//var path = "/advisory/";
//var allowedPath = new Array();
//allowedPath[] = "/advisory/";
//allowedPath[] = "/";

//function ComparePath () {
//  var path = GetPath();
//  for (i=1; i<allowedPath.length; i++) 
//  if (allowedPath[i] == GetPath()) return true;
//  return false;
//}
//function GetPath () {
//  var path = "/";
//  return path;
//}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

var usr;
var pw;
var wv;
//var sv;
function getme()
{
usr = document.loginform.username;
pw = document.loginform.email;
wv = document.loginform.version;
//sv = document.loginform.save;
	//if (GetCookie('username') != null && ComparePath()) 
	if (GetCookie('username') != null)
	{
		usr.value = GetCookie('username')
		pw.value = GetCookie('email')
		wv.value = GetCookie('version')
//		if (GetCookie('save') == 'true')
//		{
//			sv[0].checked = true;
//		}
	}
}

function saveCookie (expdate, path) {
	if (wv[0].checked) SetCookie('version', 'w2000', expdate, path);
	if (wv[1].checked) SetCookie('version', 'w2002', expdate, path);
	SetCookie('username', usr.value, expdate, path);
	SetCookie('email', pw.value, expdate, path);
}

function saveme()
{
	if (usr.value.length != 0 && pw.value.length)
	{
//		if (sv[0].checked)
//		{

			expdate = new Date();
			// Expire cookie in 1 month
			expdate.setTime(expdate.getTime()+(30 * 24 * 60 * 60 ));
			
			saveCookie (expdate, '/research/');
			saveCookie (expdate, '/advisory/');
			saveCookie (expdate, '/login/');
			saveCookie (expdate, '/nfc/');
			saveCookie (expdate, '/ashtown/');
			

//			SetCookie('save', 'true', expdate);
//		}
//		*maybe required for logout functionallity*
//		if (sv[1].checked)  
//		{
//			DeleteCookie('username');
//			DeleteCookie('password');
//			DeleteCookie('save');
//		}
		alert('You are now logged in.');
	}
		else
	{
		alert('You must enter your name and email address.');
		return false;
	}
}

if (GetCookie('username') != null) document.write(cpmenu);
else document.write(notloggedin);



/***************************************************************************
Existing functions
****************************************************************************/
function currentElement(name,type){
    current_element_name=name
    current_element_type=type
//  alert('Current element name is: ' + name + ' and, would you believe it; the Current element type is: ' + type)
    }

function resizeFormElements() {
	if (navigator.appName.indexOf('Microsoft') != -1) { 
		for (var i=0;i < document.forms[0].length;i++) {
			document.forms[0].elements[i].setAttribute('size',document.forms[0].elements[i].getAttribute('size')*2); document.forms[0].elements[i].setAttribute('cols',document.forms[0].elements[i].getAttribute('cols')*2);
			} 
	}

}

function highlight(){
	return;
}

// countdown functions

var timerID = null;
var timerRunning = false;

function stopclock (){
        if(timerRunning)
                clearTimeout(timerID);
        timerRunning = false;
}

function startclock () {
        // Make sure the clock is stopped
        stopclock();
        showtime();
}

function showtime () {
        var now = new Date();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();

        var date = now.getDate();
        var month = now.getMonth() + 1;
        var year = now.getYear();
		//	alert("This:" +year);
        if (year < 1000)
         year+=1900
        
        var timeValue = "" + ((hours < 10 ) ? "0" : "") + hours
        // var timeValue = "" + ((hours >12) ? hours -12 :hours)
        timeValue += ((minutes < 10) ? ":0" : ":") + minutes
        timeValue += ((seconds < 10) ? ":0" : ":") + seconds
        // timeValue += (hours >= 12) ? " P.M." : " A.M."
        
        tminus0 = new Date("January 1, 2002")
   		//alert("Set:" +tminus0);
		//tminus0.setUTCFullYear(year)
		//	alert("Set:" +tminus0);
        msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day
        daysLeft = (tminus0.getTime() - now.getTime()) / msPerDay;
        daysLeft = Math.round(daysLeft);

                    
        timerID = setTimeout("showtime()",1000);

        timerRunning = true;

		var verb = "" + ((year == 2002 ) ? ":since " :  "to go ");

		return daysLeft;
}

function verb () {
        var now = new Date();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();

        var date = now.getDate();
        var month = now.getMonth() + 1;
        var year = now.getYear();
        if (year < 1000)
         year+=1900
        
		var verb = "" + ((year == 2002 ) ? "since " :  "to go ");

		return verb;
}

function displayEuro() {
if (navigator.appName.indexOf('Microsoft') != -1)
{
document.write("&euro;")
}
else
{document.write("Euro")}
}

function getAction(){
var unsubsub="http://www.teagasc.ie/mailman/handle_opts/jobinfo/" + document.un_sub.email.value;
document.un_sub.action=unsubsub;
return true;
}


// validate  FORM 
function isBlank(s) {
 var len=s.length
 var i
 for(i=0;i<len;++i) {
  if(s.charAt(i)!=" ") return false
 }
 return true
}


function validate(fieldName,fieldValue) {
 if(isBlank(fieldValue)) {
  alert(fieldName+" cannot be left blank.")
  return false
 }
 return true
}


function validateForm() {
 if(!validate("The 'Name' field",document.order.cname.value))
  return false
 if(!validate("The 'Number of Copies' field",document.order.quantity.value))
  return false
 if(!validate("The 'Cost' field",document.order.cost.value))
  return false
 if(!validate("The 'Post and Packaging' field",document.order.pandp.value))
  return false
 if(!validate("The 'Total' field",document.order.total.value))
  return false
 if(!validate("The 'Address' field",document.order.address.value))
  return false


 if(!validate("The E-mail field",document.order.email.value))
  return false
 if(document.order.email.value.indexOf('@')==-1) {
  alert("The email address you have entered is not valid.\n\nPlease re-enter your correct email address!")
  return false
}
}

function clearSearch()
	{
		//if (document.quicksearch.words.value=='Search')
		// document.quicksearch.words.value='';
		if (document.quicksearch.qsearch.value=='Search')
		 document.quicksearch.qsearch.value='';
	}


function showTable(filename,w,h,s) {
 window.open(filename, "floater", "toolbar=no,width="+ w +",height=" + h + ",directories=no,status=no,scrollbars="+ s +",resize=no,menubar=no");
 } 

 function popup(filename,w,h,s) {
 window.open(filename, "floater", "toolbar=no,width="+ w +",height=" + h + ",directories=no,status=no,scrollbars="+ s +",resize=no,menubar=yes,left=10,top=10,alwaysRaised=1");
 } 

