// general purpose function to see if a suspected numeric input
// is a positive integer
function isPosInteger(inputVal) {
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}
// general purpose function to see if an input value has been
// entered at all
function isNotEmpty(inputStr) {
	if (inputStr == null || inputStr == "") {
		return false
	}
	return true
}
// general purpose function to see if a suspected numeric input
// is a positive or negative integer
function isInteger(inputVal) {
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			continue
		}
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}
// general purpose function to see if a suspected numeric input
// is a positive or negative number
function isNumber(inputVal) {
	oneDecimal = false
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			continue
		}
		if (oneChar == "." && !oneDecimal) {
			oneDecimal = true
			continue
		}
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}
// function to determine if value is in acceptable range
// for this application
function isInRange(inputStr) {
	num = parseInt(inputStr)
	if (num < 1 || num > 586 && num < 596 || num > 599 && num < 700 || num > 728) {
		return false
	}
	return true
}



function isEmailAddr(emailStr){
    var nome = "";
    var dominio = "";
    var pos = 0;
    var error = 0;
    pos = emailStr.indexOf("@");
    if(pos == -1) error = 1;
    else{
		nome = emailStr.substring(0, pos);
		if(nome.length == 0) error = 1;
		else {
		    dominio = emailStr.substring((pos+1), emailStr.length);
		    if((dominio.length == 0) ||
		       (dominio.indexOf(".") == -1)) error = 1;
		}
    }

    if(error ==1) return false;
    else return true;

}

//General purpose function to see if cookies are enabled on the brouser
function testCookies(){
    document.cookie = 'CookieTestABC123';
    var browserName = navigator.appName;
    var indSpace = browserName.indexOf(" ");
    browserNane = (indSpace == -1) ? browserName : browserName.substring(0, indSpace);
        if( (browserName.length == 0) || (document.cookie.indexOf('CookieTestABC123')) < 0) {
                alert(CookiesDisabledMsg);
                return false;
        }
        return true;
}

function CheckAnagrafica(form){
	if (!isNotEmpty(form.Nome_O.value)) {alert(AnagIncompMsg);return false;}
	if (!isNotEmpty(form.Cognome_O.value)) {alert(AnagIncompMsg);return false;}
	if (!isNotEmpty(form.Indir1_O.value)) {alert(AnagIncompMsg);return false;}
	if (!isNotEmpty(form.Indir2_O.value)) {alert(AnagIncompMsg);return false;}
	if (!isNotEmpty(form.Cap_O.value)) {alert(AnagIncompMsg);return false;}
	if (!isNotEmpty(form.Prov_O.value)) {alert(AnagIncompMsg);return false;}
	if (!isEmailAddr(form.Email_O.value)) {alert(EmailIncompMsg);return false;}
	if (!isNotEmpty(form.Tel_O.value)) {alert(AnagIncompMsg);return false;}
	if (isNotEmpty(form.Cognome_D.value)||(form.Paese_D.selectedIndex > 0)||isNotEmpty(form.Indir1_D.value)||isNotEmpty(form.Indir2_D.value)||isNotEmpty(form.Cap_D.value)||isNotEmpty(form.Prov_D.value)){
		if (!isNotEmpty(form.Cognome_D.value)) {alert(AnagDestMsg);return false;}
		if (!isNotEmpty(form.Indir1_D.value)) {alert(AnagDestMsg);return false;}
		if (!isNotEmpty(form.Indir2_D.value)) {alert(AnagDestMsg);return false;}
		if (!isNotEmpty(form.Cap_D.value)) {alert(AnagDestMsg);return false;}
		if (!isNotEmpty(form.Prov_D.value)) {alert(AnagDestMsg);return false;}
		if (form.Paese_D.selectedIndex == 0){alert(AnagDestMsg);return false;}
		if (form.Paese_D.selectedIndex != form.Paese_S.selectedIndex){alert(CoerenzaDestMsg);return false;}
	}
	else{
		if (form.Paese_O.selectedIndex != form.Paese_S.selectedIndex){alert(CoerenzaDestMsg);return false;}
	
	}
	return true;
}

function CheckQuantita(form){
	if (weight > 50){
		alert(TooHavvyMsg);
		return false;	
	}
	for (var i=0;i<numberOfRows;i++){
		if (form.Qty[i].selectedIndex > 0) {
			//La quantità è OK; controllo i totali
			if (isPosInteger(form.totalGlobLit.value))return true;
			alert(DestinazioneValidaMsg);
			return false;
		}
	}	
	alert(QuantitaMsg);
	return false;
}

