var controllerurl="/calculate.php";
var addinbasketurl="/add.php";

function cba(act) {
	var basketform = document.getElementById('basketform');
	if(act == "checkout") {
		/* questa parte serve per controllare se si sono accettate le condizioni di vendita
		var ck = document.getElementById('termsofsale').checked;
		if (!ck) { 
			document.getElementById('notermsofsale').style.visibility="visible";
			return false;
		} else {*/
			basketform.action = "/checkout.php"
			basketform.submit();
		/*}*/
	}	
	if(act == "aggiorna") {
		basketform.action = "/basket.php"
		basketform.submit();
	}		
}
/*
function processLinks(name,value) {	//aggiunge un parametro a una qs...
	var myescape = self.encodeURIComponent ? encodeURIComponent : escape;
	var toadd = name+'='+myescape(value);
	var op = '';
 
 	//Aggiunge alle form
	for ( var i=0; i<document.forms.length; i++ ) {
  		op = '?';
  		if ( document.forms[i].action.match(/\?/g) )
   			op = '&';
  		document.forms[i].action += op+toadd;
 	}
 
 	//Aggiunge ai link
 	for ( i=0; i<document.links.length; i++ ) {
		op = '?';
  		if ( document.links[i].href.match(/\?/g) )
   			op = '&';
  		document.links[i].href += op+toadd;
	}
}
*/


function setsBookmark(service, titolo) {
	var questapagina = document.URL;
	var titolo = titolo + '(www.waf.it)';
	switch(service) {
		case 'favorites':
			if (window.sidebar) {
    			// versione FF
    			window.sidebar.addPanel(document.title, location.href,'');
			} else if( window.external ) {
    			// versione IE
    			window.external.AddFavorite( location.href, document.title);
			}
			break;
		case 'google':
			burl = 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' + questapagina + '&title=' + titolo;
			break;
		case 'delicious':
			burl = 'http://del.icio.us/post?url='+ questapagina +'&title=' + titolo;
			break;
		case 'digg':
			burl = 'http://digg.com/submit?phase=2&url=' + questapagina + '&title=' + titolo;
			break;
		case 'facebook':
			burl = 'http://www.facebook.com/sharer.php?u=' + questapagina + '&t=' + titolo;
			break;
		case 'furl':
			burl = 'http://www.furl.net/storeIt.jsp?t=' + titolo + '&u=' + questapagina;
			break;
		case 'myweb':
			burl = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?t=' + titolo + '&u=' + questapagina;
			break;
		case 'technorati':
			burl = 'http://technorati.com/faves/?add=' + questapagina;
			break;
		case 'live':
			burl = 'https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=' + questapagina + '&title=' + titolo + '&top=1';
			break;
		case 'yahoobkm':
			burl = 'http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=' + questapagina + '&t=' + titolo;
			break;
		case 'ask':
			burl = 'http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url=' + questapagina + '&title=' + titolo;
			break;
		case 'aolfav':
			burl = 'https://my.screenname.aol.com/_cqr/login/login.psp?sitedomain=my.aol.com&authLev=0&lang=en&locale=us&siteState=OrigUrl=http%3A%2F%2Ffavorites.my.aol.com%2Fffclient%2Fwebroot%2F0.3.1%2Fsrc%2Fhtml%2FaddBookmarkDialog.html%3Furl%3D' + questapagina;
			break;
		case 'diigo':
			burl = 'http://www.diigo.com/post?url=' + questapagina + '&title=' + titolo;
			break;
		case 'segnalo':
			burl = 'http://segnalo.alice.it/login.html.php?uri=/post.html.php?url=' + questapagina + '&title=' + titolo;
			break;								
	}
	window.open(burl);
}

/* ShowHide dettagli */

function getEl(id)
{
    element = document.getElementById(id);
    return element;
}

function hideEl(id)
{
    var element = getEl(id);
    element.style.display = 'none';
}

function showEl(id)
{
    var element = getEl(id);
    element.style.display = '';
}

