// JavaScript Document

//var xmlHttpMyCat
var xmlHttpMyCat2


function addToCatalogue()
{
	
	var product=document.getElementById("product").value
	var price=document.getElementById("price").value
	var stock=document.getElementById("stock").value
	var unitP=document.getElementById("unit").value
	var days=document.getElementById("days").value
	var comments=document.getElementById("comments").value
	
	if (product=='') {
		alert ("Product field is empty")
		return false
	}
	if(isNaN(parseInt(price))) {
		alert ("The value of price is incorrect")
		return false
	}
	if (price=='') {
		alert ("Price field is empty")
		return false
	}
	if(parseInt(stock)!=stock-0) {
		alert ("Stock must be an integer")
		return false
	}
	if (stock=='') { // agregar validacion de numero entero
		alert ("Stock field is empty")
		return false
	}
	if (unitP=='') {
		alert ("Unit field is empty")
		return false
	}
	if(parseInt(days)!=days-0) {
		alert ("The number of days must be an integer")
		return false
	}
	if ((days=='') || (days>30) || (days<1)) {
		alert ("Value for days is incorrect")
		return false
	}
/*
	xmlHttpMyCat=GetXmlHttpObject()
	if (xmlHttpMyCat==null)
	 {
		alert ("Browser does not support HTTP Request")
		return
	 }
	var url="addMyCatalogue.php"
	url=url+"?product="+product
	url=url+"&price="+price
	url=url+"&stock="+stock
	url=url+"&unit="+unitP
	url=url+"&days="+days
	url=url+"&exp_id="+exp_id

	if (comments)
		url=url+"&comments="+comments
	url=url+"&sid="+Math.random()
alert (url)	
	xmlHttpCat.onreadystatechange=stateChangedMyCat 
	xmlHttpMyCat.open("GET",url,true)
	xmlHttpMyCat.send(null)
	*/
}

function confirmBuy()
{
	var quantity=document.getElementById("quantity").value
	var price=document.getElementById("price").value
	var stock=document.getElementById("stock").value

	if (quantity==''){
		alert("You must specify the quantity of units to buy")
		return false
	}
	if(parseInt(quantity)!=quantity-0) {
		alert ("The Quantity must be an integer")
		return false
	}

	if (parseInt(quantity) > parseInt(stock)){
		alert("The quantity to buy can not exceed the stock")
		return false
	}
	var total=quantity*price
	var conf=confirm("The total amount of this sale would be: " + total+'\n'+"Are you sure you want to buy "+quantity+" units of this product?")
	if (conf==false)
		quantity=document.getElementById("quantity").value=''
	else
		return true
	
}

function confirmDelete()
{
	
	var conf=confirm("Are you sure you want to delete this product from the catalogue?")
	if (conf==false) {
		return false
	} else {
		document.getElementById("form1").action="MainPage.php?page=myCatalogue.php"
		document.getElementById("form1").submit()
		return true
	}
	
}


function printMyCatalogue(index)
{  
	var exp_id=document.getElementById("exp_id").value

	xmlHttpMyCat2=GetXmlHttpObject()
	if (xmlHttpMyCat2==null)
	 {
		alert ("Browser does not support HTTP Request")
		return
	 }
	var url="printMyCatalogue.php"
	url=url+"?exp_id="+$exp_id
	url=url+"&index="+index
	url=url+"&sid="+Math.random()
	
	xmlHttpMyCat2.onreadystatechange=stateChangedMyCat2 
	xmlHttpMyCat2.open("GET",url,true)
	xmlHttpMyCat2.send(null)
}

function stateChangedMyCat2() 
{ 
	
	if (xmlHttpMyCat2.readyState==4 || xmlHttpMyCat2.readyState=="complete")
	 { 	
	 	document.getElementById("catalogue").value=" "
		document.getElementById("catalogue").innerHTML=xmlHttpCat.responseText
	 }
}


