// general functions
function addLoadEvent(func) {	
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function switchStyleSheet() {
    var objHiRes = document.getElementById('ctl00_cssDefault');
    var objLowRes = document.getElementById('ctl00_cssLowRes');

    // turn off - start in low res mode
    objHiRes.disabled = true;
    objLowRes.disabled = true;

    if (getWindowWidth() <= 1024) {   // low res
    //alert('low res');
        objHiRes.disabled = false;
        objLowRes.disabled = false;
    }
    else {   // high res
        objHiRes.disabled = false;
        objLowRes.disabled = true;
    }
}

function getWindowWidth() {
    var winW = window.screen.width;

    if (window.innerWidth)
        winW = window.innerWidth;
    else {
        if (document.documentElement.offsetWidth)
            winW = document.documentElement.offsetWidth;
        else if (document.body.offsetWidth)
            winW = document.body.offsetWidth;
    }
    return winW;
}

function getElementByClass(classname){ 
     var messages=new Array();
     var inc=0; 
     var alltags=document.all? document.all : document.getElementsByTagName("*"); 

     for (i=0; i<alltags.length; i++){
        if (alltags[i].className.match(classname)){ 
         messages[inc++]=alltags[i]; 
         }
     }
     if(messages != null){return messages;}
     else {return null;}
} 

function openPopup(url, width, height)
{
    var	newwindow=window.open(url,'product','width=' + width + ',height=' + height + ',status=no,toolbar=no');
	if (window.focus) {newwindow.focus()}
	return false;
}

// cookie functions
function setCookie(name,value,days,path,domain,secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


function doSelectPostCode() {
    var elementRadio = document.getElementById('ctl00_cphContent_rbNearestPostCode');

    if (elementRadio!=null){
        elementRadio.checked = true;
    }
}

function doSelectArea() {
    var elementRadio = document.getElementById('ctl00_cphContent_rbArea');

    if (elementRadio!=null){
        elementRadio.checked = true;
    }
}

//error handling
function ValidateTandCs(source, args){
    if (document.getElementById("ctl00_cphContent_chkTerms").checked)
        args.IsValid = true;
    else
        args.IsValid = false; 
} 

function applyShowHide(id){
    var elementsLongdesc = document.getElementById(id);
    if (elementsLongdesc!=null){
        if (elementsLongdesc.className == 'display-hide'){
            elementsLongdesc.className = 'display-show'; }
        else {elementsLongdesc.className = 'display-hide'; }
    }
    return false;
}


// Opacity functions
function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

function confirmDelete() {
	if (confirm('Are you sure you want to delete this record?')){
		return true;}		
	else{return false;}
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function applySearchOnfocus(){
    var eleSearch = document.getElementById("ctl00_txtSearch");
    
    if (eleSearch!=null){
        var tmpResult = eleSearch.value;
        eleSearch.onfocus = function () { 
                        if (this.value == 'Search' ) {
                            eleSearch.value = '';
                        }
                     }
//        eleSearch.onblur = function () { 
//                        if (this.value == '' ) {
//                            eleSearch.value = 'Search';
//                        }
//                     }             
    } 
} 

function applyEmailOnfocus(){
    var ele = document.getElementById("ctl00_txtEmail");
    
    if (ele!=null){
        var tmpResult = ele.value;
        ele.onfocus = function () { 
                        if (this.value == 'Email sign up' ) {
                            ele.value = '';
                        }
                     }   
//         ele.onblur = function () { 
//           if (this.value == '' ) {
//                ele.value = 'Sign up for email';
//            }
//         }        
    } 
}

// load and attach events
// load and attach events
switchStyleSheet();
window.onresize = switchStyleSheet;
addLoadEvent(applySearchOnfocus);
addLoadEvent(applyEmailOnfocus);


