startHr = 8;
endHr = 20;
selectString = "seleziona";

function limitChars(textid, limit)
{
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
		return true;
}

function substringLimit(textId, limit)
{
	if($('#'+textId).val().length > limit)
		$('#'+textId).val($('#'+textId).val().substring(0,limit));
}

function townTokenizer(selectedString)
{
	//var form  = townSelect.form;
	return selectedString.split("|");
}

function showOverlayBusy()
{
	var h = $(document).height();
	var w = $(document).width();
	
	$('#busy').css('top', $(document).scrollTop() + 100);
	$('#busy').css('left', $(document).scrollLeft() + (w/2) - 50);
	
	$('#overlay').css('width', w);
	$('#overlay').css('height', h);
	$('#overlay').css('display', 'block');
	$('#busy').css('display', 'block');
}

function hideOverlayBusy()
{
	$('#busy').css('display', 'none');
	$('#overlay').css('width', 0);
	$('#overlay').css('height', 0);
	$('#overlay').css('display', 'none');
	
}

function submitLocatorForm(depotID)
{
	var form = document.locatorForm;
	form.depotID.value = depotID;
	var depotDetailsWindow = window.open('', 'depotDetails', 'width= 560, heigth=670, status=0, scrollbars=yes, resizable=yes, location=no, toolbar=no');
	form.submit();
}



function checkGoodChars(field, e, goodChars, maxLength)
{
	var charCode = 	getCharCode(e);
	if(charCode == 0)
		return true;

	if(goodChars == 'numbers')
		goodChars = '0123456789';
	else if(goodChars == 'letters')
		goodChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVZ ';
	else if(!goodChars)
		goodChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVZ.-_ &àèéìòùÀÈÉÌÒÙñÑçÇ';

	
	
	var char = String.fromCharCode(charCode);
	
	if(goodChars.indexOf(char) == -1)
		return false;

	if(field)
		if(field.value.length >= maxLength)
			return false;
		
	return true;
}



function checkGoodNumber(field, e, integers, floaters)
{
	if(!checkGoodChars(null, e, '0123456789.,'))
		return false;

	var regExp = new RegExp("\\.", "ig");
	field.value = field.value.replace(regExp,',');	
	return checkNumber(field, e, integers, floaters, ',')

}

function checkNumber(field, e, integers, floaters, separatorChar)
{
	var thereIsSeparator = (field.value.indexOf(separatorChar) != -1);
	
	if(getCharCode(e) == 0)
		return true;

	if(String.fromCharCode(getCharCode(e)) == separatorChar)
	{
		if(parseInt(floaters) == 0)
			return false;

		if(field.value.length == 0)
			field.value = '0';
			
	}
	else
	{
		if(!thereIsSeparator && field.value.length >= integers)
			return false;
	}
	
	
	commaIdx = field.value.indexOf(separatorChar);

	if(commaIdx != -1)
		if(field.value.substring(commaIdx + 1).length >= floaters)	
			return false;

}

function checkGoodTime(field)
{
	var hour = 0;
	var min = 0;
	
	var splitField = $(field).val().split(':');
	hour = splitField[0];
	min = splitField[1];
	if(parseInt(hour) > 23 || parseInt(min) > 59)
		return false;
	
	return true;
}


function checkTime(field, e)
{
	separatorChar = ':';
	
	if(!checkGoodChars(null, e, '0123456789.,:'))
	return false;
	
	var regExp = new RegExp("\\.", "ig");
	field.value = field.value.replace(regExp,':');	
	regExp = new RegExp("\\,", "ig");
	field.value = field.value.replace(regExp,':');	
	
	
	var thereIsSeparator = (field.value.indexOf(separatorChar) != -1);
	
	if(getCharCode(e) == 0)
		return true;

	if(String.fromCharCode(getCharCode(e)) == separatorChar)
	{
		if(thereIsSeparator)
			return false;
		
		if(field.value.length == 0)
			field.value = '0';
			
	}
	else
	{
		if(!thereIsSeparator && field.value.length >= 2)
			return false;
	}
	
	commaIdx = field.value.indexOf(separatorChar);

	if(commaIdx != -1)
		if(field.value.substring(commaIdx + 1).length >= 2)	
			return false;

	return true;		
}

