// $Id: $
// 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 checkInputID(inputId, errorMessage) {
	var input = document.getElementById(inputId);
	if(!input) {
		alert( "Input " + inputId + " not found!");
		return false;
	}
	if( input.value == "" ) return focusFailedInputID(inputId,errorMessage);
	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 focusFailedInputID(inputId, errorMessage) {
	var labels = document.getElementsByTagName("label");

	var tmplabel;
	// set error class to correct label and remove error class from others
	for(var i = 0; i < labels.length; i++ ) {
		var label = labels[i];
		label.className = label.className.replace("error", "");
		// if anything will be wrong, remove break statement
		if( label.htmlFor == inputId ) { label.className += " error"; break; }
	}
	
	if(errorMessage) alert(errorMessage);
		
	var element = document.getElementById(inputId);
	if(element) {
		element.onkeypress = clearError;
		element.focus();
	}
	return false;
}

function clearError() {
	var labels = document.getElementsByTagName("label");
	for(var i = 0; i < labels.length; i++ ) {
		var label = labels[i];
		label.className = label.className.replace("error", "");
	}
	return true;
}

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 checkEmailID(inputId,errorMessage) {
	var input = document.getElementById(inputId);
	if(!input) {
		alert( "Input " + inputId + " not found!");
		return false;
	}
	if (!isValidEmail(input.value)) {
		focusFailedInputID(inputId,errorMessage);
		return false;
	} return true;
}

function isValidEmail(email) {
 	var 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(inputId,errorMessage) {
	var input = document.getElementById(inputId);
	template=/^[0-9\.]*$/i;
	if((input.value.length==0) || !template.test(input.value)) {
		return focusFailedInput(input,errorMessage);
	}
	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";
}

function compareFields(fieldId1, fieldId2, errorMessage)
{
	var field1 = document.getElementById(fieldId1);
	if(!field1)
	{
		alert( "Element " + fieldId1 + " not found!");		
		return false;
	}

	var field2 = document.getElementById(fieldId2);
	if(!field2)
	{
		alert( "Element " + fieldId2 + " not found!");		
		return false;
	}

	if( field1.value != field2.value )
    	return focusFailedInputID(fieldId2,errorMessage);
     
    return true;
}

function checkRadio(form, input, errorMessage, inputId)
{
	var tmpForm = document.getElementById(form);
	if(tmpForm)
		form = tmpForm;
	if(!form[input])
		alert( "Element " + input + " not found!");		
	var i = 0;
	for( i ; i < form[input].length; i++ )
	{
		if(form[input][i].checked == true) break;
	}
	
	var tmp = inputId ? inputId : tmp;
	
	if( i == form[input].length )
	{
		alert(errorMessage);
    	return false;    	
    }
    return true;
}


