﻿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 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);
}

function ValidatePriceAlert(field, message)
{
	if(ValidatePrice(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidateOrderAmount(field)
{
   	var re = /^[0-9]+([.][0-9]+)*$/;

	if((field.value.match(re))||(field.value == 0))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

function ValidateOrderAmountAlert(field, message)
{
	if(ValidateOrderAmount(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 ValidateMaximumOrder(field)
{
	var re = /^[0-9]+$/;
    if(field.value.match(re)&&(field.value>0))
	{
	    return(true);
	}
	else
	{
		return(false);
	}
}

function ValidateMaximumOrderAlert(field, message)
{
	if(ValidateMaximumOrder(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));
	//formObj.elements[1].focus();	
	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);

	if(isNaN(year) || isNaN(month) || isNaN(day))
	{
		return(false);
	}

	year = parseFloat(year);

	if(year < 2000 || year > 3000)
	{
        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);
}
