
//** These are my general-purpose functions.
//** All of them were created by myself. If you want to use any of them,
//** please give me credit: www.billbaldwin.co.uk 

function convertUKDate(value)
// Return 0 if param does not contain valid date
{	
	//  First convert the date - JScript Date object assumes US format
	var strUKDate = value.toString();
	var firstSlash = strUKDate.indexOf("/");
	var lastSlash = strUKDate.lastIndexOf("/");
	var strUSDate = strUKDate.substring(firstSlash + 1,lastSlash + 1);
	strUSDate = strUSDate + strUKDate.substring(0,firstSlash + 1);
	var yearBit = strUKDate.substring(lastSlash + 1);
	if (yearBit.length == 1)
		strUSDate = strUSDate + "200" + yearBit;
	else if (yearBit.length == 2)
					strUSDate = strUSDate + "20" + yearBit;
				else
					strUSDate = strUSDate + yearBit;
	// alert (strUSDate);
		
	if (isNaN(Date.parse(strUSDate))) // Date validation
		return 0;
		
	return strUSDate;
} 

function convertUSDate(strUSDate)
// Return 0 if param does not contain valid date
{
	if (isNaN(Date.parse(strUSDate))) // Date validation
		return 0;

	var dt = new Date(strUSDate); 

	var days = dt.getDate();
	var mths = dt.getMonth()+1;  // Bug in JScript (What about Netscape?)
	var year = dt.getFullYear().toString();
	//var year = dt.getYear();
	var formattedUKDate = days + '/' + mths + '/' + year.substring(2);
	
	return formattedUKDate
}









//** Load page in the specified frameset, unless it is already in a frameset.
//** This is to resolve the problem where users can select a page from their
//** History folders, causing JavaScript errors if it refers to another frame
//** which also needs to be loaded.
function loadInFrame(frameURL)
{
  // This page is designed to be loaded in a frameset, so make sure it is!
  // NB we allow the page to be loaded into a different 
  // frameset to that specified in the parameter.
  if (self.location.href == parent.location.href) // Only true if page not in a Frameset
    window.location.replace(frameURL);
}


//** Test whether variable is Empty (which includes containing nothing but blanks)
function isEmpty(variable)
{
  if (variable == null || variable == "" || variable == " ")
    return true;
  // Test for all characters being blanks
  for (i = 0; i < variable.length; i++)
  {
    if (variable.charAt(i) != " ")
      return false;
  }
  return true;  // No non-blank characters found
}


//** Test whether a colour code is dark, to know whether 
//** to use white or black as contrasting colour.
//** Colour code should be in format: '#' followed by 6 hex digits
//** First use isColour function to check for valid colour code.
function isDark(variable)
{
  
  var testChar
  // A colour is dark if none of the 3 colours is bright  
  // Test the first digit of each colour, ignoring the '#'
  for (i = 1; i < 7; i += 2)
  {
    testChar = variable.charAt(i);
    if ( testChar > "9")
      return false;
  }
  return true;  // No bright values found
}


//** Test whether variable contains a valid Colour code
//** Colour code should be in format: '#' followed by 6 hex digits
function isColour(variable)
{
  if (isEmpty(variable))
    return false;

  // Test length
  if (variable.length != 7)
    return false;
    
  var result;
  
  var lowerCase = variable.toLowerCase();  // Make testing easier
  var testChar = lowerCase.charAt(0);
  
  // Test first character
  if (testChar != "#")
    return false;

  // Test each digit of the colour    
  for (i = 1; i < 7; i ++)
  {
    var testChar = lowerCase.charAt(i);
    result = false; // Assume the digit not valid
    
    if (testChar >= "0" && testChar <= "9")  //Numeric
      result = true;
    else
      if (testChar >= "a" && testChar <= "f")  //Valid hex 
        result = true;

    if (result == false)
      return false;      
  }
  return true;  // No invalid characters were found
}

//** Given a url with full path included, remove the filename
//** portion and return only the path
  function extractPath(urlHref)
  {
    var currLocn = urlHref.toString();
    var lastSlash = currLocn.lastIndexOf("/");

    // We want path to contain: "http://fullpathname/"
    var path = currLocn.substring(0,lastSlash + 1);
    return path;
  }

//** Given a url with full path included, remove the path
//** portion and return only the filename
//** This version caters for existence of QueryString
function extractFilename(urlHref)
  {
    var currLocn = urlHref.toString();
    var lastSlash = currLocn.lastIndexOf("/");
    var queryChar = currLocn.lastIndexOf("?");
    if (queryChar == -1)
        queryChar = currLocn.length;


    // We want filename to contain: "filename.htm" or similar
    var filename = currLocn.substring(lastSlash + 1,queryChar);
    return filename;
  }

//** Show webpage in new (maximised) window without Toolbars
function newMaxWindow(webPage)
{    
  // Near-maximise window
  var screenHeight = screen.availHeight - 100;
  var screenWidth = screen.availWidth - 10;
      
  winNew=window.open(webPage,"WindowName", 
  "height=" + screenHeight + ",width=" + screenWidth + 
  ",left=0,top=0,status=yes,toolbar=no,menubar=yes,location=no");

}

//** Show webpage in new (maximised) window with Toolbars
function newMaxWinTools(webPage)
{    
/* Following code commented-out because it gave error 'Permission denied'
   if the new window was previously opened, but user then loaded a 
   different page into that window

  // If new window was previously opened, and contains the page, 
  // just set the focus
  if (typeof(winNew) != "undefined")
    if (extractFilename(winNew.location.href)== webPage)  
    {
      winNew.focus();
      return;
    }
*/  

  // Near-maximise window
  var screenHeight = screen.availHeight - 100;
  var screenWidth = screen.availWidth - 10;
      
  winNew=window.open(webPage,"WindowName", 
  "height=" + screenHeight + ",width=" + screenWidth + 
  ",left=0,top=0,status=yes,toolbar=yes,menubar=yes,location=yes");

}



//** Show the Web Colour Wizard in its own window
//**
function showWizard()
{    

  // Near-maximise window for most effective use of Colour Wizard
  var screenHeight = screen.availHeight - 100;
  var screenWidth = screen.availWidth - 10;
    
  winWizard=window.open("WizardFrames.htm","Wizard",
  "height=" + screenHeight + ",width=" + screenWidth + 
  ",left=0,top=0,status=yes,toolbar=no,menubar=yes,location=no");

}