var oDiv = document.createElement('div');

/* Initialize this script on load */
window.onload = loadSCStyle;

function loadSCStyle() {

	setupRoundedCorners();
	setupPng();
	setupInput();
	setupLinks();
	preloadImages();
	setupContentBoxes();
	setupHighlightBoxes();


	if(typeof init == "function") init();
}

function setupLinks() {
	links = document.getElementsByTagName('a');
	for(i=0; i<links.length; i++) {
		a = links[i];
		if(a.onfocus == null) {
      a.onclick2 = a.onclick;
      a.onclick = new Function("event", "this.blur(); this.onclick2(event)");
    }
	}
}

function setupRoundedCorners() {

	divs = document.getElementsByTagName("div");
	for(i=0; i<divs.length; i++)
  {
		if(divs[i].className.match('rounded'))
    {
			divs[i].appendChild(createCornerImage('tl'));
      divs[i].appendChild(createCornerImage('tr'));
      divs[i].appendChild(createCornerImage('bl'));
      divs[i].appendChild(createCornerImage('br'));
    }
  }
}

function createCornerImage(pos)
{
  var img       = document.createElement('img');
  img.src       = '/images/corners/whitebg_'+pos+'.png';
  img.className = pos;
  return img;
}

function setupPng() {
	if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent)
		fnLoadPngs();
}


function setupHighlightBoxes() {
	function onMouseOver() { highlightBox(this); }
	function onMouseOut() { normalBox(this); }

	var divs = document.getElementsByTagName("div");
	var div;
	var width;

	for(i=0; i<divs.length; i++) {
		if(divs[i].className.match('mouseHighlight')) {
			div = divs[i];
			div.onmouseover = onMouseOver;
			div.onmouseout = onMouseOut;
			if(div.currentStyle) {
				// Workaround for the IE bug
				width = div.scrollWidth;
				width -= div.currentStyle['paddingLeft'].replace("px","");
				width -= div.currentStyle['paddingRight'].replace("px","");
				div.style.width = width+'px';
			}
		}
	}
}

/* IE needs width of content to display floats inside */
/* Also needed for onmouseover actions in IE */
function setupContentBoxes() {
	var divs = document.getElementsByTagName("div");
	var div;
	var width;

	for(i=0; i<divs.length; i++) {
		if(divs[i].className.match('content')) {
			div = divs[i];
			if(div.currentStyle) {
				// Workaround for the IE bug
				width = div.scrollWidth;
				width -= div.currentStyle['paddingLeft'].replace("px","");
				width -= div.currentStyle['paddingRight'].replace("px","");
				if(width > 0)
					div.style.width = width+'px';
			}
		}
	}}


function highlightBox(obj) {
	obj.className += " mouseover";
	var links = obj.getElementsByTagName("a");
	for(var i=0; i<links.length; i++) {
		if(links[i].className.match("mouseaction")) {
			links[i].className += " hover";
			
		}
	}
}

function normalBox(obj) {
	
	obj.className = obj.className.replace("mouseover", "");

	var links = obj.getElementsByTagName("a");
	for(var i=0; i<links.length; i++) {
		if(links[i].className.match("mouseaction")) {
			links[i].className = links[i].className.replace(" hover", "");
		}
	}
}


/**
 * Apply custom settings to input fields
 */
function setupInput() {
	function fieldOnFocus() { highlightInput(this); }
	function fieldOnBlur() { normalInput(this); }
	function fieldKeyPressed() {  }
	function keypress(e){ enterCheck(this, e); };

	// Loop through all elements in all forms and
	// set up highlighting + tooltips
	for (var i = 0; i < document.forms.length; i++) {
		form = document.forms[i];
		for (var j = 0; j < form.elements.length; j++)
		{
			element = form.elements[j];

			// If it is not a button, apply style-fix (IE-bug)
			// and onFocus/onBlur-functions.
			if(element.type != "submit" && element.type != "button" && element.type != "checkbox" && element.type != "radio")
			{
				element.className = 'input';
				element.onfocus = fieldOnFocus;
				element.onblur = fieldOnBlur;
			}
			
			if(element.type != "submit" && element.type != "button" && element.type != "textarea") {
				element.onkeypress = keypress;
			}
		}
    }

	oDiv.className = 'tooltip';
	document.body.appendChild(oDiv); 
}

function enterCheck(myfield,e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13) {
	   myfield.form.submit();
	}
}

/**
 * Highlight and show description if it exists
 */
function highlightInput(obj) {
	
	// Change backgorund color and get offsetWidth
	obj.className = 'inputHighlighted';

	var width = obj.offsetWidth;

	// Check if there exist a message
	var message;
	if(eval("typeof  tt_"+obj.id+" == 'undefined'"))
		message = null;
	else
		eval("message = tt_"+obj.id);

	if(message == null)
		return;

	// Get x and y position for the input field
	var curtop = 0;
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curtop += obj.y;
		curleft += obj.x;
	}

	// Add the width
	var left = curleft + width+2;

	// Subtract half the padding
	var top = curtop - 3;
		
	oDiv.style.left = left+'px';
	oDiv.style.top  = top+'px';

	oDiv.innerHTML = message;
	oDiv.style.visibility = 'visible';
}

/**
 * Un-highlight the input
 */
function normalInput(obj) {
	obj.className = 'input';
	oDiv.style.visibility = 'hidden';
}

/**
 * Display the language div
 */
