// John Hayes
// 11/11/2005
// Style switching adapted from styleswitcher.js by Paul Sowden, which uses cookie functions by Peter-Paul Koch

if(window != window.top){
//top.location.replace(location.href);
}

window.onload = function() {onLoadFunction();}

function onLoadFunction() {
    preparePage("c4:c528:c590:c559:c714:c745:c530");
    getURL();
  }

Styles = new Array();

var basicLinksStringold = '<li><a href="/contact" title="Contact">Contact us</a></li><li><a title="Accessibility" href="/accessibility">Accessibility</a></li><li><a href="/sitemap" title="Sitemap">Sitemap</a></li><li><a title="Press and policy" href="/pressandpolicy">Press and policy</a></li><li><a title="Terms and Conditions" href="/content/item2388/">Terms and Conditions</a></li>';

var basicLinksString = '<li><a href="/contact" title="Contact us">Contact us</a></li><li><a href="/pressandpolicy" title="Press and Policy">Press and Policy</a></li><li><a href="/sitemap" title="Sitemap">Sitemap</a></li><li><a href="/accessibility" title="Accessibility">Accessibility</a></li>'; 

function preparePage(styleSheetString) // first style is always text style, second common style, third colours style, fourth background image style
{
 Styles = styleSheetString.split(':');
 var isText = readCookie("style");
 if (isText == "text") setStyleText();
 else setStyleFull();
}

// **************************************************************************************************************************

var textLinkString = '<li class="last"><a href="JavaScript:setStyleText()" title="Text Only">Text only version</a></li>';
var graphicalLinkString = '<li class="last"><a href="JavaScript:setStyleFull()" title="Graphical Version">Graphical Version</a></li>';

function setActiveStyleSheet(whichStyle) 
{
if (document.getElementById('switchlinks')!=undefined) {

  var i, j, a;

/*
this was probably here for debugging - jg
  if (document.all)
  {
    location.reload();
  }
*/

  for(j=0; j < Styles.length; j++) // for every custom css file
  {
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) //for each possible file
    {
      if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("href")) //if of correct type
      {
        if(a.getAttribute("href") == ("/vyre4/generated_files/pub_module/css/" + Styles[j] + ".css") || a.getAttribute("href") == ("/generated_files/pub_module/css/" + Styles[j] + ".css")) // if matches
        {
          a.disabled = true; // disabled
        }
      }
    }
  } // all disabled at this point
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) //for each possible file
  {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("href")) //if of correct type
    {
      if(whichStyle == 'text') // text version)
      {
        if(a.getAttribute("href") == ("/vyre4/generated_files/pub_module/css/" + Styles[0] + ".css") || a.getAttribute("href") == ("/generated_files/pub_module/css/" + Styles[0] + ".css")) //matches correct one
        {
          a.disabled = false; // correct one enabled
          document.getElementById('switchlinks').innerHTML = basicLinksString + graphicalLinkString;
        }
      }
      else // graphical
      {
        if(a.getAttribute("href") == ("/vyre4/generated_files/pub_module/css/" + Styles[3] + ".css") || a.getAttribute("href") == ("/generated_files/pub_module/css/" + Styles[3] + ".css") || a.getAttribute("href") == ("/vyre4/generated_files/pub_module/css/" + Styles[1] + ".css") || a.getAttribute("href") == ("/generated_files/pub_module/css/" + Styles[1] + ".css") || a.getAttribute("href") == ("/vyre4/generated_files/pub_module/css/" + Styles[2] + ".css") || a.getAttribute("href") == ("/generated_files/pub_module/css/" + Styles[2] + ".css"))
        {
          a.disabled = false; // common and colours and background enabled
          document.getElementById('switchlinks').innerHTML = basicLinksString + textLinkString;
        }
      }
    }
  } 
}
}

function setStyleFull()
{
  createCookie("style", "graphical", 365);
  setActiveStyleSheet('graphical');

  createCookie("graphicalreload", "0", 365);

  //document.getElementById("msg1").style.display = "block";//new ticker javascript
  //document.getElementById("msg2").style.display = "none"; //news ul list only

  checkrefresh1();

}

