// JavaScript Document

function GetCookie(name) 
{
	var startIndex = document.cookie.indexOf(name); 
	if (startIndex != -1) 
		{
		var endIndex = document.cookie.indexOf(";", startIndex); 
		if (endIndex == -1) endIndex = document.cookie.length;
		var cookie = unescape(document.cookie.substring(startIndex+name.length+1, endIndex));
		return cookie;
		}
	else
		return null;
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 
 // create cookie in "gadgetfix.com" domain
 
 var domain = ".gadgetfix.com";
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString()
				 + ";domain="+domain;

// make sure to delete any old cookies left over in "www.gadgetfix.com" domain

 domain = "www.gadgetfix.com";
 expire.setTime(today.getTime() - 1); // "already expired"
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString()
				 + ";domain="+domain;
}

function decToHex(dec)
{
  var hexStr = "0123456789ABCDEF";
  var low = dec % 16;
  var high = (dec - low)/16;
  hex = "" + hexStr.charAt(high) + hexStr.charAt(low);
  return hex;
}

function GetStyleColor(colorname)
{
	var theRules = new Array();
	var stylename = '.' + colorname.toLowerCase();
	
	if (document.styleSheets[0].cssRules)
		theRules = document.styleSheets[0].cssRules
	else if (document.styleSheets[0].rules)
		theRules = document.styleSheets[0].rules
	else 
		return '000000';
	
	var cstyles = theRules.length;

	for (var i = 0; 
		 (i < cstyles) && (theRules[i].selectorText.toLowerCase() != stylename); 
		 i++) ;
	
	if (i == cstyles)
		return '000000'
	else
		{
		var col = theRules[i].style.color;
		
		if (col.charAt(0)=='#')
			return col.substr(1)
		else
			{
			var bytes = new Array();
			
			col = col.substr(4);
			col = col.substring(0, col.length - 1);
			
			bytes = col.split(',');
			
			return decToHex(bytes[0]) + decToHex(bytes[1]) + decToHex(bytes[2]);
			}
		}
}

function GetSkinString(strname)
{
	var theRules = new Array();
	var stylename = '.str-' + strname.toLowerCase();
	
	if (document.styleSheets[0].cssRules)
		theRules = document.styleSheets[0].cssRules
	else if (document.styleSheets[0].rules)
		theRules = document.styleSheets[0].rules
	else 
		return '';
	
	var cstyles = theRules.length;

	for (var i = 0; 
		 (i < cstyles) && (theRules[i].selectorText.toLowerCase() != stylename); 
		 i++) ;
	
	if (i == cstyles)
		return ''
	else
		return theRules[i].style.backgroundImage;
}

function showheadline(headline, permalink)
	{
	var html = '';
	var bighed = (GetCookie('sitestyle') == 'styles-alt2') && fFirst;
	
	fFirst = false;

	html += '<a href="' + permalink + '">';
	
	if (bighed)
		html += '<span class="hedstyle">';
	else
		html += '<div class="title">';

	html += headline;
	
	if (bighed)
		html += '</span>';
	else
		html += '</div>';
	
	html += '</a>';
	
	if (bighed)
		{
		var hedDiv = getRefToDiv('hed');
		hedDiv.innerHTML += html;
		show_div('hed');
		
		var contDiv = getRefToDiv('content');
		contDiv.style.paddingTop = 0;

		var navDiv = getRefToDiv('navigation');
		navDiv.style.borderTopWidth = "1px";
		navDiv.style.borderTopStyle = "solid";
		navDiv.style.borderTopColor = GetStyleColor('banner');
		}
	else
		document.write(html);
	}
	
function checkEmail(addr, warning_label, allowblank) 
{
	if ((allowblank && (addr=="")) || (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(addr)))
		{
		fInvalidEmail = false;
		warning_label.style.display = 'none';
		return (true);
		}
		
	fInvalidEmail = true;
	warning_label.style.display = 'inline';
	return (false);
}

function checkCommentEmailField(addr, warning_label)
{
	if (fInvalidEmail)
		checkEmail(addr, warning_label, true);
}

function checkBody(txt, submit_button)
{
	if (txt=="")
		{
		fEmptyBody = true;
		submit_button.className = "form_button_disabled";
		submit_button.disabled = true;
		return false;
		}
	else
		{
		fEmptyBody = false;
		submit_button.disabled = false;
		submit_button.className = "form_button";
		return true;
		}
}

function checkFeedbackEmailField(addr, warning_label)
{
	checkFeedbackForm();
	if (fInvalidEmail)
		checkEmail(addr, warning_label, false);
}

function checkFeedbackForm()
{
	document.feedback_form.submit_button.disabled = 
	   !((document.feedback_form.name.value != "") && 
	 	(document.feedback_form.email.value != "") && 
		(document.feedback_form.comments.value != ""));
}

function checkIdeaEmailField(addr, warning_label)
{
	checkIdeaForm();
	if (fInvalidEmail)
		checkEmail(addr, warning_label, false);
}

