//Set the avaliable screen width to an easy to remember variable.
var aw = screen.availWidth;
//Set the avaliable screen height to an easy to remember variable.
var ah = screen.availHeight;


addary=new Array(); //red
addary[0]=new Array(0,1,0);  //red green
addary[1]=new Array(-1,0,0); //green
addary[2]=new Array(0,0,1);  //green blue
addary[3]=new Array(0,-1,0); //blue
addary[4]=new Array(1,0,0);  //red blue
addary[5]=new Array(0,0,-1); //red
addary[6]=new Array(255,1,1);
clrary=new Array(360);
for(i=0;i<6;i++)
 for(j=0;j<60;j++)
  { clrary[60*i+j]=new Array(3);
    for(k=0;k<3;k++)
     { clrary[60*i+j][k]=addary[6][k];
       addary[6][k]+=(addary[i][k]*4);
     };
  };

function capture()
 { if(document.layers)
    { layobj=document.layers['wheel'];
      layobj.document.captureEvents(Event.MOUSEMOVE);
      layobj.document.onmousemove=moved;
    }
   else
    { layobj=document.all["wheel"];
      layobj.onmousemove=moved;
    };
 };

function moved(e)
 { y=(document.layers)?e.layerX:event.offsetX;
   x=(document.layers)?e.layerY:event.offsetY;
   sx=x-128; sy=y-128;
   qx=(sx<0)?0:1; qy=(sy<0)?0:1;
   q=2*qy+qx; quad=new Array(-180,360,180,0);
   xa=Math.abs(sx); ya=Math.abs(sy);
   d=ya*45/xa;
   if(ya>xa) d=90-(xa*45/ya);
   deg=Math.floor(Math.abs(quad[q]-d));

   n=0;
   sx=Math.abs(x-128); sy=Math.abs(y-128);
   r=Math.sqrt((sx*sx)+(sy*sy));
   if(x==128 & y==128)
    { c="000000"; }
   else
    { for(i=0;i<3;i++)
       { r2=clrary[deg][i]*r/64;
         if(r>64) r2+=Math.floor(r-64)*4;
          if(r2>255) r2=255;
         n=256*n+Math.floor(r2);
       };
      c=n.toString(16);
      while(c.length<6) c="0"+c;
    };
   if(document.layers)
    { document.layers["wheel"].document.f.t.value="#"+c;
      //document.layers["wheel"].bgColor="#"+c;
     
    }
   else
    { document.all["wheel"].document.f.t.value="#"+c;
      //document.all["wheel"].style.backgroundColor="#"+c;
     
    };
   return false;
 };

 
 function store()
 {
 document.all.colorcode.value = document.all.t.value
document.all.showrow.bgColor=document.all.t.value
 }
 
function showBranding(matrix) {
  B10_spawnWindow('Toolbar','branding_toolbar.cfm?matrix=' + matrix,350,410,false);
}

function hideBranding(matrix) {
  window.opener.moveTo(0, 0);
  window.opener.resizeTo(aw, ah);
}

/* Resize the parent window to the available width of the 
screen minus the width of the help window. */
function intialiseBranding(toolbarwidth) {
  window.opener.moveTo(0, 0);
  window.opener.resizeTo(aw - toolbarwidth, ah);
  window.moveTo(aw - toolbarwidth, 0);
}

function initialiseColour(toolbarwidth,toolbarheight) {
  window.moveTo(aw - toolbarwidth, ah - toolbarheight);
}

function GH_priorityListCommit() {
  with (document.eventform.list2) {
    for(var i=0; i<options.length; i++) { options[i].selected = true; }
  }
}

//  Create an array to hold the folder info to change sequence
//  This type of function is called a Constructor Function
function GH_prioritysequencehold(Value, Text) { this.Value = Value; this.Text = Text; }

//  Move the selected category higher in sequence
//  This function will take the selected item and move it one record higher.
function GH_priorityMoveUp(Position) {
  var Position
  Position = document.eventform.list2.selectedIndex;
  if (Position != 0) {
    HoldingPlace = new Array(2);
    var SequenceNumber, PreviousFolder;
    PreviousFolder = Position - 1;
    HoldingPlace[0] = new GH_prioritysequencehold(document.eventform.list2[PreviousFolder].value, document.eventform.list2[PreviousFolder].text);
    HoldingPlace[1] = new GH_prioritysequencehold(document.eventform.list2[Position].value, document.eventform.list2[Position].text);
    document.eventform.list2[PreviousFolder].value = HoldingPlace[1].Value;
    document.eventform.list2[PreviousFolder].text = HoldingPlace[1].Text;
    document.eventform.list2[Position].value = HoldingPlace[0].Value;
    document.eventform.list2[Position].text = HoldingPlace[0].Text;
    document.eventform.list2.selectedIndex = PreviousFolder;
  }
}