function ShowHide(sid, last)
{
    var el = 'info_' + sid;
    if(getEl(el).style.display == 'none') {
        var class_name = (last == true) ? 'meno_info ultimo' : 'meno_info';
        showEl(el);
        getEl('p_' + sid).className = class_name;
    } else {
        var class_name = (last == true) ? 'piu_info ultimo' : 'piu_info';
        hideEl(el);
        getEl('p_' + sid).className = class_name;
    }
}
/*************** Test Cookie ********************/
function getCookie(name)
{ var pos
  var token = name + "=";
  var tnlen = token.length;
  var cklen = document.cookie.length;
  var i = 0;
  var j;
  while (i < cklen)
  { j = i + tnlen;
    if (document.cookie.substring(i, j) == token)
    { pos = document.cookie.indexOf (";", j);
      if (pos == -1)
        pos = document.cookie.length;
      return unescape(document.cookie.substring(j, pos));
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  } //End While
  return null;
}
function setCookie(name, value)
{ document.cookie = name + "=" + escape(value)
}
function deleteCookie(name)
{ var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = getCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

}
function cookieIsEnabled() {
	var gCookieName;
	var gCookieValueRet;
	gCookieName="STBCOOKIETEST";
	gCookieValue="STB_TESTING_COOKIE";
	gCookieValueRet="???"
	setCookie(gCookieName,gCookieValue,1);
	gCookieValueRet=getCookie(gCookieName);
	if (gCookieValueRet == null) 
		return 0;
	else {
		deleteCookie(gCookieName);//cancella il cookie di test
		return 1;
	}	
}

/*************** Controllo Privacy ********************/

function checkPrivacy() {
	var found = false;
	var pay = document.getElementById('pay').selectedIndex;
	var ck = document.getElementById('privacy').checked;
	var vh = document.getElementById('visithour').checked;
	var email = document.getElementById('email').value;
	var c_email = document.getElementById('c_email').value;
	var name = document.getElementById('name').value;
	var surname = document.getElementById('surname').value;
	var phone = document.getElementById('phone').value;
	var address = document.getElementById('address').value;
	var town = document.getElementById('town').value;
	var country = document.getElementById('country').selectedIndex;
	var zip = document.getElementById('zip').value;
	var codice_fiscale = document.getElementById('codice_fiscale').value;
	
	document.getElementById('pay').style.borderColor='';
	document.getElementById('formerrors').style.visibility="hidden";	
	document.getElementById('email').style.borderColor='';
	document.getElementById('c_email').style.borderColor='';
	document.getElementById('name').style.borderColor='';
	document.getElementById('surname').style.borderColor='';
	document.getElementById('phone').style.borderColor='';
	document.getElementById('address').style.borderColor='';
	document.getElementById('town').style.borderColor='';
	document.getElementById('country').style.borderColor='';
	document.getElementById('zip').style.borderColor='';
	document.getElementById('codice_fiscale').style.borderColor='';
	document.getElementById('privacyckeck').style.backgroundColor='';
	document.getElementById('visithourckeck').style.backgroundColor='';
	
	if(!pay) {
		document.getElementById('pay').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}
	
	if(!email) {
		document.getElementById('email').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}
	if(!isValidEmail(email)) {
		document.getElementById('email').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}	
	if(email != c_email) {
		document.getElementById('email').style.borderColor='red';
		document.getElementById('c_email').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}	
	if(!name) {
		document.getElementById('name').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}
	if(!surname) {
		document.getElementById('surname').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}	
	if(!phone) {
		document.getElementById('phone').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}
	if(!address) {
		document.getElementById('address').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}	
	if(!town) {
		document.getElementById('town').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}
	if(!country) {
		document.getElementById('country').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}
	if(!zip) {
		document.getElementById('zip').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}
	if(country == 99 && !codice_fiscale) {
		document.getElementById('codice_fiscale').style.borderColor='red';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	}	
	if (!ck) {
		document.getElementById('privacyckeck').style.backgroundColor ='#FF0000';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	} 
	if (!vh) {
		document.getElementById('visithourckeck').style.backgroundColor ='#FF0000';
		document.getElementById('formerrors').style.visibility="visible";
		found = true;
		//return false;
	} 	
	if(found)
		return false;	
	
}

function isValidEmail(email) {
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")	
	if (AtPos == -1 || StopPos == -1) {
		return false;
	}
	return true;
}