function checkIdeaForm()
{
	document.idea_form.submit_button.disabled = 
	   !((document.idea_form.email.value != "") && 
		(document.idea_form.comments.value != ""));
}


var skinMenuItems = new Array("../images/alt3_style_menu_item.jpg", 
							  "../images/alt1_style_menu_item.jpg",
							  "../images/alt2_style_menu_item.jpg",
							  "../images/default_style_menu_item.jpg",
							  "../images/alt4_style_menu_item.jpg",
							  "../images/alt5_style_menu_item.jpg");
							  
var skinMenuValues = new Array("styles-alt3", "styles-alt1", "styles-alt2", "styles-site", "styles-alt4", "styles-alt5");

var DEFAULT_STYLE_SHEET = "styles-alt3";

var downarrow = "../images/down_arrow.jpg";
var uparrow = "../images/up_arrow.jpg";


function GetPreferredStyleSheet()
{
	var style = GetCookie('sitestyle');
	
	if (style==null) {style=DEFAULT_STYLE_SHEET;}
	
	return style;
}

function getRefToDiv(divID,oDoc) {
    if( !oDoc ) { oDoc = document; }
    if( document.layers ) {
        if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
            //repeatedly run through all child layers
            for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
                //on success, return that layer, else return nothing
                y = getRefToDiv(divID,oDoc.layers[x].document); }
            return y; } }
    if( document.getElementById ) {
        return document.getElementById(divID); }
    if( document.all ) {
        return document.all[divID]; }
    return false;
}

function show_div(divID)
{
	myReference = getRefToDiv(divID);
	
    if( myReference.style ) { //DOM & proprietary DOM
        myReference.style.display = 'block';
    } else {
        if( myReference.visibility ) { //Netscape
            myReference.display = 'block';
        } else {
            return false; //don't go any further
        }
    }
}

function hide_div(divID)
{
	myReference = getRefToDiv(divID);
	
    if( myReference.style ) { //DOM & proprietary DOM
        myReference.style.display = 'none';
    } else {
        if( myReference.visibility ) { //Netscape
            myReference.display = 'none';
        } else {
            return false; //don't go any further
        }
    }
}

function buildSkinMenu()
{
	menuDIV = getRefToDiv('skinselector');
	
	menuDIV.style.height = skinMenuItems.length * (34 + 8);
	menuDIV.style.width = "128";
	
	for (var i = 0; i < skinMenuItems.length; i++)
		{
		var newhtml = '';
		
		newhtml += '<div id="skinmenuitem' + i + '"';
		
		if (i + 1 < skinMenuItems.length)
			newhtml += ' class="skinmenuitem"';
		else
			newhtml += ' class="skinmenuitemlast"';
	
		newhtml += ' onmousedown="selectItem(this)"';
		
		newhtml += ' onmouseover="highlightItem(this)"';

		newhtml += ' onmouseout="highlightClear(this)"';
		
		newhtml +='>';
			
		newhtml += '<img src="' + skinMenuItems[i] + '" border="0" height="34" width="120"></div>';
		
		menuDIV.innerHTML += newhtml;
		}
}

var menuitemOldHighlight;

function highlightItem(menuitem)
{
	if (menuitem == menuitemOldHighlight)
		return;
	
	if (menuitemOldHighlight)
		menuitemOldHighlight.style.backgroundColor = '#' + GetStyleColor('textbg');
		
	menuitem.style.backgroundColor = '#' + GetStyleColor('color1');

	menuitemOldHighlight = menuitem;
}

function highlightClear(menuitem)
{
	menuitem.style.backgroundColor = '#' + GetStyleColor('textbg');
	
	if (menuitemOldHighlight)
		menuitemOldHighlight.style.backgroundColor = '#' + GetStyleColor('textbg');

	menuitemOldHighlight = null;
}

function selectItem(menuitem)
{
	var newstyle;
	
	i = menuitem.id.substr(12);
	
	newstyle = skinMenuValues[i];
	
	if (newstyle == GetCookie('sitestyle'))
		{
		toggleskinselector();
		return;
		}
	
	//var url = "http://pilafidis.org/switcher.php?set=" + newstyle;
	//url += "&ref=" + window.location.href;
	//window.location.href = url;
	
	switchStyle(newstyle);
}

function switchStyle(newstyle)
{
	SetCookie('sitestyle', newstyle, 365);
	document.location.reload();
}

var fskinselectoropen = false;

function toggleskinselector()
{
	if (fskinselectoropen)
		{
		hide_div('skinselector');
		if (document.getElementById)
			document.getElementById('skinselectorarrow').src = downarrow;
		fskinselectoropen = false;
		}
	else
		{
		show_div('skinselector');
		if (document.getElementById)
			document.getElementById('skinselectorarrow').src = uparrow;
		fskinselectoropen = true;
		}
}

function SearchValidator() {
    if (document.blogsearch.search.value.length > 0)
        return true;
    else {
        return false;
    }
}


