// $Id:$ */
// Janmedia Interactive 

FAILED_INPUT_BACKGROUND_COLOR = "#FEDDBC";

function checkInput(input,message)
{
    if (input.value.length==0) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function checkSelect(input,message)
{
    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; 
    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 checkCheckboxes(input,message)
{
    if (input.length)
    {
        var i;
        for (i=0; i<input.length; i++)
        {
            if (input[i].checked)
            {
                return true;
            }
        }
        alert(message);
        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;
    }
}

function setCountry(countryinput, stateinput){
  if (stateinput.value != "Outside US") countryinput.value="United States";
}
function setState(countryinput, stateinput){
  if (countryinput.value != "United States") stateinput.value = "Outside US";
}