﻿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 ValidateEmail(field)
{
    var re = /^[a-z0-9_-]+(?:\.[a-z0-9_-]+)*@[a-z0-9_-]+(?:\.[a-z0-9_-]+)*\.[a-z]{2,6}$/i
    if(field.value != "")
    {
        if (field.value.match(re))
	    {
		    return(true);
	    }
	    else
	    {
		return(false);
	    }
	}
	else
	{
		return(true);
	}
}

function ValidateEmailAlert(field, message)
{
	if(ValidateEmail(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}


function ValidateUrl(field)
{
    var re = /^(http:|https:|ftp:)\/\/\w+(\.\w+)*(\-\w+)?\.\w{2,3}(\:\d{2,6})?(\/{1,2}(\:|\-|\w|\.|\?|\/|\=|\&|\%|\@|\\|\,)*)?$/;
    if(field.value != "")
    {
        if (field.value.match(re))
	    {
		    return(true);
	    }
	    else
	    {
		    return(false);
	    }
	}
	else
	{
		return(true);
	}
}

function ValidateUrlAlert(field, message)
{
    if (ValidateUrl(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidatePasswordAlert(field, message)
{
    if (ValidatePassword(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	return(false);
}

function ValidatePassword(field)
{
    var re = /^[a-zA-Z0-9\.\_\-\/]+$/;
    if (field.value.length < 30 && field.value.match(re))
    {
	   return(true);
    }
	return (false);
}

function ValidateIsNumber(field)
{
	var re = /^[0-9]+$/;
    if(field.value.match(re) || field.value == "")
	{
	    return(true);
	}
	else
	{
		return(false);
	}
}

function ValidateIsNumberAlert(field, message)
{
	if(ValidateIsNumber(field))
	{
		return(true);
	}

	alert(message);
	field.focus();
	field.select();
	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 ValidateImageAlert1(field, message)
{
    if (ValidateImage(field) || field == "")
    {
        return (true);
    }
    alert(message);
    return (false);
}




function ValidateUrlTextAlert(field, field1, message, message1)
{
    if ((field.value != "" && field1.value != "") || (field.value == "" && field1.value == ""))
    {
        return (true);
    }
    else
    {
        if(field.value != "" && field1.value == "")
        {
            alert(message);
            return (false);
        }
    }
    alert(message1);
    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 ValidateDescriptionLength(field, value)
{
    if (field.value.length < value) {
        return (true);
    }
    return (false);
}

function ValidateDescriptionLengthAlert(field, value, message)
{
    if (ValidateDescriptionLength(field, value)) {
        return (true);
    }
    alert(message);
    return (false);
}

function ValidateIsSmallInt(field) {
    var re = /^[0-9]+$/;
    if ((field.value >= 0) && (field.value <= 32767) && (field.value.match(re)) || (field.value == "")) {
        return true;
    }
    else {
        return false;
    }
}

function ValidateIsSmallIntAlert(field, message)
{
    if (ValidateIsSmallInt(field)) {
        return (true);
    }

    alert(message);
    field.focus();
    field.select();
    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 ValidateYears(field) {
    var re = /^[0-9]+$/;
    if ((field.value.length == 4) && (field.value >= 1000) && (field.value <= 3000) && (field.value.match(re)))
    {
        return true;
    }
    if (field.value == "")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function ValidateYearsAlert(field, message)
{
    if (ValidateYears(field))
    {
        return (true);
    }

    alert(message);
    field.focus();
    field.select();
    return (false);
}

function ValidateMonth(field) {
    var re = /^[0-9]+$/;
    if ((field.value >= 1) && (field.value <= 12) && (field.value.match(re)) || (field.value == "")) {
        return true;
    }
    else {
        return false;
    }
}

function ValidateMonthAlert(field, message) {
    if (ValidateMonth(field)) {
        return (true);
    }

    alert(message);
    field.focus();
    field.select();
    return (false);
}

function ValidateDay(field) {
    var re = /^[0-9]+$/;
    if ((field.value >= 1) && (field.value <= 31) && (field.value.match(re)) || (field.value == "")) {
        return true;
    }
    else {
        return false;
    }
}

function ValidateDayAlert(field, message) {
    if (ValidateDay(field)) {
        return (true);
    }

    alert(message);
    field.focus();
    field.select();
    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("/");
    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 < 500 || 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 ValidateYearAndMonth(field) {
    var date;
    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)) {
        return (false);
    }

    year = parseFloat(year);

    if (year < 500 || year > 3000) {
        return (false);
    }

    month = parseInt(month, 10);

    if (month < 1 || month > 12) {
        return (false);
    }

    return (true);
}

function ValidateYearAndMonthAlert(field, message) {
    if (ValidateYearAndMonth(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 setHeightAndWidthOfPicture(standarWidth,standarHeight,heightFloat,widthFloat)
{
    var percentage = 0;
    /*string path = "";
    path = "Picture" + pictureId + ".jpg";
    string completePath = Server.MapPath("Pictures").ToString() + "\\" + path;
    Image i = Image.FromFile(completePath);
    SizeF sz = i.PhysicalDimension;
    heightFloat = sz.Height;
    widthFloat = sz.Width;*/
    if (heightFloat <= standarHeight && widthFloat <= standarWidth)
    {
        height = heightFloat;
        width = widthFloat;
    }
    else if (widthFloat > standarWidth && heightFloat > standarHeight)
    {
        if (widthFloat > heightFloat)
        {
            percentage = standarWidth / widthFloat;
            heightFloat = percentage * heightFloat;
            height = heightFloat;
            width = standarWidth;
        }
        else
        {
            percentage = standarHeight / heightFloat;
            widthFloat = percentage * widthFloat;
            width = widthFloat;
            height = standarHeight;
        }
    }
    else if (widthFloat > standarWidth)
    {
        percentage = standarWidth / widthFloat;
        heightFloat = percentage * heightFloat;
        height = heightFloat;
        width = standarWidth;
    }
    else if (heightFloat > standarHeight)
    {
        percentage = standarHeight / heightFloat;
        widthFloat = percentage * widthFloat;
        width = widthFloat;
        height = standarHeight;
    }
}
function GetHeightOfPicture(standarWidth,standarHeight,heightFloat,widthFloat)
{
    this.setHeightAndWidthOfPicture(standarWidth,standarHeight,heightFloat,widthFloat);
    return height;
}
function GetWidthOfPicture(standarWidth,standarHeight,heightFloat,widthFloat)
{
    this.setHeightAndWidthOfPicture(standarWidth,standarHeight,heightFloat,widthFloat);
    return width;
}
