/* 
	Method for mini login box to switch to * password field
*/
function showPwd(theField){
  theField.style.display='none';
  pwdField = theField.form.password;
  pwdField.style.display='block';
  pwdField.focus();
}
/*
	Method for mini login box to switch to regular plain text characters
*/
function showNote(theField){
  if(theField.value==""){
    theField.style.display='none';
    noteField = theField.form.pwdNote;
    noteField.style.display='';
  }
}

function newMimeType(fileName) {
	msgWindow=window.open('','altwindow','menubar=yes,toolbar=yes,location=no,resizable=yes,scrollbars=yes,status=yes,width=600,height=400,screenX=130,screenY=30,top=18,left=130');
	msgWindow.close();
	
	msgWindow=window.open(fileName,'altwindow','menubar=yes,toolbar=yes,location=no,resizable=yes,scrollbars=yes,status=yes,width=600,height=400,screenX=130,screenY=30,top=18,left=130');
	msgWindow.focus();
}
function newSite(fileName) {
	msgWindow=window.open('','altwindow','menubar=yes,toolbar=yes,location=no,resizable=yes,scrollbars=yes,status=yes,width=600,height=400,screenX=130,screenY=30,top=18,left=130');
	msgWindow.close();

	msgWindow=window.open(fileName,'altwindow','menubar=yes,toolbar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=600,height=400,screenX=130,screenY=30,top=18,left=130');
	msgWindow.focus();
}

