var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31) ;

//**************************************************************************
function getDays(month, year)
{
	if (month == 1)
		return ((year % 4 == 0) && (0 != (year % 100))) || (year % 400 == 0) ? 29 : 28 ;
	else
		return daysInMonth[month] ;
}
//**************************************************************************************************
function checkDate(dateVar)
{
if (dateVar.indexOf('/')==2 && dateVar.lastIndexOf('/')==5 &&  dateVar.length==10)
{
	for(var index=0;index<dateVar.length;index++)
	{
		if(dateVar.charAt(index)!="/")
		{
			var temp = parseInt(dateVar.charAt(index));
		    if (isNaN(temp))
			{
				window.alert("Date must include only 'numbers' and '/' for separation.  (Date format is: dd/mm/yyyy)")
		        return false;
            }
	   	}
	}
	var day = parseInt(dateVar.substring(0,2),10);
	var month = parseInt(dateVar.substring(3,5),10);
	var year = parseInt(dateVar.substring(6,dateVar.length),10) ;
	var today = new Date() ;
	if (year < (today.getYear()))
	{
		alert("The year must be greater then present year.\nPlease check the year.") ;
		return false ;
	}
	if (month > 12 && month < 1)
	{
		alert("You have entered an illegal month.\nPlease check the month.") ;
		return false ;
	}
	if ((month < (today.getMonth()+1)) && (year == today.getYear()))
	{
		alert("You can't enter a month that is smaller then present month.\nPlease check the month.") ;
		return false ;
	}
	if ((day > getDays(month-1,year)) || (day < 1))
	{
		alert("You have entered an illegal day.\nPlease check the day.") ;
		return false ;
	}
	if ((day < today.getDate()) && (month == (today.getMonth()+1)) && (year == today.getYear()))
	{
		alert("You can't enter a day that is smaller then present day.\nPlease check the day.") ;
		return false ;
	}
	return true;	
}
else 
{
	alert("Date format is: dd/mm/yyyy");
	return false;
}
}
//*********************************************************************************************
function isEmpty(str)
{//this function checks if the string is empty
	for (var intloop = 0; intloop < str.length; intloop++)
		if (str.charAt(intloop) !=  " ")
			return false;
		return true ;
}
//*********************************************************************************************
function checkMail(str)
{//check if email address is valid
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; //valid
	if (!reg1.test(str) && reg2.test(str))
		return true ;
	return false ;
}
//*********************************************************************************************
function valForm(what)
{
	if (isEmpty(document.all.userOrder.userName.value))
	{
		alert("Please enter the user name.");
		document.all.userOrder.userName.focus();
		return false ;
	}
	if (isEmpty(document.all.userOrder.address.value))
	{
		alert("Please enter the address.");
		document.all.userOrder.address.focus();
		return false ;
	}
	if (isEmpty(document.all.userOrder.phone.value))
	{
		alert("Please enter the phone number.");
		document.all.userOrder.phone.focus();
		return false ;
	}
	if (isEmpty(document.all.userOrder.phone.value))
	{
		alert("Please enter the phone number.");
		document.all.userOrder.phone.focus();
		return false ;
	}
	if (parseInt(what,10) == 0)
	{
		if (!checkMail(document.all.userOrder.email.value))
		{
			alert("Please enter a valid email.");
			document.all.userOrder.email.focus();
			return false ;
		}
		else
			if (!checkDate(document.all.userOrder.returnDate.value))
			{
				document.all.userOrder.returnDate.focus();
				return false ;
			}
		}
//		var sendform = false ;
//		alert(document.forms[0].elements);
//		for (var i=8; i < document.forms[0].elements.length-1;i++)
//		{	
//			
//			if (!isEmpty(document.forms[0].elements[i].value))
//			{
//				sendform = true ;
//				for (var j = 0; j < document.forms[0].elements[i].value.length;j++)
//					if (isNaN(parseInt(document.forms[0].elements[i].value.charAt(j),10)))
//					{
//						alert("Please enter only numbers.");
//						document.forms[0].elements[i].focus();
//						return false;
//					}
//			}
//		}
//		if (!sendform)
//		{
//			alert("You must enter quantity for some product.") ;
//			return false ;
//		}
	return true ;
}
//***********************************************************************************************
function validPicture(str)
{//this function checks the valid picture file extension (only .gif, .png, .jpg)
	var validExpr = /((\.jpg)|(\.gif)|(\.png))$/ ;
	if (validExpr.test(str))
		return true ;
	return false ;
}
//***********************************************************************************************
//*************************************************************************************
function valOutOrderForm(whatForm)
{
	//alert("oper= " + whatForm);
	if (parseInt(whatForm,10) == 1)
	{
		return true ;
	}
	if (parseInt(whatForm,10) == 2)
	{
		var clicked
		clicked = false;
		var value ;
		if (document.all.orderDet.prodOut.length > 1)
		{
			for (var i = 0; i < document.all.orderDet.prodOut.length; i++)
				if (document.all.orderDet.prodOut[i].checked)
				{
					clicked = true;
					value = document.all["quantFor"+document.all.orderDet.prodOut[i].value].value ;
					if (isEmpty(value))
					{
						alert("Please enter the quantity.");
						document.all["quantFor"+document.all.orderDet.prodOut[i].value].focus();
						return false ;
					}
					else
					{
						for (var j = 0; j < value.length;j++)
							if (isNaN(parseInt(value.charAt(j),10)))
							{
								alert("Please enter only numbers.");
								document.all["quantFor"+document.all.orderDet.prodOut[i].value].focus();
								return false;
							}
					}
				}
		}
		else
			if (document.all.orderDet.prodOut.checked)
			{
				clicked = true;
				value = document.all["quantFor"+document.all.orderDet.prodOut.value].value ;
				if (isEmpty(value))
				{
					alert("Please enter the quantity.");
					document.all["quantFor"+document.all.orderDet.prodOut.value].focus();
					return false ;
				}
				else
				{
					for (var j = 0; j < value.length;j++)
						if (isNaN(parseInt(value.charAt(j),10)))
						{
							alert("Please enter only numbers.");
							document.all["quantFor"+document.all.orderDet.prodOut.value].focus();
							return false;
						}
				}
			}
		if (clicked)
		{
			return true ;
		}
		alert("To order a product, Please click on the check box.");
		return false;
	}
	if ((parseInt(whatForm,10) == 4) || (parseInt(whatForm,10) == 5))
	{
		var clicked
		clicked = false;
		var value ;
		if (document.all.orderDet.chk.length > 1)
		{
			for (var i = 0; i < document.all.orderDet.chk.length; i++)
				if (document.all.orderDet.chk[i].checked)
				{
					clicked = true;
					value = document.all["retQtyFor"+document.all.orderDet.chk[i].value].value ;
					if (isEmpty(value))
					{
						alert("Please enter the quantity.");
						document.all["retQtyFor"+document.all.orderDet.chk[i].value].focus();
						return false ;
					}
					else
					{
						for (var j = 0; j < value.length;j++)
							if (isNaN(parseInt(value.charAt(j),10)))
							{
								alert("Please enter only numbers.");
								document.all["retQtyFor"+document.all.orderDet.chk[i].value].focus();
								return false;
							}
					}
				}
		}
		else
			if (document.all.orderDet.chk.checked)
			{
				clicked = true;
				value = document.all["retQtyFor"+document.all.orderDet.chk.value].value ;
				if (isEmpty(value))
				{
					alert("Please enter the quantity.");
					document.all["retQtyFor"+document.all.orderDet.chk.value].focus();
					return false ;
				}
				else
				{
					for (var j = 0; j < value.length;j++)
						if (isNaN(parseInt(value.charAt(j),10)))
						{
							alert("Please enter only numbers.");
							document.all["retQtyFor"+document.all.orderDet.chk.value].focus();
							return false;
						}
				}
			}
		if (clicked)
		{
			return true ;
		}
		alert("To order a product, Please click on the check box.");
		return false;
	}
}
//****************************************************************************************************
function openWind(orderId)
{
	window.open('print.asp?orderId='+orderId+'',null,'toolbar=no ,  status=no, scrollbars = yes, width=680 height=680 top=20 left=80');
}
//*****************************************************************************************************
function showMainCategory()
{
	var str, mainCat, subCat ;
	mainCat = 0 ;
	subCat = 0 ;
	mainCat = document.getElementById("mainCategorySelect").value ;
	subCat = 0;
	//alert('before mainCat=' +mainCat+ 'subCat=' +subCat);
	//changeSubASelect(mainCat,0);
	str = "productMaint.asp?mainCat=" + mainCat +"&subCat="+subCat;
	//str = "productMaint.asp?mainCat=" + mainCat +"&subCat="+0;
	//alert(str);
	document.location.href = str ;
}
//*****************************************************************************************************
function showCategory()
{
	var str, mainCat, subCat ;
	mainCat = 0 ;
	subCat = 0 ;
	mainCat = document.getElementById("mainCategorySelect").value ;
	subCat = document.getElementById("subCatASelect").value ;
	str = "productMaint.asp?mainCat=" + mainCat +"&subCat="+subCat;
	document.location.href = str ;
}
//*****************************************************************************************************
function showMainCategoryCContent()
{
	var str, mainCat, subCat ;
	mainCat = 0 ;
	subCat = 0 ;
	mainCat = document.getElementById("mainCategorySelect").value ;
	subCat = 0;
	//alert('before mainCat=' +mainCat+ 'subCat=' +subCat);
	//changeSubASelect(mainCat,0);
	str = "categoriesContent.asp?mainCat=" + mainCat +"&subCat="+subCat;
	//str = "productMaint.asp?mainCat=" + mainCat +"&subCat="+0;
	//alert(str);
	document.location.href = str ;
}
//*****************************************************************************************************
function showCategoryCContent()
{
	var str, mainCat, subCat ;
	mainCat = 0 ;
	subCat = 0 ;
	mainCat = document.getElementById("mainCategorySelect").value ;
	subCat = document.getElementById("subCatASelect").value ;
	str = "categoriesContent.asp?mainCat=" + mainCat +"&subCat="+subCat;
	document.location.href = str ;
}
//*****************************************************************************************************
function checkCategoriesContent()
{
	if (document.all.subCatASelect.length > 1 && document.all.subCatASelect.selectedIndex == 0)
		{
			alert("The Sub Category must be required");
			return false;
		}
	return true;
}
//*****************************************************************************************************
function showCategoryInEditProduct(productId)
{
	var str, mainCat, subCat ;
	mainCat = 0 ;
	subCat = 0 ;
	mainCat = document.getElementById("mainCategorySelect").value ;
	subCat = document.getElementById("subCatASelect").value ;
	document.getElementById("mainCat").value = mainCat;
	document.getElementById("subCat").value = subCat;
	//alert("mainCat = " +mainCat);
	//alert("subCat = " +subCat);

	changeSubASelect(mainCat,subCat);
	//alert("ani po1");
	//str = "editProduct.asp?action=edit&product=" +productId+ "&mainCat=" +mainCat+ "&subCat=" +subCat;
	//action=edit&product=2&mainCat=1&subCat=12
	//document.location.href = str ;
}
//*******************************************************************************************************
function changeSubASelect(mainCat, selValue)
{
	if (mainCat < 0 )
		alert("Wrong selecttion.") ;
	else
	{
		for (;document.all.subCatASelect.length>0;)
			document.all.subCatASelect.remove(0);
		var newElement ;
		var index = 0 ;
		newElement = document.createElement("OPTION") ;
		newElement.text = "None" ;
		newElement.value = 0 ;
		if (parseInt(selValue,10) == 0) newElement.selected = true ;
		document.all.subCatASelect.options.add(newElement) ;
		for( index=0; index < subCategoryAData.length; index++)
			if (parseInt(subCategoryAData[index][1]) == mainCat)
			{
				newElement = document.createElement("OPTION") ;
				newElement.text = subCategoryAData[index][2] ;
				newElement.value = parseInt(subCategoryAData[index][0])  ;
				if (parseInt(selValue,10) == parseInt(subCategoryAData[index][0]))
					newElement.selected = true ;
				document.all.subCatASelect.options.add(newElement) ;
			}
	}
	//alert("mainCat = " +mainCat);
	//alert("subCat = " +subCat);
}
//*****************************************************************************************************
function valAddProduct()
{
	if (isEmpty(document.all.prodFrm.product.value))
	{
		alert("Please enter a product.");
		document.all.prodFrm.product.focus() ;
		return false;
	}
	return true;
}
//******************************************************************************************************
function validateEditProduct(form)
{
	//alert(form.productName.value);
	if (!form.productName.value)
	{
		alert ("Please insert product name.");
		form.productName.focus();
		return false;
	}
	if (!form.productPrice.value)
	{
		alert ("Please insert product price.");
		form.productPrice.focus();
		return false;
	}
	else if(isNaN(parseFloat(form.productPrice.value)))
	{
		alert("Please insert just numbers.");
		form.productPrice.focus();
		return false;
	}
	if (form.productSpecialPrice.value && isNaN(parseFloat(form.productSpecialPrice.value)))
	{
		alert("Please insert just numbers in special price.");
		form.productSpecialPrice.focus();
		return false;
	}
	if(form.action.value=="add")
	{
	 if (!form.productSmallPicture.value)
		{
			alert("The fileld Thumbnail  Image is required!");
			return false ;
		}
		else
			if (!validPicture(form.productSmallPicture.value))
			{
				alert("The file you've entered in the Thumbnail Image field isn't an image file.") ;
				return false ;
			}
		if (!form.productPicture.value)
		{
			alert("The fileld Large Image is required!");
			return false ;
		}
		else
			if (!validPicture(form.productPicture.value))
			{
				alert("The file you've entered in the Large Image field isn't an image file.") ;
				return false ;
			}
	}
	return true;
}
//******************************************************************************************************
function fillDate()
{
   var d, s = "";
   d = new Date();
   s = d.getDate() + "/";
   s += (d.getMonth() + 1) + "/";
   s += d.getYear();
   document.all.userOrder.Ddate.value = s ;
}
//****************************************************************************************************
function checkLoginFields(form)
{			
	if (isEmpty(form.user.value) || isEmpty(form1.pas.value))
	{
		alert("You must fill both of the fields.") ;
		return false ;
	}
	return true ;
}
//*******************************************************************************************************
function buildSelects(selValue)
{
	var newElement ;
	var index = 0 ;
	newElement = document.createElement("OPTION") ;
	newElement.text = "None" ;
	newElement.value = 0 ;
	if (parseInt(selValue,10) == 0)
		newElement.selected = true ;
	document.getElementById("mainCategorySelect").options.add(newElement) ;
	for( index=0; index < mainCategoryData.length; index++)
	{
		newElement = document.createElement("OPTION") ;
		newElement.text = mainCategoryData[index][1] ;
		newElement.value = parseInt(mainCategoryData[index][0])  ;
		if (parseInt(selValue,10) == parseInt(mainCategoryData[index][0]))
			newElement.selected = true ;
		document.getElementById("mainCategorySelect").options.add(newElement) ;
	}
}
//*******************************************************************************************************
function formSubmit(el)
{//edit categories onsubmit function
	if ((parseInt(document.all.form1.do_what_A.value) == 0) && (parseInt(document.all.form1.do_what_B.value) == 0))
	{
		alert("You must select any operation to do!") ;
		return false ;
	}
	if (((parseInt(document.all.form1.do_what_A.value) == 2) ||  (parseInt(document.all.form1.do_what_A.value) == 3))
	 && (parseInt(document.all.form1.mainCategorySelect.value) == 0))
	{
		alert("For edit or delete a \"Main Category\" you must select a \"Main Category\"") ;
		return false ;
	}
	if (((parseInt(document.all.form1.do_what_A.value) == 2) ||  (parseInt(document.all.form1.do_what_A.value) == 1))
	 && isEmpty(document.all.form1.edit.value))
	{
		alert("For add or edit \"Main Category\" you must get a new value") ;
		return false ;
	}
	if (((parseInt(document.all.form1.do_what_B.value) == 2) ||  (parseInt(document.all.form1.do_what_B.value) == 3))
	 && ((parseInt(document.all.form1.mainCategorySelect.value) == 0) || 
	 (parseInt(document.all.form1.subCatASelect.value) == 0)))
	{
		alert("For edit or delete \"Sub-Category\" you must select a \"Main Category\" and a \"Sub-Category\"") ;
		return false ;
	}
	if (((parseInt(document.all.form1.do_what_B.value) == 2) ||  (parseInt(document.all.form1.do_what_B.value) == 1))
	 && isEmpty(document.all.form1.edit.value))
	{
		alert("For add or edit \"Sub-Category\" you must get a new value") ;
		return false ;
	}
	if ((parseInt(document.all.form1.do_what_B.value) != 0)  && (parseInt(document.all.form1.do_what_A.value) != 0))
	{
		alert("You must select only one operation!") ;
		return false ;
	}
	if ((parseInt(document.all.form1.do_what_A.value) == 3) && (document.all.form1.subCatASelect.length > 1))
	{
		alert("You must first delete all \"Sub-Category\"elements!") ;
		return false ;
	}
	var answer = confirm("All changes will be procced permanently.\nAre you sure?") ;
	if (answer)
		return true ;
	return false ;
}
//***************************************************************************************************
function valChangeOrder(form)
{
	var values = new Array() ;
	var duplicates = new Array() ;
	var arrayindex = 0;
	var ok = true ;
	var elementslist = form.elements ;
	for (i = 0; i < elementslist.length; i++)
		if(elementslist[i].type == "select-one")
		{
			values[arrayindex] = elementslist[i].value;
			arrayindex++ ;
		}
//check for duplicates
	for (i = 0; i < values.length; i++)
	{
		checkeditem = values[i] ;
		for(z = i + 1; z < values.length; z++)
			if(checkeditem == values[z])
			{
				duplicates[i] = values[i] ;
				ok = false ;
			}
	}
	if (!ok)
		alert("Please provide different sort order for each Item.") ;
	return (ok) ;
}
//**********************************************************************************************************
function recalc()
{
	document.all.form1.submit() ;
}
//*********************************************************************************************************
function CurrencyFormatted(amount)
{//this function do format for the currency in the format of $$$$.CC
	var i = parseFloat(amount) ;
	if (isNaN(i)) 
		i = 0.00 ;
	var minus = '';
	if (i < 0)
		minus = '-';
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if (s.indexOf('.') < 0)
		s += '.00';
	if (s.indexOf('.') == (s.length - 2))
		s += '0';
	s = minus + s;
	return s;
}
//********************************************************************************************
function CommaFormatted(amount)
{//this function format any number to be represented in the format 100,000.000
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if (isNaN(i)) 
		return ''; 
	var minus = '';
	if (i < 0) 
		minus = '-';
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0)
		a.unshift(n);
	n = a.join(delimiter);
	if(d.length < 1)
		amount = n;
	else 
		amount = n + '.' + d;
	amount = minus + amount;
	return amount;
}
//********************************************************************************************************
function valShippMethod(form)
{
	if (isEmpty(form.methodName.value))
	{
		alert("Please enter a shippment method name.");
		form.methodName.focus() ;
		return false ;
	}
	return true; 	
}
//**********************************************************************************************
function valRaffleTicket(what)
{
	if (isEmpty(document.all.form1.numTickets.value))
	{
		alert("Please enter the number of tickets.");
		document.all.form1.numTickets.focus();
		return false ;
	}
	
		for (var j = 0; j < form1.numTickets.value.length;j++)
		if (isNaN(parseInt(form1.numTickets.value.charAt(j),10)))
			{
				alert("Please enter only numbers.");
				document.all.form1.numTickets.focus();
				return false;
			}


	if (isEmpty(document.all.form1.clientName.value))
	{
		alert("Please enter the clients name.");
		document.all.form1.clientName.focus();
		return false ;
	}
	if (isEmpty(document.all.form1.clientEmail.value))
	{
		alert("Please enter the clients email.");
		document.all.form1.clientEmail.focus();
		return false ;
	}
	if (!checkMail(document.all.form1.clientEmail.value))
		{
			alert("Please enter a valid email.");
			document.all.form1.clientEmail.focus();
			return false ;
		}
	if (isEmpty(document.all.form1.clientPhone.value))
	{
		alert("Please enter the clients phone number.");
		document.all.form1.clientPhone.focus();
		return false ;
	}
	

	return true ;
}
//**********************************************************************************************
