//JS functions for Eklavya order form

//Select or deselect (set qty=1) all the books in the current category
function selectSet(catname, list) {
	//list is the select list for the current category
	for(var j=0; j<list.options.length; j++)
	{
	 if (list.options[j].selected) {
	   var value = list.options[j].value;
		}
	}
	if(value != 0){
	  for(var i=0; i<catname.length; i++) {
	    //Set the quantity value to selected value
	  	document.orderForm[catname[i]].value = value ;
		
	  	//Explicitly invoke the onchange event for quantity field
	  	document.orderForm[catname[i]].onchange();
		}
	}
  else {
		for(var i=0; i<catname.length; i++) {
		  document.orderForm[catname[i]].value = '';
		  document.orderForm[catname[i]].onchange();
	  }
  }
}

//String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
function trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

//Calculate the amount for the quantity entered. Also verify qtty value
function calcAmt(amtField, nPrice, qtyField, numBooks){
	//Remove preceding and trailing blanks if any
	qtyField.value = trim(qtyField.value);
	if ((qtyField.value == 0) || (qtyField.value == "")){
		qtyField.value = "";
		amtField.value = "";
		updateTot(numBooks);
		return true;
	}
	//Validate the quantity first
	if (isNaN(qtyField.value)) {
		alert("Quantity must be a number!");
		setTimeout(function(){qtyField.focus()}, 10);	//		qtyField.focus()
		return false;
	}

	nAmt = nPrice * qtyField.value;
	amtField.value = nAmt;
	updateTot(numBooks);
}

//Update the total amount
function updateTot(numBooks){
	totAmt = 0
	for (i=0; i<numBooks; i++) {
		totAmt = totAmt + Number(document.orderForm["amt"+i].value)
	}
	document.orderForm.total.value = Math.ceil(totAmt);
}

// This will disable enter key in form apply it using keypressed event
function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     
		
			if(key == 13)
			{
				alert("Please use Tab key to move to the next field.\n Use mouse click for the Submit button.");
			}
     return (key != 13);
}

//Verify that the form being submitted is not blank
function verifyNoBlankForm(passForm, numBooks) {
	//Check if the form is blank
	
	isBlankForm = true
	for (i=0; i<numBooks; i++) {
		qty = trim(document.orderForm.elements['qty['+i+']'].value)
		if (qty != "" && qty != "0") {
			isBlankForm = false
			break
		}
	}//end-for
	if (isBlankForm) {
		alert("You cannot submit a blank form!")
		return false
	}
	//Check whether any quantity is still non-numeric
	for (i=0; i<numBooks; i++) {
		qty = trim(document.orderForm.elements['qty['+i+']'].value)
		if (isNaN(qty)) {
			alert('Quantity must be a number!!')
			document.orderForm.elements['qty['+i+']'].focus()
			return false
		}
	}//end-for
}

//Validate contact details
function validateContact(passForm) {
	//Check for mandatory fields
	if (!validateMandatory(passForm.CustName, "Name")) return false
	if (!validateMandatory(passForm.CustAddr, "Address")) return false
	if (!validateMandatory(passForm.CustDist, "District/City")) return false
	if (!validateMandatory(passForm.CustState, "State")) return false
	if (!validateMandatory(passForm.CustPin, "Pin")) return false
	if (!validateMandatory(passForm.CustEmail, "Email")) return false
	if (!validateMandatory(passForm.CustSTD, "STD number")) return false
	if (!validateMandatory(passForm.CustPhone, "Phone number")) return false
	//Verify that pin is numeric
	if (!validateNumeric(passForm.CustPin, "Pin code")) return false
	//Verify that pin is a 6 digit number
	if (passForm.CustPin.value.length != 6) {
		alert("Pin code must be 6 digits long")
		passForm.CustPin.focus()
		return false
	}
	//Verify that email address is valid
	if (!validateEmail(passForm.CustEmail.value)) {
		alert("Invalid Email address format!")
		passForm.CustEmail.focus()
		return false
	}
	//Verify that STD & phone are numeric
	if (!validateNumeric(passForm.CustSTD, "STD code")) return false
	if (!validateNumeric(passForm.CustPhone, "Phone number")) return false
	//Verify that mobile is numeric (implicit that this field is not blank)
	if (!validateNumeric(passForm.CustMobile, "Mobile number")) return false
}

//Validate mandatory field
function validateMandatory(passField, fieldName) {
	passField.value = trim(passField.value)
	if (passField.value == "") {
		alert(fieldName + " must be entered!")
		passField.focus()
		return false
	}
	return true
}

//Validate numeric field
function validateNumeric(passField, fieldName) {
	passField.value = trim(passField.value)
	if (isNaN(passField.value)) {
		alert(fieldName + " must be a number")
		passField.focus()
		return false
	}
	return true
}

//Validate email id format
function validateEmail(email) {
	invalidChars = " /:,;"

	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@", 1)
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@", atPos+1) > -1) {
		return false
	}
	periodPos = email.indexOf(".", atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length) {
		return false
	}
	return true
}