function getCharCode(e)
{
	if(jQuery.browser.msie == true || jQuery.browser.opera == true)
		return e.keyCode;
	else
		return e.charCode;
}

function setOptionSelected(field, value)
{
	
	$(field).find('option').each(function(i){
		if($(this).val() == value)
			$(this).attr('selected', 'selected');
	
	});	

/*

	
	var opts = field.options;



	for(i =0; i< $(field).find('option').length;i++)
	{
		if(opts[i].value == value)
		{
			opts[i].selected = true;
			break;
		}
		
	}
*/
}

function setRadioChecked(field, value)
{
	for(i =0; i < field.length; i++)
	{
		if(field[i].value == value)
		{
			field[i].checked = true;
			break;
		}
	}
}


function convertDate(stringDate)
{
	// Date format dd/mm/yyyy
	var splittedDateString = stringDate.split("/");	
	return new Date(splittedDateString[2], (splittedDateString[1] - 1), splittedDateString[0],0,0,0);
}

function checkDate(stringDate)
{
	//This function returns '<' if the date is lower than today, '=' if it is the same, '>' if it is higher;
	var myDate = convertDate(stringDate);
	var oggi = today;
	
	if(oggi.getDay() == 0 || oggi.getDay() == 6)
		return '>';
	
	myDate.setHours(0);
	myDate.setMinutes(0);
	myDate.setSeconds(0);
	myDate.setMilliseconds(0);
	
	oggi.setHours(0);
	oggi.setMinutes(0);
	oggi.setSeconds(0);
	oggi.setMilliseconds(0);

	var idx = oggi - myDate;
	if(idx == 0)
		return '=';
	if(idx > 0)
		return '<';
	if(idx < 0)
		return '>';
}

function trim(theString)
	{
		return theString.replace(/^\s\s*/, '').replace(/\s\s*$/, '');	
	}


function createTimeOptions(selection, selectStr, same_day, start_hr, start_min, finish_hr, finish_min) {
    
    if(typeof myNow == 'undefined')
    	myNow = new Date();
    	 
    //var mioAdesso = new Date();
    hr = myNow.getHours();
    min = myNow.getMinutes();
    with (selection) {
        options.length = 0;
        options[options.length] = new Option(selectStr, "", true, true);
        options.selected = true;
        if (same_day == 1) {
            if (hr >= finish_hr) {
                start_hr = 800;
            } else {
                if ((hr >= start_hr) && (hr < finish_hr)) {
                    if (min > 30) {
                        start_hr = (hr + 1) * 100;
                    } else {
                        start_hr = (hr + 0.5) * 100;
                    }
                } else {
                    start_hr = start_hr * 100;
                }
            }
        } else {
            if (start_min == 0) {
                start_hr = start_hr * 100;
            } else {
                start_hr = (start_hr + 0.5) * 100;
            }
        }
        if (finish_min == 0) {
            finish_hr = finish_hr * 100;
        } else {
            finish_hr = (finish_hr + 0.5) * 100;
        }
        for (i = start_hr; i <= finish_hr; i = i + 50) {
            mod_res = i % 100;
            if (mod_res == 0) {
                div_res = i / 100;
                if (div_res < 10) {
                    options[options.length] = new Option("0" + div_res + ":00", "0" + div_res + "00");
                } else {
                    options[options.length] = new Option(div_res + ":00", div_res + "00");
                }
            } else {
                div_res = (i / 100) - 0.5;
                if (div_res < 10) {
                    options[options.length] = new Option("0" + div_res + ":30", "0" + div_res + "30");
                } else {
                    options[options.length] = new Option(div_res + ":30", div_res + "30");
                }
            }
        }
    }
}