//  Move the selected category lower in sequence
function GH_priorityMoveDown(Position) {
  var Position; var SelectLength; var LastPosition;
  SelectLength = document.eventform.list2.length;
  LastPosition = SelectLength - 1;
  Position = document.eventform.list2.selectedIndex;
  if (Position != LastPosition) {
    HoldingPlace = new Array(2);
    var SequenceNumber, NextFolder;
    NextFolder = Position + 1;
    HoldingPlace[0] = new GH_prioritysequencehold(document.eventform.list2[NextFolder].value, document.eventform.list2[NextFolder].text);
    HoldingPlace[1] = new GH_prioritysequencehold(document.eventform.list2[Position].value, document.eventform.list2[Position].text);
    document.eventform.list2[NextFolder].value = HoldingPlace[1].Value;
    document.eventform.list2[NextFolder].text = HoldingPlace[1].Text;
    document.eventform.list2[Position].value = HoldingPlace[0].Value;
    document.eventform.list2[Position].text = HoldingPlace[0].Text;
    document.eventform.list2.selectedIndex = NextFolder;
  }
}

//Check that the specified form element contains a number 
//- attempt to interpret content as a number - but if not - inform user of problem.
function ValidateNumber(element)
{
  number = parseInt(element.value);
  if (isNaN(number)){
   element.value = 0;
   alert("You must enter a number.");
  }
  else {
    element.value = number;
  }
  
}




function pageSaved()
{
  document.all.cautionimg.src = '/img/admin/boxes/box_caut_datsaved.gif'
  document.all.cautionimg.alt = '';
}

//Function to display the unsaved data image when the page is changed.
function pageChanged() 
{
  //alert(window.event.keyCode);
  if (window.event.keyCode != 27)
    document.all.cautionimg.src = '/img/admin/boxes/box_caut_nosave.gif';
    document.all.cautionimg.alt = 'caution you have not saved your most recent changes';
 }


function validateUser()
{

 if (document.frmUserEdit.txtusername.value == "") {
				alert("You must enter a name.");
 }
 /* else	if (document.frmUserEdit.txtusermail.value.search("@") == "-1") {
				alert("You must enter a valid email address.");
 }*/
  else	if (!document.frmUserEdit.txtusermail.value.match(/^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.(([A-Za-z]{2,3})|(aero|coop|info|museum|name))$/)) {
				alert("You must enter a valid email address.");
 }
 /*else	if (document.frmUserEdit.txtuserUsername.value  == "") {
				alert("You must enter a username.");
 }*/
 // GC 23/11/07: no longer validate usernames for special characters in admin tool. Usernames are now only validated at registration
 /*else	if (!document.frmUserEdit.txtuserUsername.value.match(/^[A-Za-z0-9]+$/) && !document.frmUserEdit.txtuserUsername.value.match(/^[A-Za-z0-9\-+_.]+@[A-Za-z0-9\-+_.]+\.[A-Za-z.]{2,}$/)) {
				alert("You must enter a valid username.");
 }*/
 else
 {	 
       document.frmUserEdit.submit();
 }
}        

function setHeadingFont(intFont) {
  eval("window.opener.frmStyle.hdnHeadingFont" + intFont + ".value = document.frmStyle.selHeadingFont" + intFont + ".value");
  eval("window.opener.heading" + intFont + ".style.fontFamily = document.frmStyle.selHeadingFont" + intFont + ".value");
}
function setHeadingSize(intFont) {
  eval("window.opener.frmStyle.hdnHeadingSize" + intFont + ".value=document.frmStyle.selHeadingSize" + intFont + ".value + 'pt'");
  eval("window.opener.heading" + intFont + ".style.fontSize = document.frmStyle.selHeadingSize" + intFont + ".value  + 'pt'");
}

function setBodyFont(intFont) {
  eval("window.opener.frmStyle.hdnBodyFont.value=document.frmStyle.selHeadingFont" + intFont + ".value");
  for (i = 1; i <= 5; i++) {
    eval("window.opener.body" + i + ".style.fontFamily = document.frmStyle.selHeadingFont" + intFont + ".value");
	}
}

function setBodySize(intFont) {
  eval("window.opener.frmStyle.hdnBodySize.value=document.frmStyle.selHeadingSize" + intFont + ".value + 'pt'");
  for (i = 1; i <= 5; i++) {
    eval("window.opener.body" + i + ".style.fontSize = document.frmStyle.selHeadingSize" + intFont + ".value + 'pt'");
	}
}

function assign(matrixID) {
  var location =   'grouppicker_action.cfm?action=assign&matrix=' + matrixID + '&item=' + selUnassignedGroups.value;
  document.location = location;
}
function unAssign(matrixID) {
  var location =   'grouppicker_action.cfm?action=unassign&matrix=' + matrixID + '&item=' + selAssignedGroups.value;
  document.location = location;
}






// Date Functions used in matrix pre-publication tools 



//checks for a valid date

function isValidDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
return false;
}

day = matchArray[1];    // p@rse date into variables
month = matchArray[3]; 
year = matchArray[5];

if (day < 1 || day > 31) {
return false;
}

if (month < 1 || month > 12) { // check month range
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
return false;
}
}
return true; // date is valid
}


