// JavaScript Document

	var annoMinimo		=	1970																		;
	var curdate 		= 	new Date()																	;
	var curyear 		= 	curdate.getFullYear()														;
	var annoMax			=	curyear																		;
	var maxChartext		=	500																			;
	var txtAlertNoConc	=	'Attenzione, nella provincia selezionata non sono presenti Concessionari.'	;
	
$(document).ready(function() {
	var idConc			=	'concessionario'		;
	var	idProvConc		=	'province'				;
	var	idAutoVW		=	'modelloVetturaVW'		;
	var	idModelAltro	=	'altroModello'			;
	var idAltraAuto		=	'marcaVetturaAltro'		;	
	var classCalendar	=	'clsCalendar'			;	
	var conObj			=	null					;
	var modelloAltro	=	null					;
	var altraMarca		=	null					;
	var autoVW			=	null					;
	var modelloAltro	=	null					;
	
	
	$('textarea').keydown(function(event){
		  var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
		  if ((event.ctrlKey && (pressedKey == "c" || pressedKey == "v") ) || event.which == 45 || event.which == 224) {
		    return false;
		  }
	});
	
	$('textarea').bind("contextmenu",function(e){
		return false;
	});

	//inizializza i concessionari
	
	if($('#'+idConc).is('select')){
		conObj = $('#'+idConc);
	}
	
	
	//inizializza calendario
	if ($.datepicker){

		jQuery(function($){
		$.datepicker.regional['it'] = {clearText: 'Svuota', clearStatus: '',
			closeText: 'Chiudi', closeStatus: '',
			prevText: '&lt;Prec', prevStatus: '',
			nextText: 'Succ&gt;', nextStatus: '',
			currentText: 'Oggi', currentStatus: '',
			monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno',
			'Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
			monthNamesShort: ['Gen','Feb','Mar','Apr','Mag','Giu',
			'Lug','Ago','Set','Ott','Nov','Dic'],
			monthStatus: '', yearStatus: '',
			weekHeader: 'Sm', weekStatus: '',
			dayNames: ['Domenica','Lunedi','Martedi','Mercoledi','Giovedi','Venerdi','Sabato'],
			dayNamesShort: ['Dom','Lun','Mar','Mer','Gio','Ven','Sab'],
			dayNamesMin: ['Do','Lu','Ma','Me','Gio','Ve','Sa'],
			dayStatus: 'DD', dateStatus: 'D, M d',
			dateFormat: 'dd/mm/yy', firstDay: 0, 
			initStatus: '', isRTL: false};
			$.datepicker.setDefaults($.datepicker.regional['it']);
			$.datepicker.setDefaults({yearRange: annoMinimo+':'+annoMax, maxDate: curdate});
		});
		
		$('.'+classCalendar).datepicker();    
	}
	//inizializza le altre vetture
	
	if($('#'+idAltraAuto).is('select')){
		altraMarca = $('#'+idAltraAuto);
	}
	
	if($('#'+idModelAltro).is('input')){
		modelloAltro = $('#'+idModelAltro);
	}
	
	if($('#'+idAutoVW).is('select')){
		autoVW = $('#'+idAutoVW);
	}	
	
	//inizializza le provicnie
	
	if($('#'+idProvConc).is('select')){
		$('#'+idProvConc).html('');
		$(conObj).html('');
		$(conObj).append("<option value =\"" + "0" + "\">" + ""  + "</option>" );
		$(conObj).append("<option value =\"" + "0" + "\">" + "--- --- --- ---"  + "</option>" );
		for(i=-1;i<js_LunghDati;i++){
				if(i==-1){
							$('#'+idProvConc).append("<option class=\"optSelectedFirst\" value =\"false\">" + "--- --- --- ---"  + "</option>" );
				}
				if(JS_prov_full[i] != undefined){
							$('#'+idProvConc).append("<option value =\"" + i + "\">" + JS_prov_full[i]  + "</option>" );
				}
		}
		$('.optSelectedFirst',$('#'+idProvConc)).attr('selected','selected');
		
		/*$('#'+idProvConc).bind("change", function(){
			matchProvConc(this,conObj);
		});*/
	}
	
	$('.textAeraInput').bind("change", function(){
		imposeMaxLength(this, maxChartext)	
	});
	$('.textAeraInput').bind("keypress", function(){
		imposeMaxLength(this, maxChartext)	
	});
	
	$('.textAeraInput').keypress(function(event){ 
		  var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
		  if (event.which == 8364) {
		    $('.textAeraInput').val($('.textAeraInput').val() + ' euro ');
		    return false;
		  }
	});
	
	$('.textAeraInput').bind("mouseout", function(){
		imposeMaxLength(this, maxChartext)	
	});
	
	//inizializza la Select Auto VW
	
	if($('#'+idAutoVW).is('select')){
		$('#'+idAutoVW).bind("change", function(){
			checkAutoSelectedVW(this,altraMarca,modelloAltro);
		});
	}
	
	//bind Azione to Altra marca
	if($('#'+idAltraAuto).is('select')){
		$('#'+idAltraAuto).bind("change", function(){
			checkAltraVetturaSelected(this,autoVW,modelloAltro);
		});
	}
	
	if($('#'+idModelAltro).is('input')){
		$('#'+idModelAltro).bind("focus", function(){
			checkAltraVetturaSelected(this,autoVW,modelloAltro);
		});
	}
	
	/*$("#province").bind("change", function(){
	
		  $("#infoConc").html('<br/>');
		  $("#captionIndirizzo").html('');
	});
	$("#concessionario").bind("change", function(){
		$.post("/include-VW/getInfoConcByIdentification.asp", { uidConc: $(this).val()},function(data){
		  $("#infoConc").html(data);
		  $("#captionIndirizzo").html('Indirizzo');
		});
	});*/
	
});

