// $Id: forms.js,v 1.31 2008-09-03 13:50:20 tkepski Exp $
// Janmedia Interactive 

FAILED_INPUT_BACKGROUND_COLOR = "#FEDDBC";

function checkInput(input,message)
{
	if (typeof(input) == 'string') input = document.getElementById(input);

	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 (typeof(input) == 'string') input = document.getElementById(input);

	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)
{
	if (!input)
	{
		alert('focusFailedInput(input, message) - input parameter is null!');
		return;
	}
    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 (typeof(input) == 'string') input = document.getElementById(input);

    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 (stateinput.value != "Outside US")
		countryinput.value="United States";
	else return;

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

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

function compareFields(input1, input2, errorMessage)
{
	if (typeof(input1) == 'string')
		input1 = document.getElementById(input1);
		
	if(!input1)
	{
		alert( "Element not found!");		
		return false;
	}

	if (typeof(input2) == 'string')
		input2 = document.getElementById(input2);

	if(!input2)
	{
		alert( "Element not found!");		
		return false;
	}

	if( input1.value != input2.value )
    	return focusFailedInput(input2,errorMessage);
     
    return true;
}

function clearForm(formId) {
	var f = document.getElementById(formId);
	if (!f) {
		f = document.forms[formId] ;
	}
	if (f) {
		var elems = f.elements ;
		for (i = 0; i < elems.length; i++) {
			if (elems[i].type != 'button' && elems[i].type != 'submit') {
				elems[i].value = '' ;
				if (elems[i].type == 'select-one') {
					elems[i].selectedIndex = 0; 
				}
				if (elems[i].type == 'checkbox' || elems[i].type == 'radio') {
					elems[i].checked = false ;
				}
			}
		}
	}
}

