/* $Id: forms.js,v 1.6 2008/06/11 18:36:50 pkulik Exp $ */

/* ----------------------------------------------------------------------
	DEFAULT VALIDATION FORMS
---------------------------------------------------------------------- */
function checkInput(inputId, errorMessage)
{
	var input = document.getElementById(inputId);
	if(!input)
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}

	if(input.type=="checkbox")
	{
		if(input.value == "" || !input.checked)
			return focusFailedInput(inputId,errorMessage);	
    	return true;
	}

	if( input.value == "" )
    	return focusFailedInput(inputId,errorMessage);

    return true;
}

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 focusFailedInput(fieldId2,errorMessage);
     
    return true;
}

function checkTextarea(inputId, errorMessage)
{
	var input = document.getElementById(inputId);
	if(!input)
	{
		alert( "Textarea " + inputId + " not found!");		
		return false;
	}

	if( input.value.length <= 3 )
    	return focusFailedInput(inputId,errorMessage);
     
    return true;
}

function checkEmail(inputId,errorMessage)
{
	var input = document.getElementById(inputId);
	if(!input)
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}

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

    if (!isValidDate(input.value)) 
    {
        focusFailedInput(inputId,errorMessage);
        return false;
    }

    return true;
}

function isValidDate(dateStr)
{
	// Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) 
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables

	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

	// MM/DD/YYYY MM-DD-YYYY
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null)
		return false;

	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12)
		return false;

	if (day < 1 || day > 31)
		return false;

	if ((month==4 || month==6 || month==9 || month==11) && day==31)
		return false

	if (month == 2)
	{
		// check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap))
			return false;
	}

	return true;
}

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 focusFailedInput(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.onchange = clearError;
		element.focus();
	}

	return false;
}

function checkRadio(form, input, errorMessage, inputId)
{
	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 )
    	return focusFailedInput(tmp,errorMessage);
     
    return true;
}

