function stripCharsInBag(s, bag)
{
	var strResult = "";
	var i;

	if (!s || s.length == 0)
		return "";

	for (i = 0; i < s.length; i++)
	{
		if (bag.indexOf(s.charAt(i)) < 0)
		{
			strResult += s.charAt(i);
		}
	}

	return strResult;
}

function stripCharsNotInBag(s, bag)
{
	var strResult = "";
	var i;

	if (!s || s.length == 0)
		return "";

	for (i = 0; i < s.length; i++)
	{
		if (bag.indexOf(s.charAt(i)) >= 0)
		{
			strResult += s.charAt(i);
		}
	}

	return strResult;
}

function isNumber(obj)
{
	var s = stripCharsNotInBag(obj.value, "01234567899.");

	if (!s)
		return false;

	if (s.length == 0 || s.length != obj.value.length)
		return false;

	return true;
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if ((c < "0") || (c > "9"))
        	return false;
    }

    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++)
	{
		this[i] = 31;

		if (i==4 || i==6 || i==9 || i==11)
			this[i] = 30;

		if (i==2)
			this[i] = 29;
   }

   return this;
}

function isDate(dtStr)
{
	var dtCh = "/";
	var minYear = 1900;
	var maxYear = 2100;

	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh, pos1+1);
	var strMonth = dtStr.substring(0, pos1);
	var strDay = dtStr.substring(pos1+1, pos2);
	var strYear = dtStr.substring(pos2+1);
	strYr = strYear;

	if (strDay.charAt(0) == "0" && strDay.length>1)
		strDay = strDay.substring(1);

	if (strMonth.charAt(0) == "0" && strMonth.length>1)
		strMonth = strMonth.substring(1);

	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0) == "0" && strYr.length>1)
			strYr = strYr.substring(1);
	}
	month = parseInt(strMonth);
	day = parseInt(strDay);
	year = parseInt(strYr);
	if (pos1 == -1 || pos2 == -1){
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length < 1 || month < 1 || month > 12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh, pos2 + 1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
	return true;
}

function validateStautsUpdate()
{
	var i;

	for (i = 0; i < document.forms[0].elements.length; i++)
	{
		switch (stripCharsInBag(document.forms[0].elements[i].name, "1234567890"))
		{
			case "date":
				if (!isDate(document.forms[0].elements[i].value))
				{
					document.forms[0].elements[i].focus();
					return false;
				}

				break;

			case "explanation":
				if (document.forms[0].elements[i].value == "")
				{
					alert("Please enter an explanation.");
					document.forms[0].elements[i].focus();
					return false;
				}

				break;

			/*
			case "reference":
				if (document.forms[0].elements[i].value == "")
				{
					alert("Please enter an reference.");
					document.forms[0].elements[i].focus();
					return false;
				}

				break;
			*/
		}
	}

	if (document.forms[0].Caption)
	{
		if (document.forms[0].Caption.length)
		{
			for (i = 0; i < document.forms[0].Caption.length; i++)
			{
				if (document.forms[0].PhotoInput.item(i).value != "")
				{
					if (document.forms[0].Caption.item(i).value == "")
					{
						alert("Please enter a caption for photo #" + (i + 1) + ".");
						document.forms[0].Caption.item(i).focus();
						return false;
					}
				}
			}
		}

		else if (document.forms[0].PhotoInput.value != "")
		{
			if (document.forms[0].Caption.value == "")
			{
				alert("Please enter a caption for the photo.");
				document.forms[0].Caption.focus();
				return false;
			}
		}
	}

	return true;
}

function validateStautsInsert()
{
	var i;

	if (!isNumber(document.forms[0].Height))
	{
		alert("Please enter a number for the height.");
		document.forms[0].Height.focus();
		return false;
	}

	if (document.forms[0].ReportGrp.selectedIndex == 0)
	{
		alert("Please select a report group.");
		document.forms[0].Height.focus();
		return false;
	}

	if (document.forms[0].Description.value == "")
	{
		alert("Please enter a description.");
		document.forms[0].Description.focus();
		return false;
	}

	if (document.forms[0].Caption)
	{
		if (document.forms[0].Caption.length)
		{
			for (i = 0; i < document.forms[0].Caption.length; i++)
			{
				if (document.forms[0].PhotoInput.item(i).value != "")
				{
					if (document.forms[0].Caption.item(i).value == "")
					{
						alert("Please enter a caption for photo #" + (i + 1) + ".");
						document.forms[0].Caption.item(i).focus();
						return false;
					}

					/*
					else if (!isNumber(document.forms[0].PrintNumber.item(i)))
					{
						alert("Please enter a number for the print number of photo #" + (i + 1) + ".");
						document.forms[0].PrintNumber.item(i).focus();
						return false;
					}
					*/
				}
			}
		}

		else if (document.forms[0].PhotoInput.value != "")
		{
			if (document.forms[0].Caption.value == "")
			{
				alert("Please enter a caption for the photo.");
				document.forms[0].Caption.focus();
				return false;
			}

			/*
			else if (!isNumber(document.forms[0].PrintNumber))
			{
				alert("Please enter a number for the print number for the photo.");
				document.forms[0].PrintNumber.focus();
				return false;
			}
			*/
		}
	}

	return true;
}