/**************************************
	Controllo del Codice Fiscale
	Linguaggio: JavaScript
***************************************/

function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return false;
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return false;
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return false;
	return true;
}

function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return false;
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return false;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return false;
	return true;
}

function showAlertMessage()
{
	if(alertMessage !='')
		alert(alertMessage);
}

function addPackageSection()
{
	var pkgObj = getObj("pkgSection");
	var pkgObjClone = pkgObj.cloneNode(true);
	pkgObj.parentNode.appendChild(pkgObjClone);
	var lastIdx = $('div[id=pkgSection]').length - 1;
	var obj = $('div[id=pkgSection]').get(lastIdx);
	
	$(obj).find('input').val('');
}

function removePackageSection(linkObj)
{
	//var form = document.bookingForm;
	var pkgItemNo = $('[name=pkgItemNo]').get();
	
	
	if(pkgItemNo.length < 2)
		return;

	do
	{
		linkObj = linkObj.parentNode;	
	}
	while(linkObj.id != 'pkgSection');
	
	linkObj.parentNode.removeChild(linkObj);

}

function trimAllFields(form)
	{
		$(form.elements).each(function(){this.value = $.trim(this.value);});
	
	}

/*
/*
function get_Postcode_Mask(countryCode)
{
	return postcodeMasks[countryCode];
}
*/

function togglePostCode(countryCode, sectionId)
{
	if(isPostcodeCountry(countryCode))
	{
		$('#'+sectionId).show();
	}
	else
	{
		$('#'+sectionId).hide();
	}
}


function isPostcodeCountry(countryCode)
{
	var postcodeMasks = populatePostcodeMask();
	if (postcodeMasks[countryCode] == null)
		return false;
	else
		return true;
}

function checkPostCode(countryCode,postCodeField)
{
	$(postCodeField).removeClass('error');
	if($.trim(postCodeField.value) == '')
		return false;
		
	if(!validate_Postcode(countryCode,	postCodeField.value))
	{
		alert('Zip Code errato');
		$(postCodeField).addClass('error');
		postCodeField.focus();
	}

}


function validate_Postcode(countryCode,	pCode)
{
	var postcodeMasks = new Array();
	var postcodeMasks = populatePostcodeMask();
	var mask_array = postcodeMasks[countryCode].split(",");
	var pcode_valid = false;
	for (p=0; p < mask_array.length; p++)
	{
		var postcodeMask = '^';
		for (i = 0; i < mask_array[p].length; i++)
		{
			if (mask_array[p].charAt(i) == 'A')
				postcodeMask = postcodeMask + '[a-zA-Z]';

			if (mask_array[p].charAt(i) == 'N')
				postcodeMask = postcodeMask + '\\d';
			
			if (mask_array[p].charAt(i) == ' ')
				postcodeMask = postcodeMask + '\\s';
			
			if (mask_array[p].charAt(i) == '-')
				postcodeMask = postcodeMask + '\-';
		
		}
	
		postcodeMask = postcodeMask + '$';
		pcodeRE = new RegExp(postcodeMask);

		if (pcodeRE.test(pCode) == true)
			pcode_valid = true;
	
	}
	return pcode_valid;
}

function setSelectedItem(selectField, selectedValue)
{
	for(i=0; i < selectField.options.length;i++)
	{
		if(selectField.options[i].value == selectedValue)
		{
			selectField.options[i].selected = true;
			return;
		}
	}
}

function inizializeTimePicker(fieldName)
{
	$(".clockPicker").clockpick(
	{
		starthour: 8,
		endhour: 20,
		showminutes: true,
		minutedivisions: 4,
		military: true,
		event: 'click',
		layout: 'vertical',
		valuefield: fieldName,
		hoursopacity: 1,
		minutesopacity: 1
	});


}

		
// Prep the setup routine
//$(document).ready(appSetup);
//$(document).ready(loadInBody);