function setStyleText()
{
  createCookie("style", "text", 365);
  setActiveStyleSheet('text');

  //document.getElementById("msg1").style.display = "none"; //new ticker javascript
  //document.getElementById("msg2").style.display = "block"; //news ul list only

  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
      a.disabled = true;
  };

  $.cookie("stylerefresh", "2", { expires: 365, path: '/', domain: 'aat.org.uk'});

  images = document.getElementsByTagName('img')
                var l = images.length;
                for (var i = 0; i < l; i++) {
                   images[0].parentNode.removeChild(images[0]);
                }

                background = document.getElementsByTagName('background')
                var B = background.length;
                for (var i = 0; i < B; i++) {
                   background[0].parentNode.removeChild(background[0]);
                }

               // createCookie("imagecheck", "hide", 365);
}

// **************************************************************************************************************************

function setStylesheet(medium) {
 var stylesheets = document.getElementsByTagName('link');
 for (i=0;i<stylesheets.length;i++) {
   var thisStyleMedium = stylesheets[i].getAttribute('media');
     if(thisStyleMedium == medium) {
  	   stylesheets[i].disabled = false;
  	 } else if (thisStyleMedium == 'text' || thisStyleMedium == 'screen') {
  	   stylesheets[i].disabled = true;
  	 }
	}
  document.getElementById('switchlinks').innerHTML = basicLinksString + basicLinksString2 + (medium=='screen'?textLinkString:graphicalLinkString);
}

// ***************************************************************************************************************************

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function checkExamForm() {
  if (document.aatUserEditForm.termsread.checked) document.enterExamForm.submit();
  else alert("You must read the terms and condtions and check the confirmation box before you can continue");
}

// These functions used to say document.forms[1] - expecting the form being tested to be the second on the page
// Termporarily fixed this to assume it's the last form (because the presence of search is unpredictable)
// Now it says document.forms[document.forms.length-1]
// Will fix it later genuinely to check the form clicked

// James - I have changed these to reference the correct form by id instead of position.
// I have left in using document.forms[document.forms.length-1] for if the id is wrong.

function registerhiddendobandvalidate(chk) {
  var a;
  if(document.getElementById('aatUserEditForm'))
    a = document.getElementById('aatUserEditForm');
  else
    a = document.forms[document.forms.length-1];

  var dob = a.day.value + '/' + a.month.value + '/' + a.year.value;
  document.getElementById('hiddendob').innerHTML = '<input type="hidden" name="dob" value="' + dob + '"/>';

  if (chk.checked == 1)
    a.submit();
  else
    alert("You must read the terms and conditions and check the confirmation box before you can continue"); 
}

function fillpostcodeandsubmit(type) {
  var a;
  if(document.getElementById('aatUserEditForm'))
    a = document.getElementById('aatUserEditForm');
  else
    a = document.forms[document.forms.length-1];
  if (eval('a.' + type + 'country').value == 'UK')
  {
    var postcodestring = '';
    postcodestring = eval('a.' + type + 'postcode1.value');
    postcodestring = postcodestring + ' ';
    postcodestring = postcodestring + eval('a.' + type + 'postcode2.value');
    eval('a.' + type + 'postcode.value = postcodestring');
  }
  a.submit();
}

function fillcpdsubjectandtype() {
  var a;
  if(document.getElementById('aatUserEditForm'))
    a = document.getElementById('aatUserEditForm');
  else
    a = document.forms[document.forms.length-1];
  var subjectfield = a.subject;
  var typefield = a.type;
  a.subjectname.value = subjectfield.options[subjectfield.selectedIndex].text;
  a.typename.value = typefield.options[typefield.selectedIndex].text;
}


function fillletters() {
  var hsb;
  if(document.getElementById('aatUserEditForm'))
    hsb = document.getElementById('aatUserEditForm').honoursselect;
  else
    hsb = document.forms[document.forms.length-1].honoursselect;
 var len = hsb.length;
 var i = 0;
 var honours = "";

 for (i = 0; i < len; i++) 
 {


   if (hsb[i].selected) 
   {
     honours = honours + " " + hsb[i].value;
   }
 }
  //document.forms[1].honours.value = "";

  if(document.getElementById('aatUserEditForm'))
    document.getElementById('aatUserEditForm').honours.value = document.getElementById('aatUserEditForm').honours.value + honours;
  else
    document.forms[document.forms.length-1].honours.value = document.forms[document.forms.length-1].honours.value + honours;
}

