// $Id: misc.js,v 1.5 2007/04/18 14:44:36 tkepski Exp $
function checkPollVote(form)
{
	var inputs = form.getElementsByTagName("input");
	for( var i = 0; i < inputs.length; i++ )
	{
		if(inputs[i].getAttribute("type").toLowerCase() == "radio")
		{
			if(inputs[i].checked)
			{
				return true;
			}
		}
	}
	
	alert("Please check an answer!");
	return false;
}

/* ----------------------------------------------------------------------
	POLL VIEW
---------------------------------------------------------------------- */
function setPollView(viewNo, thisLink)
{
	var view = document.getElementById("change_poll_view");
	if(!view) return;
	var j = 1;
	for( var i = 0; i < view.childNodes.length; i++)
	{
		if(view.childNodes.item(i).nodeType == 1)
		{
			if(j++ == viewNo)
				view.childNodes.item(i).style.display="block";
			else
				view.childNodes.item(i).style.display="none";
			
		}
	}
	
	var switchers = document.getElementById("change_poll_view_switcher").getElementsByTagName("a");
	for(var i = 0; i < switchers.length; i++ )
	{
		switchers[i].className = switchers[i].className.replace("selected", "");
	}
	thisLink.className += " selected";
	
	
	return false;
}

function PopupWindow(url,width,height,name,scroll){
    if (name==null){
      name = new String(Math.round(Math.random()*10000000));
    }
		var scrollbar = (scroll=true) ? "yes" : "no";
			
    var left = (screen.width - width)/2;
    var top = (screen.height - height)/2 - 18;

    var win = window.open(url,name,"toolbar=no,location=no,directories=no,"+
      "status=no,menubar=no,scrollbars="+scrollbar+",resizable=no,"+
      "copyhistory=no, width="+width+",height="+height+",left="+left+",top="+top);
}

function PopupImage(url,width,height,title,name)
{
    if (name==null){
      name = new String(Math.round(Math.random()*10000000));
    }
    var windowheight = height+40;
    var windowwidth = width+20;

    var left = (screen.width - windowwidth)/2;
    var top = (screen.height - windowheight)/2 - 18;
    
    var scrollbars = "no";
    
    if (left<0) { scrollbars = "yes"; left = 32; windowwidth = screen.width - 2*left; }
    if (top<0) { scrollbars = "yes"; top = 32; windowheight =  screen.height - 2*top - 18; top = top - 18; }

    var win = window.open("",name,"toolbar=no,location=no,directories=no,"+
        "status=no,menubar=no,scrollbars=" + scrollbars + ",resizable=no,"+
        "copyhistory=no, width="+windowwidth+",height="+windowheight+",left=" + left + ",top=" + top);
    win.document.write("\<HEAD\>\<link rel=\"stylesheet\" type=\"text\/css\" href=\"\/style.css\"\/\>\<meta http-equiv=\"pragma\" content=\"no-cache\"\>\<TITLE\>"+title+"\<\/TITLE\>");
    win.document.write("\<link rel=\"STYLESHEET\" type=\"text/css\" href=\"css/style.css\"\>");
    win.document.write("\<\/HEAD\>");
    win.document.write("\<BODY topmargin=0 leftmargin=0\>\<center\>\<img src=\""+url+"\" width=\""+width+"\" height=\""+height+"\" border=0 vspace=\"10\" hspace=\"10\"\>\<BR\>\<a href=\"javascript:window.close();\" class=\"popup\"\>Close\<\/a\>\<\/center\>\<\/BODY\><\/HTML\>");
}

function jmShowHelp(url)
{
    var width  = 750;
    var height = 580;
    var left   = (screen.width - width)/2;
    var top    = (screen.height - height)/2

    var win = window.open(url,null,"toolbar=no,location=no,directories=no,"+
      "status=no,menubar=no,scrollbars=yes,resizable=yes,"+
      "copyhistory=no, width="+width+",height="+height+",left=" + left + ",top=" + top);
   
    return false;

}

/* ----------------------------------------------------------------------
	DEFAULT VALIDATION FORMS
---------------------------------------------------------------------- */
FAILED_INPUT_BACKGROUND_COLOR = "#FEDDBC";
function checkInput(input,message)
{
	if (!input)
	{
		alert(message + "\nInput not found!");		
		return false;
	}

    if (input.type=="checkbox") 
    {
    	if (!input.checked)
    	{
	        focusFailedInput(input,message);
	        return false;
	    }
	    return true;
    }
	
    if (input.value.length==0) 
    {
        focusFailedInput(input,message);
        return false;
    }
    
    return true;
}

function checkSelect(input,message)
{
	if (!input)
	{
		alert(message + "\nInput not found!");		
		return false;
	}
    if (input.disabled == true) return true;
    if (input.selectedIndex==0) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function focusFailedInput(input, message)
{
    input.oldOnBlur = input.onblur;
    input.oldBackgroundColor = input.style.backgroundColor;
    input.style.backgroundColor = FAILED_INPUT_BACKGROUND_COLOR;
    if (message) alert(message);
    input.onblur = onAfterBlurFailedInput; 
    if (!input.disabled) input.focus();
}

function markFailedInput(input)
{
    input.style.border = "1px solid red";
}

function checkEmail(input,message)
{
    if (!_checkEmail(input.value)) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function _checkEmail(email)
{
	if (email == "") return false;
  template=/^[0-9a-z]+[0-9a-z._-]*\@[0-9a-z]+[0-9a-z._-]*\.[0-9a-z]{2,}$/i;
  if (template.test(email) == false) return false;
	return true;
}

function checkNumberInput(input,message)
{
  
	template=/^[0-9]*$/i;
	if ((input.value.length==0) || !template.test(input.value))
	{
	    focusFailedInput(input,message);
	    return false;
	}
	return true;
}

function checkCheckboxes(input,message)
{
    if (input.length)
    {
        var i;
        for (i=0; i<input.length; i++)
        {
            if (input[i].checked)
            {
                return true;
            }
        }
        alert(message);
        if (!input[0].disabled) input[0].focus();
        return false;
    }
    return true;
}

function onAfterBlurFailedInput()
{
    if (this.oldOnBlur)
    {
        this.onblur = this.oldOnBlur; 
    }
    if (this.oldBackgroundColor!=null)
    {
        this.style.backgroundColor = this.oldBackgroundColor;
    }
}


