// $Id: forms.js,v 1.1 2008-01-04 16:46:51 dkumor Exp $
// Janmedia Interactive 

FAILED_INPUT_BACKGROUND_COLOR = "#FEDDBC";

function checkInput(input,message)
{
	if (!input)
	{
		alert(message + "\nInput not found!");		
		return false;
	}
	
    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;
  template=/^[0-9]*$/i;
  //if (template.test(email) == false) return false;

    if ((input.value.length==0) || !template.test(input.value))
    {
    alert(input.value.length==0);
alert(!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;
    }
}

/*
  Dodalem 3 parametr, bo jest wymagana zgodnosc w dÃÂ¯ÃÂ¿ÃÂ½ :(
*/
function setCountry(countryinput, stateinput, newversion)
{
	/*
  if (newversion == true)
  {
    if (stateinput.value != "")
      countryinput.value="US";
  }
  else 
  {
  */

	if (stateinput.value != "Outside US")
		countryinput.value="United States";

	if (countryinput.value!="United States")
		countryinput.value="US"
}

function setState(countryinput, stateinput, newversion)
{
  if (newversion == true)
  {
    if (countryinput.value != "US")
      stateinput.value = "";
  }
  else 
    if (countryinput.value != "United States")
      stateinput.value = "Outside US";
}