function switchLanguage()
{
	var div;
	div = document.getElementById("lang");
	if(div.style.display != "block")
		div.style.display = "block";
	else
		div.style.display = "none";
}

function showLoginBox() {
  var loginForm = document.getElementById("loginForm");
  if(loginForm != null) {
    if(loginForm.style.display == 'none') {
      loginForm.style.display = '';
      document.getElementById("username").focus();
    } else {
      loginForm.style.display = 'none';
    }
  }
}


/**
 * Sleight fix for transparent png files
 **/

function fnLoadPngs() {
	if(document.images.length == 0) return;
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var x = document.images[0];
    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5 && x.style.filters == null);

    for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
        if (itsAllGood && img.src.match(/\.png$/i) != null) {
            fnFixPng(img);
            img.attachEvent("onpropertychange", fnPropertyChanged);
		}
	}

    var nl = document.getElementsByTagName("INPUT");
    for (var i = nl.length - 1, e = null; (e = nl[i]); i--) {
        if (e.className && e.className.match(/\bimage\b/i) != null) {
            if (e.src.match(/\.png$/i) != null) {
                fnFixPng(e);
                e.attachEvent("onpropertychange", fnPropertyChanged);
            }
        }
    }
}

function fnPropertyChanged() {
    if (window.event.propertyName == "src") {
        var el = window.event.srcElement;
		if (!el.src.match(/\/images\/x\.gif$/i)) {
            el.filters.item(0).src = el.src;
            el.src = "/images/x.gif";
        }
    }
}

function fnFixPng(img) {
  var src = img.src;
  img.style.width = img.width + "px";
  img.style.height = img.height + "px";
  
  img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='image')"
  img.src = "/images/x.gif";
	img.pngFix = true;
}


/**
 * Macromedia functions
 */

function MM_preloadImages() { //v3.0
	var d=document;
	if(d.images){
	if(!d.MM_p)
		d.MM_p=new Array();

	var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
	for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0){
			d.MM_p[j]=new Image;
			d.MM_p[j++].src=a[i];
		}
	}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr;
	for(i=0; a && i < a.length && (x=a[i]) && x.oSrc; i++) {
		if(x.pngFix)
			x.filters.item(0).src = x.oSrc;
		else
			x.src=x.oSrc;
	}
}



function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments;
	document.MM_sr=new Array;
	for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null){
			document.MM_sr[j++]=x;
			if(x.pngFix) {
				if(!x.oSrc) {
					var rslt = x.style.filter.match(/src..([\w,.,\/,:]*)/);
					x.oSrc=rslt[1];
				}
				x.filters.item(0).src = a[i+2];
			} else {
				if(!x.oSrc) x.oSrc=x.src;
				x.src=a[i+2];
			}
		}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  var win = window.open(theURL,winName,features);
  win.focus();
}

function preloadImages() {
	MM_preloadImages('/images/tryout_light.png');
	MM_preloadImages('/images/menubutton_hl.png');
	MM_preloadImages('/images/offer_bg_hl.png');
	MM_preloadImages('/images/nextarrow_light.png');
	MM_preloadImages('/images/prevarrow_light.png');
	MM_preloadImages('/images/stop_hl.png');
	MM_preloadImages('/images/tryout_light.png');
	MM_preloadImages('/images/offer_bg_hl.png');

	MM_preloadImages('/images/icons/servicemenu/backup_16_low.png');
	MM_preloadImages('/images/icons/servicemenu/parentalcheck_16_low.png');
	MM_preloadImages('/images/icons/servicemenu/securitybulletin_16_low.png');
	MM_preloadImages('/images/icons/servicemenu/securitycheck_16_low.png');
	MM_preloadImages('/images/icons/servicemenu/spamfilter_16_low.png');

	MM_preloadImages('/images/icons/mailclients/eudora_hl.png');
	MM_preloadImages('/images/icons/mailclients/outlook_hl.png');
	MM_preloadImages('/images/icons/mailclients/outlook_express_hl.png');
	MM_preloadImages('/images/icons/mailclients/thunderbird_hl.png');

	MM_preloadImages('/images/icons/mailclients/thunderbird_hl.png');
	MM_preloadImages('/images/icons/mailclients/thunderbird_hl.png');
}


function gotoURL(dest) {
	top.location.href=dest;
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };


function createQuery(form)
{
    var elements = form.elements;
    var pairs = new Array();
	
    for (var i = 0; i < elements.length; i++) {
        pairs.push(elements[i].name + "=" + encodeURIComponent(elements[i].value));
    }

    return pairs.join("&");
}


function makeRequest(url, onreadystatechange, form) {
	var http_request = false;
	var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	if(onreadystatechange != null)
		http_request.onreadystatechange = function() { onreadystatechange(http_request) };

	http_request.open('post', url, true);
	
	if(form != null) {
		var query = createQuery(form);
		http_request.setRequestHeader("Content-Type", contentType);
		http_request.send(query);
	} else {
		http_request.send(null);
	}
}

function enableForm(form) {
    var elements = form.elements;
    for (var i = 0; i < elements.length; i++)
        elements[i].disabled = false;
}

function disableForm(form) {
    var elements = form.elements;
    for (var i = 0; i < elements.length; i++)
        elements[i].disabled = true;
}


function showhide(num) {
	var div = document.getElementById("cat_"+num);
	if(div.style.display != 'none')
		div.style.display = 'none';
	else
		div.style.display = '';
}

function openSupportWindow(url) {
  MM_openBrWindow(url,'Support','width=700,height=500');
}