function filladdress(type) {
  var a;
  if(document.getElementById('aatUserEditForm'))
    a = document.getElementById('aatUserEditForm');
  else
    a = document.forms[document.forms.length-1];
  if (type == 'work')
  {
    a.jobtitle.value = a.fill_jobtitle.value;
    a.jobdept.value = a.fill_jobdept.value;
    a.workname1.value = a.fill_name1.value;
    a.workname2.value = a.fill_name2.value;
  }
  eval('a.' + type + 'line1.value = a.fill_line1.value');
  eval('a.' + type + 'line2.value = a.fill_line2.value');
  eval('a.' + type + 'line3.value = a.fill_line3.value');
  eval('a.' + type + 'country.value = a.fill_country.value');
  eval('a.' + type + 'phone.value = a.fill_phone.value');
  eval('a.' + type + 'town.value = a.fill_town.value');
  filltowncounty(type);
  if (eval('a.' + type + 'country.value == "UK"')) fillukpostcode(type,a.fill_postcode.value);
  else eval('a.' + type + 'postcode.value = a.fill_postcode.value');
  eval('a.' + type + 'county.value = a.fill_county.value');
}

function fillukpostcode(type,postcodestring)
{
  var a;
  if(document.getElementById('aatUserEditForm'))
    a = document.getElementById('aatUserEditForm');
  else
    a = document.forms[document.forms.length-1];
  var postcodestring1 = postcodestring.substring(0,postcodestring.length - 4);
  var postcodestring2 = postcodestring.substring(postcodestring.length - 3,postcodestring.length);
  eval('a.' + type + 'postcode1.value = postcodestring1');
  eval('a.' + type + 'postcode2.value = postcodestring2');
}

function fillcountry(type) {
  var a;
  if(document.getElementById('aatUserEditForm'))
    a = document.getElementById('aatUserEditForm');
  else
    a = document.forms[document.forms.length-1];
  var field = type + 'country';
  eval('a.' + field + '.value = a.fill_country.value');
}

function townchange(type) {
  var a;
  if(document.getElementById('aatUserEditForm'))
    a = document.getElementById('aatUserEditForm');
  else
    a = document.forms[document.forms.length-1];
  if (eval('a.' + type + 'country.value') == 'UK')
  {
    var county = eval('a.' + type + 'county.value');
    var postcode1 = eval('a.' + type + 'postcode1.value');
    var postcode2 = eval('a.' + type + 'postcode2.value');
    filltowncounty(type);
    eval('a.' + type + 'county.value = county');
    eval('a.' + type + 'postcode1.value = postcode1');
    eval('a.' + type + 'postcode2.value = postcode2');
  }
}

