// Newsky Group (c) 2007 - Tutti i diritti riservati
// Ultima revisione: 12/11/2006

if ( document.location.search.length > 0 )
{ alert("Si è verificato un errore. :-(") ; location.replace(location.href.indexOf("donzelle") == -1 ?  "fanciulli.htm" : "donzelle.htm") }

function opt(text, value)
{
if ( text == null || value == null ) { text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" ; value = "|" }
document.write("<option value='" + value + "'>" + text + "</option>")
}

function initialize()
{
mod = document.modulo
R1m = R2m = R3m = R4m = mod.D1m.value = mod.D2m.value = mod.D3m.value = mod.D4m.value = "|"
R1f = R2f = R3f = R4f = mod.D1f.value = mod.D2f.value = mod.D3f.value = mod.D4f.value = "|"
flagPts = 0
if ( IE )
{ drawHeartIE(0,4)
window.setTimeout("document.getElementById('h0').outerHTML = \"<img id='h0' border='0' src='_/0.gif' width='30' height='30'>\"",160) }
else
{ drawHeartFx(1,0.25)
window.setTimeout("drawHeartFx(0,0)",160) }
}

function calculate()
{
if ( document.modulo.D0m.value == "" || document.modulo.D0f.value == "" ) { alert("Non hai inserito i vostri due nomi!") }
else if ( R1m == "|" || R2m == "|" || R3m == "|" || R4m == "|" || R1f == "|" || R2f == "|" || R3f == "|" || R4f == "|" ) { alert("Non hai risposto a tutte le domande!") }
else
{
document.getElementById("heartsx").innerHTML = '<img id="h0" border="0" src="_/0.gif" width="30" height="30"><img id="h1" border="0" src="_/0.gif" width="30" height="30"><img id="h2" border="0" src="_/0.gif" width="30" height="30"><img id="h3" border="0" src="_/0.gif" width="30" height="30"><img id="h4" border="0" src="_/0.gif" width="30" height="30"><img id="h5" border="0" src="_/0.gif" width="30" height="30"><img id="h6" border="0" src="_/0.gif" width="30" height="30"><img id="h7" border="0" src="_/0.gif" width="30" height="30"><img id="h8" border="0" src="_/0.gif" width="30" height="30"><img id="h9" border="0" src="_/0.gif" width="30" height="30">'
scrollTo(0,1500)
getFlags()
score = 100 + (40-Math.abs(R2m-R2f)) + Math.abs(R3m-R3f) + Math.abs(R4m-R4f) + flagPts
mult = 0.25+0.75/Math.exp(Math.pow((R1m < R1f ? R1f/R1m-0.85 : R1m/R1f-1.15)/0.8,2)/2)
score = Math.floor(20*(1+Math.atan((score*mult-125)/85)))/4
document.getElementById("verdict").innerHTML = "Cari <b>" + document.modulo.D0m.value + "</b> e <b>" + document.modulo.D0f.value + "</b>, la vostra è una <span id='levx'>affinità " + levels[Math.floor(score)][0] + "</span>&nbsp;(<a href='javascript:levelsList(" + Math.floor(score) + ")'>a che livello siamo?</a>)"
document.getElementById("commx").innerHTML = levels[Math.floor(score)][1]
document.getElementById("endx").innerHTML = getEnd(score)
drawHearts(score+0.25)
}
}

function getFlags()
{
flags = [ 3,3,3,3,2,2,2,1,1,2,2,1,1,2,2,1,1,-5,-2,-3,-2,-1,-3,-2,-1,-2,-1,-2,-2,-2,-2,-1,-2,-1,-2,-1,-2,-2,-1,-1,-2,-1,-1,-2,-1 ]
flagPts = 0
for ( i = 1 , j = 0 ; i <= 17 ; i++ , j++ )
{ flagPts += flags[j] * ( eval("mod.m"+(i<10?"0"+i:i)+".checked") + eval("mod.f"+(i<10?"0"+i:i)+".checked") ) }
for ( i = 21 ; i <= 48 ; i++ , j++ )
{ flagPts += flags[j] * ( eval("mod.m"+i+".checked") + eval("mod.f"+i+".checked") ) }
}

function getEnd(score)
{
if ( score >= 7.75 )
ends = happyending
else if ( location.href.indexOf("donzelle") == -1 )
ends = ends4him
else
ends = ends4her
hash = 0
for ( i = 1 ; i <= 17 ; i++ )
{ hash += i * ( eval("mod.m"+(i<10?"0"+i:i)+".checked") + 2 * eval("mod.f"+(i<10?"0"+i:i)+".checked") ) }
for ( i = 21 ; i <= 48 ; i++ )
{ hash += i * ( eval("mod.m"+i+".checked") + 2 * eval("mod.f"+i+".checked") ) }
hash += 9 * mod.D1m.value + 18 * mod.D2m.value + 27 * mod.D3m.value + 36 * mod.D4m.value + 3 * mod.D1f.value + 4 * mod.D2f.value + 5 * mod.D3f.value + 6 * mod.D4f.value
if ( score >= 7.75 )
return ends[hash%ends.length]
else
return "Finirà " + ends[hash%ends.length] + "."
}

function drawHearts(num)
{
if ( IE )
switch ( Math.floor(num) )
{
case 10: window.setTimeout("drawHeartIE(9,4)",1440)
case 9: window.setTimeout("drawHeartIE(8,4)",1280)
case 8: window.setTimeout("drawHeartIE(7,4)",1120)
case 7: window.setTimeout("drawHeartIE(6,4)",960)
case 6: window.setTimeout("drawHeartIE(5,4)",800)
case 5: window.setTimeout("drawHeartIE(4,4)",640)
case 4: window.setTimeout("drawHeartIE(3,4)",480)
case 3: window.setTimeout("drawHeartIE(2,4)",320)
case 2: window.setTimeout("drawHeartIE(1,4)",160)
case 1: drawHeartIE(0,4)
default: window.setTimeout("drawHeartIE(" + Math.floor(num) + "," + (num%1)*4 + ")",160*Math.floor(num))
}
else
{ drawHeartFx(num,0.25) }
}

function drawHeartIE(heart,parts)
{
switch ( parts )
{
case 4: window.setTimeout("document.getElementById('h" + heart + "').outerHTML = \"<img id='h" + heart + "' border='0' src='_/4.gif' width='30' height='30'>\"",120)
case 3: window.setTimeout("document.getElementById('h" + heart + "').outerHTML = \"<img id='h" + heart + "' border='0' src='_/3.gif' width='30' height='30'>\"",80)
case 2: window.setTimeout("document.getElementById('h" + heart + "').outerHTML = \"<img id='h" + heart + "' border='0' src='_/2.gif' width='30' height='30'>\"",40)
case 1: document.getElementById("h"+heart).outerHTML = "<img id='h" + heart + "' border='0' src='_/1.gif' width='30' height='30'>"
default: return
}
}

function drawHeartFx(num,i)
{
if ( i > num ) return
text = ""
for ( j = 0 ; j < Math.floor(i) ; j++ ) { text += "<img border='0' src='_/4.gif' width='30' height='30'>" }
if ( i%1 != 0 ) { text += "<img border='0' src='_/" + (i%1)*4 + ".gif' width='30' height='30'>" }
for ( j = 0 ; j < 10-Math.ceil(i) ; j++ ) { text += "<img border='0' src='_/0.gif' width='30' height='30'>" }
window.setTimeout("document.getElementById('heartsx').innerHTML = text;drawHeartFx("+num+","+(i+0.25)+")",40)
}

/*
function attestation()
{
if ( !done ) { alert("Devi chiedere il verdetto prima!") }
else
{
q = score
window.open("richiesta.htm?" + q,"richiesta","height=295,width=600,status=no,scrollbars=no,toolbar=no,menubar=no,location=no,resizable=no")
}
}
*/

function levelsList(level)
{
n = level == null ? "" : "?" + level
window.open("livelli.htm"+n,"livelli","height=275,width=250,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no,resizable=no")
}