String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)};

function onLoadPunti(){
	document.getElementById('idCompagniaPP').onchange=alertCodeGNV;
}


function alertCodeGNV(){
	if(document.getElementById('idCompagniaPP').value == 38)
		alert("ATTENZIONE! Nel campo 'Numero Biglietto' scrivere il codice che inizia con 'GN-'");
	
}

function salvaPPCoop(){
	
	var lenEAN13 = document.getElementById('tesseraPP').value.length;
	var tessera = document.getElementById('tesseraPP').value;

	if (
				alertWrong(msgNomePP, 'checkName','nomePP',false)
				|| alertWrong(msgCognomePP, 'checkName','cognomePP',false)
				|| (lenEAN13 == 13 && alertWrong(msgTesseraPP, 'checkEAN13','tesseraPP',false))
				|| (alertWrongOnly(msgTesseraPP, !(lenEAN13 == 8 || lenEAN13 == 13)))
				|| alertWrong(msgNprenotazionePP, 'checkNotEmpty','nprenotazionePP',false)
				|| alertWrong(msgNprenotazionePP, 'checkValidChar','nprenotazionePP',false)
				|| alertWrong(msgImportoPP, 'checkCurrency','importoPP',false)
				|| alertWrong(msgMailPP, 'checkMail','mailPP',false)
				|| alertWrong(msgTelefonoPP, 'checkTelNumber','telefonoPP',true)
				|| alertWrongOnly("Privacy", !document.getElementById('privacyPP').checked)
				|| alertWrongOnly("Regolamento", !document.getElementById('regolamentoPP').checked)
			
				){
		return false;
	}else{
	
		if (lenEAN13 == 13){
			if (
											 (tessera.startsWith('0270') &&	(tessera.substr(10,2)=='00' || tessera.substr(10,2)=='10')) 
										|| (tessera.startsWith('0272') &&	(tessera.substr(10,2)=='02')) 
										|| (tessera.startsWith('0273') &&	(tessera.substr(10,2)=='00' || tessera.substr(10,2)=='10')) 
										
										|| (tessera.startsWith('0250') &&	(tessera.substr(10,2)=='00')) 
										|| (tessera.startsWith('0252') &&	(tessera.substr(10,2)=='00')) 
										|| (tessera.startsWith('0255') &&	(tessera.substr(10,2)=='00')) 
										|| (tessera.startsWith('0258') &&	(tessera.substr(10,2)=='00')) 
										
										|| (tessera.startsWith('0280')) 
										|| (tessera.startsWith('0281')) 
										|| (tessera.startsWith('0290')) 
										|| (tessera.startsWith('0295')) 
										|| (tessera.startsWith('0292')) 
										
										|| (tessera.startsWith('0210'))
										|| (tessera.startsWith('0213'))
										|| (tessera.startsWith('0220'))
										|| (tessera.startsWith('0221'))
										|| (tessera.startsWith('0229'))
										|| (tessera.startsWith('0231'))
										|| (tessera.startsWith('0232'))
										|| (tessera.startsWith('0233'))
										|| (tessera.startsWith('0238'))
										|| (tessera.startsWith('0240'))
										|| (tessera.startsWith('0241'))
										|| (tessera.startsWith('0243'))
	
									){
					
					document.getElementById('partnerPoint').submit();
				
			}else{
				alert('Codice tessera non valida');
				//return false;
			}
		//se il codice inserito e' di 8 caratteri controllo i codici e poi antepongo 02 e postpongo 000
		}else{
			if (		 
										(tessera.startsWith('29')) 
										|| (tessera.startsWith('31')) 
										|| (tessera.startsWith('32')) 
										|| (tessera.startsWith('33')) 
										|| (tessera.startsWith('38')) 
					
									){
					document.getElementById('tesseraPP').value = '02'+document.getElementById('tesseraPP').value+'000';
					
					document.getElementById('partnerPoint').submit();
				
			}else{
				alert('Codice tessera non valida');
				//return false;
			}
		
		
		}
		
					
			
		
	}
}


function salvaPPQ8(){
	if (alertWrong(msgNomePP, 'checkName','nomePP',false)
				|| alertWrong(msgCognomePP, 'checkName','cognomePP',false)
				|| alertWrong(msgTesseraPP, 'checkEAN13','tesseraPP',false)
//				|| alertWrong(msgNprenotazionePP, 'checkNotEmpty','nprenotazionePP',false)
				|| alertWrong(msgNprenotazionePP, 'checkValidChar','nprenotazionePP',false)
				|| alertWrong(msgImportoPP, 'checkCurrency','importoPP',false)
				|| alertWrong(msgMailPP, 'checkMail','mailPP',false)
				|| alertWrong(msgTelefonoPP, 'checkTelNumber','telefonoPP',true)
				|| alertWrongOnly("Privacy", !document.getElementById('privacyPP').checked)
				|| alertWrongOnly("Regolamento", !document.getElementById('regolamentoPP').checked)
			
				){
		return false;
	}else{
	
		document.getElementById('partnerPoint').submit();

	}	
}

function salvaPP(){
	
		
	if (document.getElementById('partnerPP').value=='COOP'){
		return salvaPPCoop();
	}else{
		return salvaPPQ8();
	}
	
}