function addPhoto()
{
	var id = "";

	if (arguments.length > 0)
	{
		id = arguments[0];

		if (!this.photoCount)
		{
			this.photoCount = new Array;
			//alert("a");
		}

		if (!this.photoCount[id])
		{
			this.photoCount[id] = 1;
			//alert("b");
		}
		else
		{
			this.photoCount[id]++;
			//alert("c");
		}
	}

	else if (!this.photoCount)
		this.photoCount = 1;

	else
		this.photoCount++;

	var d = document.getElementById("photos" + id);
	var p = document.createElement("p");

	p.setAttribute("style", "margin-top: 0px;");

	var b1 = document.createElement("br");
	var i1 = document.createElement("input");
	var b2 = document.createElement("br");
	var i2 = document.createElement("input");
	var b3 = document.createElement("br");
	var i3 = document.createElement("input");
	var b4 = document.createElement("br");
	var i4 = document.createElement("input");

	var t;

	i1.setAttribute("type", "file");
	i1.setAttribute("name", "photo" + id + "[]");
	i1.setAttribute("size", "40");
	i1.setAttribute("id", "PhotoInput");

	p.appendChild(i1);
	p.appendChild(b1);

	i2.setAttribute("type", "text");
	i2.setAttribute("name", "Caption" + id + "[]");
	i2.setAttribute("size", "20");
	i2.setAttribute("id", "Caption");

	t = document.createTextNode(" Caption *");

	p.appendChild(i2);
	p.appendChild(t);
	p.appendChild(b2);

	/*
	i3.setAttribute("type", "text");
	i3.setAttribute("name", "PrintNumber[]");
	i3.setAttribute("size", "20");
	i3.setAttribute("id", "PrintNumber");

	t = document.createTextNode(" Print Number");

	p.appendChild(i3);
	p.appendChild(t);
	p.appendChild(b3);
	*/

	i4.setAttribute("type", "text");
	i4.setAttribute("name", "OrderInLists" + id + "[]");
	i4.setAttribute("size", "20");
	i4.setAttribute("id", "OrderInLists");

	if (id != "")
		i4.setAttribute("value", this.photoCount[id]);
	else
		i4.setAttribute("value", this.photoCount);

	t = document.createTextNode(" Order In List");

	p.appendChild(i4);
	p.appendChild(t);
	p.appendChild(b4);

	d.appendChild(p);
}

function addInventoryPhoto()
{
	if (!this.photoCount)
		this.photoCount = 1;
	else
		this.photoCount++;

	var d = document.getElementById("photos");
	var p = document.createElement("p");

	var b1 = document.createElement("br");
	var i1 = document.createElement("input");
	var b2 = document.createElement("br");
	var i2 = document.createElement("input");
	var b3 = document.createElement("br");
	var i3 = document.createElement("input");
	var b4 = document.createElement("br");
	var i4 = document.createElement("input");
	var b5 = document.createElement("br");
	var i5 = document.createElement("input");
	var b6 = document.createElement("br");
	var i6 = document.createElement("input");

	var t;

	i1.setAttribute("type", "file");
	i1.setAttribute("name", "photo[]");
	i1.setAttribute("size", "40");
	i1.setAttribute("id", "PhotoInput");

	p.appendChild(i1);
	p.appendChild(b1);

	i2.setAttribute("type", "text");
	i2.setAttribute("name", "Caption[]");
	i2.setAttribute("size", "20");
	i2.setAttribute("id", "Caption");

	t = document.createTextNode(" Caption *");

	p.appendChild(i2);
	p.appendChild(t);
	p.appendChild(b2);

	/*
	i3.setAttribute("type", "text");
	i3.setAttribute("name", "PrintNumber[]");
	i3.setAttribute("size", "20");
	i3.setAttribute("id", "PrintNumber");

	t = document.createTextNode(" Print Number");

	p.appendChild(i3);
	p.appendChild(t);
	p.appendChild(b3);
	*/

	i4.setAttribute("type", "text");
	i4.setAttribute("name", "OrderInLists[]");
	i4.setAttribute("size", "20");
	i4.setAttribute("id", "OrderInLists");
	i4.setAttribute("value", this.photoCount);

	t = document.createTextNode(" Order In List *");

	p.appendChild(i4);
	p.appendChild(t);
	p.appendChild(b4);

	i5.setAttribute("type", "text");
	i5.setAttribute("name", "Height[]");
	i5.setAttribute("size", "20");
	i5.setAttribute("id", "Height");

	t = document.createTextNode(" Height *");

	p.appendChild(i5);
	p.appendChild(t);
	p.appendChild(b5);

	i6.setAttribute("type", "text");
	i6.setAttribute("name", "RequiredID[]");
	i6.setAttribute("size", "20");
	i6.setAttribute("id", "RequiredID");

	t = document.createTextNode(" Required ID *");

	p.appendChild(i6);
	p.appendChild(t);
	p.appendChild(b6);

	d.appendChild(p);
}

function radioChanged()
{
	var frm = document.forms[0];
	var i;
	var j;

	for (i = 0; i < frm.elements.length; i += 5)
	{
		if (frm.elements[i].type == "radio")
		{
			for (j = 0; j < 5; j++)
			{
				if (frm.elements[i + j].checked ^ frm.elements[i + j].defaultChecked)
					return true;
			}
		}
	}

	alert("Please select a status to update.");
	return false;
}
