﻿function ValidateEmail(field)
{
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

	if(field.value.match(re))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

function ValidateEmailAlert(field, message)
{
	if(ValidateEmail(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidatePrice(field)
{
   	var re = /^[0-9]+([.][0-9]+)*$/;

	if((field.value.match(re))&&(field.value>0))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

function ValidatePriceAlert(field, message)
{
	if(ValidatePrice(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidateMeasurement(formObj)
{
    var idx;
	var fields = "";
	var firstEmptyField;

	for(idx = 0; idx < formObj.elements.length; idx++)
	{
		if(formObj.elements[idx].getAttribute("numeric") == "true" && !(ValidatePrice(formObj.elements[idx])))
		{
			if(fields == "")
			{
				firstEmptyField = formObj.elements[idx];
			}
			else
			{
				fields += ", ";
			}
			fields += formObj.elements[idx].getAttribute("fieldName");
		 }
	}
	return(fields);
}

function ValidateMeasurementAlert(formObj, message)
{
	if(ValidateMeasurement(formObj) == "")
	{
		return(true);
	}

	alert(message + " " + ValidateMeasurement(formObj));
	formObj.elements[1].focus();	
	return(false);
}

function ValidateIntNumber(field)
{
	var re = /^[0-9]+$/;
    if(field.value.match(re))
	{
	    return(true);
	}
	else
	{
		return(false);
	}
}

function ValidateIntNumberAlert(field, message)
{
	if(ValidateIntNumber(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidateRequired(formObj)
{
    var idx;
	var fields = "";
	var firstEmptyField;

	for(idx = 0; idx < formObj.elements.length; idx++)
	{
		if(formObj.elements[idx].getAttribute("required") == "true" && formObj.elements[idx].value == "")
		{
			if(fields == "")
			{
				firstEmptyField = formObj.elements[idx];
			}
			else
			{
				fields += ", ";
			}
			fields += formObj.elements[idx].getAttribute("fieldName");
		}
	}
	return(fields);
}

function ValidateRequiredAlert(formObj, message)
{
	if(ValidateRequired(formObj) == "")
	{
		return(true);
	}

	alert(message + " " + ValidateRequired(formObj));
	return(false);
}

function ValidateImage(field)
{
	var re = /[.]jpeg$|[.]jpg$/i;
    if(field.match(re))
	{
	    return(true);
	}
	else
	{
		return(false);
	}
}

function ValidateImageAlert(field, message, noImageSelectedMessage)
{
    if(field=="")
    {
	    alert(noImageSelectedMessage);
	}
	else
	{
        if(ValidateImage(field))
	    {
		    return(true);
	    }
	    alert(message);
	}
	return(false);
}

function ValidateDateFormat(field)
{
    var date;
    var day;
    var month;
    var year;
    var firstSlash;
    var lastSlash;

	date = field.value;
	firstSlash = date.indexOf("/");
	lastSlash = date.lastIndexOf("/");
	/*day = date.substring(lastSlash + 1, date.length);
	month = date.substring(firstSlash + 1, lastSlash);
	year = date.substring(0, firstSlash);*/
	year = date.substring(lastSlash + 1, date.length);
	month = date.substring(firstSlash + 1, lastSlash);
	day = date.substring(0, firstSlash);

	if(isNaN(year) || isNaN(month) || isNaN(day))
	{
		return(false);
	}

	year = parseFloat(year);

	if(year < 1900)
	{
        return(false);
	}

	month = parseInt(month,10);

	if(month < 1 || month > 12)
	{
		return(false);
	}

	day = parseInt(day,10);

	if(day < 1 || day > 31)
	{
		return(false);
	}

	return(true);
}

function ValidateDateFormatAlert(field, message)
{
	if(ValidateDateFormat(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidatePhone(field)
{
   	var re = /^([0-9\s\-])+$/;

	if(field.value.match(re))
	{
		return(true);
	}
	return(false);	
}

function ValidatePhoneAlert(field, message)
{
	if(ValidatePhone(field))
	{
		return(true);
	}
	
	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidateMessageLength(field,value)
{
    if(field.value.length < value)
    {       
        return(true); 
    }
    return(false);
}	

function ValidateMessageLengthAlert(field,value,message)
{
    if(ValidateMessageLength(field,value))
    {
        return(true); 
    }
    alert(message);     
	return(false);
}

function ValidateTime(field)
{
   	var re =/^\d{1,2}:\d{1,2}\.\d{1,2}$/;
   	
	if(field.value.match(re))
	{
		var twoPointsPosition = field.value.indexOf(":");
		var pointPosition = field.value.indexOf(".");
		var min = field.value.substring(0,twoPointsPosition);
	    var seg = field.value.substring(twoPointsPosition+1,pointPosition);
	    var cent= field.value.substring(pointPosition+1,field.value.length);
    			
	    if (((min >=0) && (min <= 59)) && ((seg >= 0) && (seg <=59) && ((cent >= 0) && (cent <= 99))))
	    {
		    return true;
	    }
    }
	
	return(false);	
}

function ValidateTimeSeconds(field) {
    
    var re = /^\d{1,2}\.\d{1,2}$/;
    if (field.value.match(re)) {
        var pointPosition = field.value.indexOf(".");
        var seg = field.value.substring(0, pointPosition);
        var cent = field.value.substring(pointPosition + 1, field.value.length);   
        if(((seg>=0) && (seg<=59))&&((cent>=0)&&(cent<=99)))
        {
             return true;
        }
    }
    return (false);
}

function ValidateTimeMinutes(field) {
    
    var re = /^\d{1,2}:\d{1,2}$/;
    if (field.value.match(re)) {

        var twoPointsPosition = field.value.indexOf(":");
        var min = field.value.substring(0, twoPointsPosition);
        var seg = field.value.substring(twoPointsPosition + 1, field.value.length);
        if(((min>=0)&&(min<=59))&&((seg>=0)&&(seg<=59)))
        {
        return true;
        }      

    }
    return (false);

 
}

function ValidateTimeAlert(field, message) {
    
	if(ValidateTime(field))
	{
		return(true);
    }
    if (ValidateTimeMinutes(field)) {
        return (true);
    }
    if (ValidateTimeSeconds(field)) {
        return (true);
    }
    	
	    alert(message);
	    field.focus();
	    field.select();
	    return(false);
}	

function ValidateWebAddress(field)
{   
    var re = /^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)( [a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$/;
	
	if(field.value.match(re))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

function ValidateWebAddressAlert(field, message)
{
	if(ValidateWebAddress(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidateIntNumberRangeAlert(field, minValue, maxValue, message)
{
	if(ValidateIntNumber(field))
	{
		if(field.value >= minValue && field.value <= maxValue)
		{
		    return(true);   
		}		
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

//"\d\d:\d\d AM|\d\d:\d\d PM"
/*function ValidateHour(field)
{
	var re = /^\d\d:\d\d AM|\d\d:\d\d PM+$/;
    if(field.value.match(re))
	{
	    return(true);
	}
	else
	{
		return(false);
	}
}

function ValidateHourAlert(field, message)
{
	if(ValidateHour(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}*/

function CompareValidator(firstField, secondField)
{
	if(firstField.value == secondField.value)
	{
		return(true);
	}
	return(false);	
}

function CompareValidatorAlert(firstField, secondField, message)
{
	if(CompareValidator(firstField, secondField))
	{
		return(true);
	}
	
	alert(message);
	secondField.focus();
	secondField.select();
	return(false);
}

function ValidateBanners(field) {
    var re = /[.]swf$/i;
    if (field.match(re)) {
        return (true);
    }
    else {
        return (false);
    }
}

function ValidateBannersAlert(field, message, noBannerSelectedMessage) {
    if (field == "") {
        alert(noBannerSelectedMessage);
    }
    else {
        if (ValidateBanners(field)) {
            return (true);
        }
        alert(message);
    }
    return (false);
}


function ValidateDuration(field) {
    var re = /^[0-9]+([.][0-9]+)*$/;

    if ((field.value.match(re)) && (field.value > 0)) {
        return (true);
    }
    else {
        return (false);
    }
}

function ValidateDurationAlert(field, message) {
    if (ValidateDuration(field)) {
        return (true);
    }

    alert(message);
    field.focus();
    field.select();
    return (false);
}