function filltowncounty(type) {
  var a;
  if(document.getElementById('aatUserEditForm'))
    a = document.getElementById('aatUserEditForm');
  else
    a = document.forms[document.forms.length-1];
  if (eval('a.' + type + 'country.value') == 'UK')
  {
     if (eval('a.' + type + 'town.value').toUpperCase() == 'LONDON')
     {
       document.getElementById('townlabel').innerHTML = '<label for="' + type + 'town">Town</label>';
       document.getElementById('countylabel').innerHTML = '';
       document.getElementById('countyfield').innerHTML = '<input type="hidden" name="' + type + 'county" value="" /> ';
       document.getElementById('postcodelabel').innerHTML = '<label for="' + type + 'postcode">Postcode</label>';
     document.getElementById('postcodefield').innerHTML = '<input type="text" name="' + type + 'postcode1" maxlength="4" value="" size="13" /><input type="text" name="' + type + 'postcode2" maxlength="3" value="" size="12" /><b class="error">*</b><input type="hidden" name="' + type + 'postcode" value=""/>';
     }
     else
     {
     document.getElementById('townlabel').innerHTML = '<label for="' + type + 'town">Town</label>';
     document.getElementById('countylabel').innerHTML = '<label for="' + type + 'county">County</label>';
     document.getElementById('countyfield').innerHTML = '<select name="' + type + 'county"><option value="" selected="selected"></option><option value="Aberdeenshire">Aberdeenshire</option><option value="Alderney">Alderney</option><option value="Angus">Angus</option><option value="Argyll">Argyll</option><option value="Avon">Avon</option><option value="Ayrshire">Ayrshire</option><option value="Banffshire">Banffshire</option><option value="Bedfordshire">Bedfordshire</option><option value="Berkshire">Berkshire</option><option value="Berwickshire">Berwickshire</option><option value="Borders">Borders</option><option value="Brecknockshire">Brecknockshire</option><option value="Buckinghamshire">Buckinghamshire</option><option value="Bute-Shire">Bute-Shire</option><option value="Caernarfonshire">Caernarfonshire</option><option value="Caithness">Caithness</option><option value="Cambridgeshire">Cambridgeshire</option><option value="Cardiganshire">Cardiganshire</option><option value="Carmarthenshire">Carmarthenshire</option><option value="Central">Central</option><option value="Channel Islands">Channel Islands</option><option value="Cheshire">Cheshire</option><option value="Clackmannanshire">Clackmannanshire</option><option value="Cleveland">Cleveland</option><option value="Clwyd">Clwyd</option><option value="Cornwall">Cornwall</option><option value="County Antrim">County Antrim</option><option value="County Armagh">County Armagh</option><option value="County Down">County Down</option><option value="County Durham">County Durham</option><option value="County Fermanagh">County Fermanagh</option><option value="County Londonderry">County Londonderry</option><option value="County Tyrone">County Tyrone</option><option value="Cumberland">Cumberland</option><option value="Cumbria">Cumbria</option><option value="Denbighshire">Denbighshire</option><option value="Derbyshire">Derbyshire</option><option value="Devon">Devon</option><option value="Dorset">Dorset</option><option value="Dumfriesshire">Dumfriesshire</option><option value="Dunbartonshire">Dunbartonshire</option><option value="Dyfed">Dyfed</option><option value="East Lothian">East Lothian</option><option value="East Sussex">East Sussex</option><option value="East Yorkshire">East Yorkshire</option><option value="Essex">Essex</option><option value="Fife">Fife</option><option value="Flintshire">Flintshire</option><option value="Gloucestershire">Gloucestershire</option><option value="Grampian">Grampian</option><option value="Greater Manchester">Greater Manchester</option><option value="Guernsey">Guernsey</option><option value="Gwent">Gwent</option><option value="Gwynedd">Gwynedd</option><option value="Hampshire">Hampshire</option><option value="Herefordshire">Herefordshire</option><option value="Hertfordshire">Hertfordshire</option><option value="Highlands">Highlands</option><option value="Huntingdonshire">Huntingdonshire</option><option value="Inverness-shire">Inverness-shire</option><option value="Isle of Arran">Isle of Arran</option><option value="Isle of Barra">Isle of Barra</option><option value="Isle of Benbecula">Isle of Benbecula</option><option value="Isle of Bute">Isle of Bute</option><option value="Isle of Harris">Isle of Harris</option><option value="Isle of Islay">Isle of Islay</option><option value="Isle of Lewis">Isle of Lewis</option><option value="Isle of Man">Isle of Man</option><option value="Isle of North Uist">Isle of North Uist</option><option value="Isle of Scalpay">Isle of Scalpay</option><option value="Isles of Scilly">Isles of Scilly</option><option value="Isle of Skye">Isle of Skye</option><option value="Isle of South Uist">Isle of South Uist</option><option value="Isle of Wight">Isle of Wight</option><option value="Jersey">Jersey</option><option value="Kent">Kent</option><option value="Kincardineshire">Kincardineshire</option><option value="Kinross-shire">Kinross-shire</option><option value="Kirkcudbrightshire">Kirkcudbrightshire</option><option value="Lanarkshire">Lanarkshire</option><option value="Lancashire">Lancashire</option><option value="Leicestershire">Leicestershire</option><option value="Lincolnshire">Lincolnshire</option><option value="Lothian">Lothian</option><option value="Merioneth">Merioneth</option><option value="Merseyside">Merseyside</option><option value="Mid Glamorgan">Mid Glamorgan</option><option value="Middlesex">Middlesex</option><option value="Midlothian">Midlothian</option><option value="Monmouthshire">Monmouthshire</option><option value="Montgomeryshire">Montgomeryshire</option><option value="Morayshire">Morayshire</option><option value="Nairnshire">Nairnshire</option><option value="Norfolk">Norfolk</option><option value="North Humberside">North Humberside</option><option value="North Yorkshire">North Yorkshire</option><option value="Northamptonshire">Northamptonshire</option><option value="Northumberland">Northumberland</option><option value="Nottinghamshire">Nottinghamshire</option><option value="Orkney">Orkney</option><option value="Oxfordshire">Oxfordshire</option><option value="Peebleshire">Peebleshire</option><option value="Pembrokeshire">Pembrokeshire</option><option value="Perthshire">Perthshire</option><option value="Powys">Powys</option><option value="Radnorshire">Radnorshire</option><option value="Renfrewshire">Renfrewshire</option><option value="Ross-shire">Ross-shire</option><option value="Roxburghshire">Roxburghshire</option><option value="Rutland">Rutland</option><option value="Sark">Sark</option><option value="Selkirkshire">Selkirkshire</option><option value="Shetland">Shetland</option><option value="Shropshire">Shropshire</option><option value="Somerset">Somerset</option><option value="South Glamorgan">South Glamorgan</option><option value="South Humberside">South Humberside</option><option value="South Yorkshire">South Yorkshire</option><option value="Staffordshire">Staffordshire</option><option value="Stirlingshire">Stirlingshire</option><option value="Strathclyde">Strathclyde</option><option value="Suffolk">Suffolk</option><option value="Surrey">Surrey</option><option value="Sutherland">Sutherland</option><option value="Tayside">Tayside</option><option value="Tyne and Wear">Tyne and Wear</option><option value="Warwickshire">Warwickshire</option><option value="West Glamorgan">West Glamorgan</option><option value="West Lothian">West Lothian</option><option value="West Midlands">West Midlands</option><option value="West Sussex">West Sussex</option><option value="West Yorkshire">West Yorkshire</option><option value="Western Isles">Western Isles</option><option value="Westmoreland">Westmoreland</option><option value="Wigtownshire">Wigtownshire</option><option value="Wiltshire">Wiltshire</option><option value="Worcestershire">Worcestershire</option><option value="Vale of Glamorgan">Vale of Glamorgan</option></select> <b class="error">*</b>';
     document.getElementById('postcodelabel').innerHTML = '<label for="' + type + 'postcode">Postcode</label>';
     document.getElementById('postcodefield').innerHTML = '<input type="text" name="' + type + 'postcode1" maxlength="4" value="" size="13" /> <input type="text" name="' + type + 'postcode2" maxlength="3" value="" size="12" /><b class="error">*</b><input type="hidden" name="' + type + 'postcode" value=""/>';
    }  
  }
  else
  {
     document.getElementById('townlabel').innerHTML = '<label for="' + type + 'town">Town/Region:</label>';
     document.getElementById('countylabel').innerHTML = '';
     document.getElementById('countyfield').innerHTML = '<input type="hidden" name="' + type + 'county" value="" /> ';
     document.getElementById('postcodelabel').innerHTML = '<label for="' + type + 'postcode">Postal Code</label>';
     document.getElementById('postcodefield').innerHTML = '<input type="text" name="' + type + 'postcode" maxlength="9" value="" size="30" />';
  }
}

