/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

function toUpper(txt){
	txt.value = txt.value.toUpperCase();
	return;
}


function countChar(txt,char) {

	count = 0;
	
	for (i=0; i<txt.value.length; i++) {
		if (txt.value.charAt(i) == char) {
			count += 1;
		}
	}

	return count;
}

function checkDate(txtDate) {

	appoDate = '';
	
	if ((txtDate.value.length != 10) && (txtDate.value.length != 8)) {
		alert('Formato della Data errato.');
		txtDate.focus();
		return false;
	}

	for (i=0;i<txtDate.value.length;i++) {
		if (isNaN(txtDate.value.charAt(i)) && (txtDate.value.charAt(i) != '/' )) {
			alert('Formato Data non valido.');
			txtDate.focus();
			return false;
		}
	}
		
	if (((countChar(txtDate,'/') != 2) && (txtDate.value.length == 10)) ||
	    ((countChar(txtDate,'/') != 0) && (txtDate.value.length == 8))){
			alert('Formato Data non valido.');
			txtDate.focus();
			return false;
	}

	if (txtDate.value.length == 8) {
		appoDate = txtDate.value.slice(0,2) + '/';
		appoDate += txtDate.value.slice(2,4) + '/';
		appoDate += txtDate.value.slice(4,8);
		txtDate.value = appoDate;
	}
	
	return true;
	
}

function checkEmail(email) {
	
	if (email.value == '') {
		return true;
	}
	
	posEt = email.value.indexOf('@');
	
	if (posEt != -1) {
		domain = email.value.substr(posEt+1);
		posDot = domain.indexOf('.');
		if (posDot != -1) {
			return true;		
		}
	}
	
	alert("E-mail errata.");
	email.select();email.focus();
	return false;
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	if (textBox.value.length > 0 && isNaN(textBox.value)) {
		alert('Il campo accetta solo valori numerici.');
		textBox.value = 0;
		textBox.select();textbox.focus();
		return false;
	}
	return true;
}

function rpad(str,chr,num) {
	w_appo = str;
	
	for (i=0;i<num;i++) {
		w_appo = w_appo.concat(chr);
	}
	return w_appo;
}

function checkPrezzo(textBox,dec) {
	w_dec = '';
	w_pos = textBox.value.indexOf('.');
	
	if (!checkNumber(textBox)) {
		return false;
	}
	
	if (dec == 0) {
		textBox.value = Math.round(textBox.value);
	} else {
		if (w_pos == -1) {
			textBox.value = rpad(textBox.value+'.','0',dec);
		} else {
			w_dec = textBox.value.substr(w_pos+1);
			if (w_dec.length < dec) {
				textBox.value = rpad(textBox.value,'0',dec);
			} else {
				textBox.value = textBox.value.substr(0,w_pos+1) + w_dec.substr(0,dec);
			}
		}
	}
}


/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

function loadPage(page,myForm,lastPage) {
	with (myForm) {
		if (isNaN(page)) {
			page = currentPage.value;
		}
		if (page < 1) {
			page = 1;
		} else if (page > lastPage) {
			page = lastPage;
		}
		goPage.value = page;
		submit();
	}
}

function openResidenza(myForm) {
	var getString = '';
	if (myForm.name == 'frmRegister') {
		with (myForm) {
			getString += '?frm=frmRegister';
			getString += '&societa=' + societa.value;
			getString += '&cognome=' + cognome.value;
			getString += '&nome=' + nome.value;
			getString += '&dtNascita=' + dtNascita.value;
			if (sesso[0].checked) {
				getString += '&sesso=M';
			} else {
				getString += '&sesso=F';
			}
			getString += '&email=' + email.value;
			getString += '&CF_PI=' + CF_PI.value;
			getString += '&username=' + username.value;
			getString += '&password=' + password.value;
			getString += '&conferma=' + conferma.value;
			getString += '&indirizzo=' + indirizzo.value;
			getString += '&action=get';
		}
	} else if (myForm.name == 'frmCheckout') {
		with (myForm) {
			getString += '?frm=frmCheckout';
			getString += '&cognome=' + txtPaymentLastName.value;
			getString += '&nome=' + txtPaymentFirstName.value;
			getString += '&indirizzo=' + txtPaymentAddress1.value;
		}
	}
	window.open ('residenza.php'+getString, 'newWin', 'width=500,height=160');
}

function loadComuni(theForm,view,getList){
	catList = theForm.cboProvincia;
	if (catList.selectedIndex == 0) {
		window.location.href = 'index.php?view='+view;
	} else {
		if (!getList) {
			window.location.href = 'index.php?view='+view+'&codProv=' + catList.options[catList.selectedIndex].value;
		} else {
			window.location.href = 'index.php?view='+view+'&codProv=' + catList.options[catList.selectedIndex].value + getList;
		}
	}
}

function viewPhoto(p_image,p_desc) {
	var getString = '?act=viewPhoto&image=' + p_image + '&desc=' + p_desc;
	//window.location.href = 'info.php'+getString;
	window.open('info.php'+getString);
}

function isCachet(formObj,checkObj) {

	if (checkObj.value == 'S') {
		formObj.hidCachet.value = 'S';
	} else {
		formObj.hidCachet.value = 'N';
	}
	formObj.hidShipmentType.value = checkObj.value;
	formObj.action = formObj.action.replace('update','view');
	formObj.submit();
}

function addToCart(productId,cboObj) {
	var specialValue = '';
	
	for (var i=0; i<cboObj.options.length; i++) {
		if (cboObj.options[i].selected) {
			specialValue = '&sp=' + cboObj.options[i].value;
		}
	}
	window.location.href = 'cart.php?action=add&p=' + productId + specialValue;
}
