/***** Text resize functions *****/

// resize text and create cookie
function fsize(size,unit,id){
  var vfontsize = document.getElementById(id);
  if(vfontsize){
   vfontsize.style.fontSize = size + unit;
   createCookie("textsizestyle", textsize, 365);
  }
}
var textsize = 1.0;
function changetextsize(up){
  if(up){
   textsize = parseFloat(textsize)+0.1;
  }else{
   textsize =parseFloat(textsize)-0.1;
  }
}

// setup the onclick events for the text resize links
function initTextResize(){ 
	decreaseText = document.getElementById("decrease-text");
	decreaseText.href = "javascript:fsize(textsize,'em','content');";
	decreaseText.setAttribute('onclick', 'changetextsize(0);'); 
	
	increaseText = document.getElementById("increase-text");
	increaseText.href = "javascript:fsize(textsize,'em','content');";
	increaseText.setAttribute('onclick', 'changetextsize(1);'); 
}

// Set the fontsize from user's cookie */
function setFontSizeFromCookie() {
	var cookie = readCookie("textsizestyle");
	textsize = cookie ? cookie : 1.0;
	fsize(textsize,'em','content');
}

/***** Read and Write Cookie functions *****/

// create cookie
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 var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
 
// read cookie
 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;
}

/***** Form highlighting functions *****/

// highlight field forms on focus
function initHighlight() {
    if (!document.getElementsByTagName){ return; }
    var allfields = document.getElementsByTagName("input");

    // loop through all input tags and add events
    for (var i=0; i<allfields.length; i++){
        var field = allfields[i];
        if ((field.getAttribute("type") == "text") || (field.getAttribute("type") == "password") ) {
            field.onfocus = function () {this.className = 'focus';}
            field.onblur = function () {this.className = 'focus-off';}
        }
    }
}

// Clear text boxes with descriptive values on focus (such as 'min' on the salary text box on the job form) */
function inputClearer()
{
	if (document.getElementById("salary")) {
		salary = document.getElementById("salary");
		salary.onfocus = function () {
			if (this.value == "min"){
				this.value = '';
			}
			this.className = 'focus';
		}
		salary.onblur = function () {
			if (this.value == ""){
				this.value = 'min';
			}
			this.className = 'focus-off';
		}
	}
	
	if (document.getElementById("salaryTo")) {
		salaryTo = document.getElementById("salaryTo");
		salaryTo.onfocus = function () {
			if (this.value == "max"){
				this.value = '';
			}
			this.className = 'focus';
		}	
		salaryTo.onblur = function () {
			if (this.value == ""){
				this.value = 'max';
			}
			this.className = 'focus-off';
		}
	}
	
	if (document.getElementById("reference")) {
		reference = document.getElementById("reference");
		reference.onfocus = function () {
			if (this.value == "reference"){
				this.value = '';
			}
			this.className = 'focus';
		}
		reference.onblur = function () {
			if (this.value == ""){
				this.value = 'reference';
			}
			this.className = 'focus-off';
		}
	}
}


/******************************************/



/***** Execute functions on page load *****/

function initPage()
{
	initTextResize();
	setFontSizeFromCookie();
	initHighlight();
	inputClearer();
}

window.onload = initPage;