/*
	Get the cookie.
	@param name The name of the cookie to get.
*/
function getCookie(name)
{ var pos
  var token = name + "=";
  var tnlen = token.length;
  var cklen = document.cookie.length;
  var i = 0;
  var j;
  while (i < cklen)
  { j = i + tnlen;
    if (document.cookie.substring(i, j) == token)
    { pos = document.cookie.indexOf (";", j);
      if (pos == -1)
        pos = document.cookie.length;
      return unescape(document.cookie.substring(j, pos));
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

/*
	Set cookie.
	@param name The name of the cookie to set.
	@param value The value of the cookie to set.
*/
function setCookie(name, value)
{ document.cookie = name + "=" + escape(value)
}

/*
	Delete cookie.
	@param name The name of the cookie to delete.
*/
function deleteCookie(name)
{ var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = getCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// Cut long strings. 
function cutLength(_src, _maxLength){
	if ((_src != null) && (_src.length >  _maxLength)){
		_src = _src.substring(0, _maxLength - 3) + "...";
	}
	document.write(_src);
}

function setDispatch(dispatchInput, dispatchAction){
	document.getElementById(dispatchInput).value=dispatchAction;
}

/*
 * Switch stylesheet to Wysiwig printing
 * and print window. This is used to implement
 * compliance print style requirement.
 */
function changeStyleAndPrint() {
	
	for(var i=0; i < document.styleSheets.length; i++) {
		var sheet = document.styleSheets[i]
		
		var medium = sheet.media.mediaText ? sheet.media.mediaText : sheet.media;

		if (medium == 'screen') {
			// replace media 'screen' to 'all
			if (sheet.media.mediaText) {
				sheet.media.deleteMedium('screen');
				sheet.media.appendMedium('all');
			} else {				
				sheet.media = 'all';
			}
		} else if (medium == 'all') {
			// leave media 'all' unchanged
		} else{
			// disable remaining stylesheets
			sheet.disabled = true
		}
	}
			
	// print window
	window.print();
	setTimeout('restoreStyleSheet()', 4000);
}

function restoreStyleSheet() {	
	// restore to printer-friendly stylesheet
	for(var i=0; i < document.styleSheets.length; i++) {
		var sheet = document.styleSheets[i]
		
		var medium = sheet.media.mediaText ? sheet.media.mediaText : sheet.media;

		if (medium == 'all') {
			// replace media 'all' to 'screen'
			if (sheet.media.mediaText) {
				sheet.media.deleteMedium('all');
				sheet.media.appendMedium('screen');
			} else {				
				sheet.media = 'screen';
			}
		} else if (medium == 'screen') {
			// leave media 'screen' unchanged
		} else{
			// enable remaining stylesheets
			sheet.disabled = false
		}
	}
}
		
/*
 * Add 'onclick' attribute to div tags with class 'mars'
 * This is used to implement compliance print style requirement.
 */
function addCompliancePrintAction() {
	// get all div tags
	 var allPageTags=document.getElementsByTagName("div"); 
	 
	 //Cycle through the tags looking for 'mars' class 
	 for (i=0; i<allPageTags.length; i++) {
	 	var tag = allPageTags[i];
	 	// Pick out the tags with 'complianceCode' class
	 	// Note : in usmf/adminWeb this was called "mars"
	 	if (tag.className == 'complianceCode') { 
	 		// add onclick event to div tag
	 		if( tag.attachEvent ){
	 			// IE
   				tag.attachEvent('onclick', changeStyleAndPrint);
			} else {
				// Firefox
   				tag.setAttribute('onclick', 'changeStyleAndPrint()'); 
			}
	 	} 
	}
}


/**
 * This script and many more are available free online at
 * The JavaScript Source!! http://javascript.internet.com
 * Original:  KK Chan
 */

function tzone(code, city) {

	this.time = new Date(0) ;		// datetime
	this.code = code ;
	this.city = city ;
}


function UpdateClocks() {
	// www.timeanddate.com/worldclock

	var ct = new Array(
		new tzone('AUS', 'Sydney'),
		new tzone('JPN', 'Tokyo'),
		new tzone('SIN', 'Singapore'),
		new tzone('GER', 'Frankfurt'),
		new tzone('LON', 'London'),
		new tzone('NYC', 'New York'),
		new tzone('DUB', 'Dubai'),
		new tzone('BSL', 'Brasilia')
	);

	// user server time to compute client time
	var currentDate = new Date();
	var currentTime = currentDate.getTime();
	
	var gmtm = getServerTime() + currentTime - getClientBaseTime();

	// compensate time zones	    
	for (n=0 ; n<ct.length ; n++) {
		var offset = getOffset(ct[n].code);
		// alert(ct[n].code + ' offset ' + offset);
		ct[n].time = new Date(gmtm + offset) ;
	}
	
	document.getElementById('Clock0').innerHTML = ClockString(ct[0].city, ct[0].time);

	document.getElementById('Clock1').innerHTML = ClockString(ct[1].city, ct[1].time);

	document.getElementById('Clock2').innerHTML = ClockString(ct[2].city, ct[2].time);

	document.getElementById('Clock3').innerHTML = ClockString(ct[3].city, ct[3].time);

	document.getElementById('Clock4').innerHTML = ClockString(ct[4].city, ct[4].time);

	document.getElementById('Clock5').innerHTML = ClockString(ct[5].city, ct[5].time);

	document.getElementById('Clock6').innerHTML = ClockString(ct[6].city, ct[6].time);

	document.getElementById('Clock7').innerHTML = ClockString(ct[7].city, ct[7].time);

	timerID = window.setTimeout("UpdateClocks()", 1001) ;
}

/**
 * Print the time (hrs:mins:seconds) with AM/PM indicator
 */
function ClockString(city, dt) {
	var stemp, ampm ;
	var dt_year = dt.getUTCFullYear() ;
	var dt_month = dt.getUTCMonth() + 1 ;
	var dt_day = dt.getUTCDate() ;
	var dt_hour = dt.getUTCHours() ;
	var dt_minute = dt.getUTCMinutes() ;
	//var dt_second = dt.getUTCSeconds() ;

	dt_year = dt_year.toString() ;

	if (0 <= dt_hour && dt_hour < 12) {
		ampm = 'AM' ;
		if (dt_hour == 0) dt_hour = 12 ;
	} else {
		ampm = 'PM' ;
		dt_hour = dt_hour - 12 ;
		if (dt_hour == 0) dt_hour = 12 ;
	}

	if (dt_minute < 10)
		dt_minute = '0' + dt_minute ;

	//if (dt_second < 10)
	//	dt_second = '0' + dt_second ;

	// stemp = dt_month + '/' + dt_day + '/' + dt_year.substr(2,2) ;
	// stemp = stemp + ' ' + dt_hour + ":" + dt_minute + ":" + dt_second + ' ' + ampm ;

	//time = dt_hour + ":" + dt_minute + ":" + dt_second + ' ' + ampm ;
	time = dt_hour + ":" + dt_minute +  ' ' + ampm ;
	
	var result = "<span class='clockCity'>" +  city + ": </span>";
	result += "<span class='clockTime'>" + time + "</span>";
	
	return result ;
}

function showId(_id){
	if (document.getElementById(_id)){
		document.getElementById(_id).style.display = 'block';
		document.getElementById(_id).className = 'over';
	}
}

function hideId(_id){
	if (document.getElementById(_id)){
		document.getElementById(_id).style.display = 'none';
	}
}