//funzione per cambiare i concessionari in base alla provincia

function matchProvConc(selezzed,oggConc){
	if  (selezzed.options[selezzed.selectedIndex].value != 'false'){ 
		x=selezzed.options[selezzed.selectedIndex].value;
		$(oggConc).html('');
		concessCount	=	0;
		for (i=0; i<=concess_IDs[x].length ; i++){	
			concessCount++;
			if(i==0){
				$(oggConc).append("<option value =\"\" class=\"optSelectedFirst\">" + "--- --- --- ---"  + "</option>" );
			}
			else{
				$(oggConc).append("<option value =\"" + concess_IDs[x][i-1] + "\">" + concessionari[x][i-1]  + "</option>" );
			}
		}
		(concessCount == 1) ? alert(txtAlertNoConc) : '';
		$('.optSelectedFirst',$(oggConc)).attr('selected','selected');
		if ($.trim($(oggConc).html())==''){
			$(oggConc).html('');
			$(oggConc).append("<option value =\"" + "" + "\" selected=\"selected\">" + "--- ---  --- ---"  + "</option>" );
			
		}
	}
	else{
	$(oggConc).html('');
	$(oggConc).append("<option value =\"" + "0" + "\">" + ""  + "</option>" );
	$(oggConc).append("<option value =\"" + "0" + "\">" + "--- --- --- ---"  + "</option>" );
	}
}

function SetConcByIdConc(idProv,idConc,oggConc,oggProv){
	idProv--;
	$(oggProv).val(idProv);
	$(oggConc).html('');
	for (i=1; i<=concess_IDs[idProv].length ; i++){	
		if (concess_IDs[idProv][i-1] == idConc){
			$(oggConc).append("<option value =\"" + concess_IDs[idProv][i-1] + "\">" + concessionari[idProv][i-1]  + "</option>" );
		}
	}
	if ($.trim($(oggConc).html())==''){
		$(oggConc).html('');
		$(oggConc).append("<option value =\"" + "" + "\" selected=\"selected\">" + "--- --- --- ---"  + "</option>" );
		alert(txtAlertNoConc);
	}	
}

function checkAutoSelectedVW(autoVW,altraMarca,altroModello){
	if (autoVW.options[autoVW.selectedIndex].text=='altro'){
		$(altroModello).val('');
		$(altraMarca).val(1);
	}
	else{
		$(altroModello).val('');
		$(altraMarca).val(0);
	}	
}

function checkAltraVetturaSelected(altraMarca,autoVW,modelloAltro){
	var	strFull = '';
	var idAltro = '';
	$('option',autoVW).each(function(){
				if($.trim($(this).html()) == 'altro'){
					idAltro = $(this).attr('value');
				}
	});
	$(autoVW).val(idAltro);
}


function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function imposeMaxLength(Object, MaxLen)
{
	$(Object).val(strReplaceSql($(Object).val()));
	if (Object.value.length > MaxLen){
		Object.value = Object.value.substr(0,MaxLen-1);
	}
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("La data deve essere nel formato : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Il mese inserito non e' valido")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Il giorno inserito non e' valido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("L'anno deve essere di 4 cifre compreso tra il "+minYear+" ed il "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Inserire una data valida")
		return false
	}

	var wroteDate = new Date();
	wroteDate.setFullYear(year,month-1,day);
	wroteDate.setHours(0);
	wroteDate.setMinutes(0);
	wroteDate.setSeconds(0);
	if (curdate < wroteDate){
		alert("la data non puo' essere futura");
		return false;
	}
	
return true
}
