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.length==4)
	{
		for(var index=0;index<dateVar.length;index++)
		{
			
				var temp = parseInt(dateVar.charAt(index));
				if (isNaN(temp))
				{
					window.alert("Date must include only 'numbers'.  (Date format is: yyyy)")
					return false;
				}
	   		
		}
		return true;
	}
	else
		{
			window.alert("Date must include only 'numbers'.  (Date format is: yyyy)")
					return false;
		}*/
	/*if ((dateVar.indexOf('/')==2 && dateVar.length==7) || (dateVar.indexOf('/')==1 &&  dateVar.length==6))
	{
		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;
				}
	   		}
		}
		if (dateVar.length==7)
		{
			var month = parseInt(dateVar.substring(0,2),10);
			var year = parseInt(dateVar.substring(3,dateVar.length),10) ;
		}
		if (dateVar.length==6)
		{
			var month = parseInt(dateVar.substring(0,1),10);
			var year = parseInt(dateVar.substring(2,dateVar.length),10) ;
		}
		var today = new Date() ;
		if (month > 12 || month < 1)
		{
			alert("You have entered an illegal month.\nPlease check the month.") ;
			return false ;
		}
		return true;	
	}
	else 
	{
		alert("Date format is: m/yyyy or mm/yyyy");
		return false;
	}
}*/
//*************************************************************************************************
function validatePrice(str)
{//this function checks the price: the price must be in form $$$$.CC
	var validExpr = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/ ;
	if (validExpr.test(str))
		return true ;
	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 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 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 verifyAddEditBookForm(whatForm)
{//this function checks add or edit product form for backend
// @param - whatForm = add form (==0) or edit form
	if (isEmpty(document.all.form1.book_name.value))
	{
		alert("The fileld Book Name is required!") ;
		document.all.form1.book_name.focus();
		return false ;
	}
	if (isEmpty(document.all.form1.author.value))
	{
		alert("The fileld Book Author is required!") ;
		document.all.form1.author.focus() ;
		return false ;
	}
	if (isEmpty(document.all.form1.catalog_number.value))
	{
		alert("The fileld Model Number is required!") ;
		document.all.form1.catalog_number.focus() ;
		return false ;
	}
	if (isEmpty(document.all.form1.pages.value))
	{
		alert("The fileld Book Pages is required!") ;
		document.all.form1.pages.focus() ;
		return false ;
	}
	else
		for (var i = 0; i < document.all.form1.pages.value.length; i++)
		{
			var temp = parseInt(document.all.form1.pages.value.charAt(i));
		    if (isNaN(temp))
			{
				alert("Field Book Pages must include only 'numbers'.")
				document.all.form1.pages.focus() ;
		        return false;
            }
		}
	if (!checkDate(document.all.form1.book_date.value))
	{
		document.all.form1.book_date.focus();
		return false ;
	}
	/*if (isEmpty(document.all.form1.book_pub.value))
	{
		alert("The fileld Book publisher is required!") ;
		document.all.form1.book_pub.focus() ;
		return false ;
	}
	/*if (parseInt(document.all.form1.mainCategorySelect.value) == 0 )

	{
		alert("The fileld Book Category is required!") ;
		document.all.form1.mainCategorySelect.focus() ;
		return false ;
	}
	if ((parseInt(document.all.form1.subCatASelect.value) == 0) && (document.all.form1.subCatASelect.length > 1))
	{
		alert("The fileld Book Sub Category is required!") ;
		document.all.form1.subCatASelect.focus() ;
		return false ;
	}
*/
	if (isEmpty(document.all.form1.price.value))
	{
		alert("The fileld Price is required!");
		document.all.form1.price.focus() ;
		return false ;
	}
	else
	{
		if (!validatePrice(document.all.form1.price.value))
		{
			alert("Invalid Price!");
			document.all.form1.price.focus() ;
			return false ;
		}
	}
	if (!isEmpty(document.all.form1.price_special.value))
		if (!validatePrice(document.all.form1.price_special.value))
		{
			alert("Invalid Special Price!");
			document.all.form1.price_special.focus() ;
			return false ;
		}
		
/*	if (isEmpty(document.all.form1.brief.value))
	{
		alert("The fileld Brief Description is required!") ;
		document.all.form1.brief.focus() ;
		return false ;
	}
*/
	if (whatForm == 0)
	{
		if (isEmpty(document.all.form1.small_picture.value))
		{
			alert("The fileld Small Image is required!");
			document.all.form1.small_picture.focus() ;
			return false ;
		}
		else
			if (!validPicture(document.all.form1.small_picture.value))
			{
				alert("The file you've entered in the Small Image field isn't an image file.") ;
				document.all.form1.small_picture.focus() ;
				return false ;
			}
		if (isEmpty(document.all.form1.big_picture.value))
		{
			alert("The fileld Large Image is required!");
			document.all.form1.big_picture.focus() ;
			return false ;
		}
		else
			if (!validPicture(document.all.form1.big_picture.value))
			{
				alert("The file you've entered in the Large Image field isn't an image file.") ;
				document.all.form1.big_picture.focus() ;
				return false ;
			}
	}
	if (isEmpty(document.all.form1.book_descr.value))
	{
		alert("The fileld Book Description is required!") ;
		document.all.form1.book_descr.focus() ;
		return false ;
	}
	return true;
}
//****************************************************************************************************
function ChangePic()
{
	if ((isEmpty(document.all.form1.first.value)) && (isEmpty(document.all.form1.second.value)))
	{
		alert("You must to enter at least one picture.") ;
		return false ;
	}
	return true ;
}
//****************************************************************************************************
function ValChkTitle()
{
	if (isEmpty(document.all.chgTitle.title.value))
	{
		alert("Please enter the title.") ;
		document.all.chgTitle.title.focus() ;
		return false ;
	}
	return true ;
}
//****************************************************************************************************
function SavePicture()
{
	if (isEmpty(document.all.frmSavePic.fileName.value))
	{
		alert("Please enter the file name.") ;
		document.all.frmSavePic.fileName.focus() ;
		return false ;
	}
	else
		if (!validPicture(document.all.frmSavePic.fileName.value))
		{
			alert("Not valid image file.") ;
			document.all.frmSavePic.fileName.focus() ;
			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 isValidCreditCard(type, ccnum) {
	if (parseInt(type) == 2) 
	{
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	} 
	else 
		if (parseInt(type) == 4) 
		{
			var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
		} 
		else 
			if (parseInt(type) == 1) 
			{
				if (ccnum.length == 8)
				{
					ccnum = "0" + ccnum ;
				}
				var re = /^\d\d{7}\d$/;
			} 
			else 
				if (parseInt(type) == 5) 
				{
					var re = /^3[4,7]\d{13}$/;
				} 
				else 
					if (parseInt(type) == 3) 
					{
						var re = /^3[0,6,8]\d{12}$/;
					}
   if (!re.test(ccnum)) 
	   return false;
   var checksum = 0;
   if (parseInt(type) != 1)
   {
		for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) 
			checksum += parseInt(ccnum.charAt(i-1));
		for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) 
		{
			var digit = parseInt(ccnum.charAt(i-1)) * 2;
			if (digit < 10) 
				checksum += digit; 
			else 
				checksum += (digit-9); 
		}
		if ((checksum % 10) == 0) 
			return true; 
		else 
			return false;
   }
   else
	{
	   for(var i=1; i <= 9; i++)
			checksum += parseInt(ccnum.charAt(i-1)) * (9 - (i-1)) ;
		if ((checksum % 11) == 0)
			return true;
		else
			return false ;
	}
}
//********************************************************************************************************
function validateOrderForm()
{
	if (isEmpty(document.all.doOrder.name.value))
	{
		alert("The field \"First Name\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.last_name.value))
	{
		alert("The field \"Last Name\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.id_number.value))
	{
		alert("The field \"ID number\" is requared.") ;
		return false ;
	}
	else
	{
		for (var i = 0; i < document.all.doOrder.id_number.value.length; i++)
			if (isNaN(document.all.doOrder.id_number.value.charAt(i)))
			{
				alert("You should enter only numbers in the \"ID Number\" field.") ;
				return false ;
			}
	}
	if (isEmpty(document.all.doOrder.telephone.value))
	{
		alert("The field \"Telephone number\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.email.value))
	{
		alert("The field \"E-mail address\" is requared.") ;
		return false ;
	}
	else if (!checkMail(document.all.doOrder.email.value))
	{
		alert(document.all.doOrder.email.value) ;
		alert("The e-mail address you've entered is invalid.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.street.value))
	{
		alert("The field \"Street\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.house_num.value))
	{
		alert("The field \"House Number\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.city.value))
	{
		alert("The field \"City\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.zip.value))
	{
		alert("The field \"Zip\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.credit_name.value))
	{
		alert("The field \"Credit card owner name\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.credit_id.value))
	{
		alert("The field \"Credit card owner ID number\" is requared.") ;
		return false ;
	}
	else
	{
		for (var i = 0; i < document.all.doOrder.credit_id.value.length; i++)
			if (isNaN(document.all.doOrder.credit_id.value.charAt(i)))
			{
				alert("You should enter only numbers in the \"Credit card owner ID Number\" field.") ;
				return false ;
			}
	}
	if (document.all.doOrder.credit_card.value == "none")
	{
		alert("The field \"Credit Card Type\" is requared.") ;
		return false ;
	}
	if (isEmpty(document.all.doOrder.credit_number.value))
	{
		alert("The field \"Credit Card number\" is requared.") ;
		return false ;
	}
	else
	{
		for (var i = 0; i < document.all.doOrder.credit_number.value.length; i++)
			if (isNaN(document.all.doOrder.credit_number.value.charAt(i)))
			{
				alert("You should enter only numbers in the \"Credit Card Number\" field.") ;
				return false ;
			}
	}
	if (!isValidCreditCard(document.all.doOrder.credit_card.value, document.all.doOrder.credit_number.value))
	{
		alert("The number of credir card is invalid.") ;
		return false ;
	}
	return true ;
}
//**********************************************************************************************************
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 valShippClass(form)
{
	if (isEmpty(form.sclassName.value))
	{
		alert("Please enter a shippment class name.");
		form.sclassName.focus() ;
		return false ;
	}
	return true; 	
}
//**********************************************************************************************
 function changeMethod(value)
{//this function work when user select some country in the set shipment class price page
	var str, value ;
	str = "setShippClassPrice.asp?method=" + value ;
	document.location.href = str ;
}
//************************************************************************************************
function validatePriceForm()
{//this function checks isers input in the edit shipment class price page
	if (parseInt(document.getElementById("method").value) == 0)
	{
		alert("Please select the country first.") ;
		return false ;
	}
	for (var i = 1 ;i < 11 ;i+=2)
	{
		if ((parseFloat(document.forms[0].elements[i].value) == 0.0) || (parseFloat(document.forms[0].elements[i+1].value) == 0.0))
		{
			alert("Price of Class " + (i -1) + " can't be equals to 0.0") ;
			return false
		}
		if ((!validatePrice(document.forms[0].elements[i].value)) || (!validatePrice(document.forms[0].elements[i+1].value)))
		{
			alert("Invalid Price!");
			return false ;
		}
	}
	return true ;
}