function filldatediv() {
  var months = new Array(12);
  months[0] = "January";
  months[1] = "February";
  months[2] = "March";
  months[3] = "April";
  months[4] = "May";
  months[5] = "June";
  months[6] = "July";
  months[7] = "August";
  months[8] = "September";
  months[9] = "October";
  months[10] = "November";
  months[11] = "December";
  var time = new Date();
  var lmonth = months[time.getMonth()];
  var date = time.getDate();
  var year = time.getYear();
  if (year < 2000) year = year + 1900;
  document.write(lmonth + ' ' + date + ", " + year);
}

function popupWindow(modal, href, x, y)
{
  LeftPosition = (screen.width) ? (screen.width-x)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-y)/2 : 0;
  if( modal )
  	showModalDialog(href,"","dialogWidth:"+x+"px; dialogHeight:"+y+"px; dialogLeft:"+LeftPosition+"; dialogTop:"+TopPosition +"");
  else   window.open(href,"","toolbar=0,location=0,directories=0,status=yes,menubar=0,scrollbars=yes,resizable=no,copyhistory=0,width="+x+",height="+y+',left='+LeftPosition+',top='+TopPosition+"");
}


function showemailmeexamresults() {
    var checkBoxObj = document.getElementById('emailmeexamresults');
    if (checkBoxObj.checked == true){
        popupWindow(false,'/account/sendexamresults',300,300);
    }  
}

function selectOption(value, options) {
    for (var i = 0; i < options.length; i++) {
        if (options[i].value == value) {
            options[i].selected = true;
            return true;
        }
    }
}

// changed - old version was not accepting decimal points

function isInteger(s)
{   
    var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (c != "." && (c < "0" || c > "9")) return false;
    }
    return true;
}

