/*
	utility functions
	creator: name: Luca Cervone
		 e-mail: lcervone@cs.unibo.it
		 tel: +39 348 2627545
		
*/

//if the string given as aString is empty  then returns true
function isEmptyString(aString)
{
	if (aString == "")
	{
		return true;
	}
	else
	{
		return false;
	}
}

//if the first param is contained in the second param returns true
function isObjectInArray(anObject,anArray)
{
	var i;
	var returnValue = false;
	for (i=0;i<anArray.length;i++)
	{
		if (anArray[i] == anObject)
		{
			returnValue = true;
			break;
		}
	}
	return returnValue;
}

//if the string rapresent an integer return true
function isInteger(value)
{
	var i;
	for (i = 0; i < value.length; i++) 
	{
		var c = value.charAt(i);
		if (!((c >= "0") && (c <= "9"))) 
		{
			return false;
		}
	}
	return true;
}

//when a useser click on an element the element calls this function that deselect the selected element and select himself
function selectElement(anElement)
{
	try
	{
		window.top.document.getElementById('properbutton').click();
		var bodyTable = document.getElementById('bodytable');
		var i;
		for (i=0;i<bodyTable.firstChild.childNodes.length;i++)
		{
			if (bodyTable.firstChild.childNodes[i].style.backgroundColor == "rgb(204, 255, 153)")
			{
				bodyTable.firstChild.childNodes[i].style.backgroundColor = "white";
				var k;
				for (k=0;k<bodyTable.firstChild.childNodes[i].childNodes.length;k++)
				{
					if (bodyTable.firstChild.childNodes[i].childNodes[k].hasAttribute("dragger"))
					{
						bodyTable.firstChild.childNodes[i].childNodes[k].style.visibility = "hidden";
					}
				}
			}
		}
	
		if (anElement.id)
		{
			anElement.style.backgroundColor="#CCFF99";
			var k;
			for (k=0;k<anElement.childNodes.length;k++)
			{
				if (anElement.childNodes[k].hasAttribute("dragger"))
				{
					anElement.childNodes[k].style.visibility = "visible";
				}
			}
			if (anElement.nextSibling.getAttributeNode('parentid') && anElement.nextSibling.getAttributeNode('parentid').nodeValue == anElement.id)
			{
				anElement.nextSibling.style.backgroundColor="#CCFF99";
			}
			if (anElement.nextSibling.nextSibling.getAttributeNode('parentid') && anElement.nextSibling.nextSibling.getAttributeNode('parentid').nodeValue == anElement.id)
            {
                anElement.nextSibling.nextSibling.style.backgroundColor="#CCFF99";
            }
		}
		else if (anElement.getAttributeNode('parentid'))
		{
			selectElement(document.getElementById(anElement.getAttributeNode('parentid').nodeValue));
		}
	}
	catch (err)
	{
		return;
	}
}

//checks the forms value
function check(aButton)
{
	
	var inputs = document.getElementsByTagName('input');
	var notinsertedstring = 'SORRY!\n';
	var i;
	/*if(document.getElementById('biciprotectionofpersonaldata') && !document.getElementById('biciprotectionofpersonaldata').checked)
	{
		notinsertedstring = notinsertedstring + '"I Agree" must be checked\n';
	}*/
	for (i=0;i<inputs.length;i++)
	{
		if (inputs[i].getAttribute('contains') && !(inputs[i].getAttribute('contains') == 'email' || inputs[i].getAttribute('contains') == 'confirmedemail' ))
		{
			if (!((inputs[i].getAttribute('id') && inputs[i].getAttribute('id') == 'mybiciinvoice') || (inputs[i].getAttribute('id') && inputs[i].getAttribute('id') == 'mybiciinvoiceaddress') || (inputs[i].getAttribute('id') && inputs[i].getAttribute('id') == 'mybiciinvoicetnumber')))
			{
				if (inputs[i].getAttribute('required') && inputs[i].getAttribute('required') == 'true' && inputs[i].value == '')
				{
					notinsertedstring = notinsertedstring + inputs[i].parentNode.previousSibling.firstChild.firstChild.nodeValue + ' is required\n'
				}
			}
			else
			{
				if (inputs[i].getAttribute('id') && inputs[i].getAttribute('id') == 'mybiciinvoice' && inputs[i].value == '')
				{
					notinsertedstring = notinsertedstring + 'Invoice issued to: is required\n';
				}
				else if (inputs[i].getAttribute('id') && inputs[i].getAttribute('id') == 'mybiciinvoiceaddress' && inputs[i].value == '')
				{
					notinsertedstring = notinsertedstring + 'Address of above subject: is required\n';
				}
				else if (inputs[i].getAttribute('id') && inputs[i].getAttribute('id') == 'mybiciinvoicetnumber' && inputs[i].value == '')
				{
					notinsertedstring = notinsertedstring + 'Tax number (or equivalent) of above subject: is required\n';
				}
			}
		}
		else if (inputs[i].getAttribute('contains') && inputs[i].getAttribute('contains') == 'email' && inputs[i].value == '')
		{
			notinsertedstring = notinsertedstring + 'e-mail is required\n';
		}
		else if (inputs[i].getAttribute('contains') && inputs[i].getAttribute('contains') == 'confirmedemail' && inputs[i].value == '')
		{
			notinsertedstring = notinsertedstring + 'confirm e-mail is required\n';
		}
		if (inputs[i].getAttribute('id') && inputs[i].getAttribute('id') == 'bicibertinoropriceforanyevent' && inputs[i].value == '')
		{
			notinsertedstring = notinsertedstring + 'Amount to be paid is required\n';
		}
		/*if (inputs[i].getAttribute('id') && inputs[i].getAttribute('id') == 'bicibertinoropriceforanyevent' && !isInteger(inputs[i].value))
		{
			notinsertedstring = notinsertedstring + 'Amount to be paid must be an integer\n';
		}*/
	}
	if((document.getElementById('mybiciemail') && document.getElementById('mybiciemailok')) && (document.getElementById('mybiciemail').value != document.getElementById('mybiciemailok').value))
	{
		notinsertedstring = notinsertedstring + 'e-mail and confirm e-mail must be the same\n';
	}
	var tareas = document.getElementsByTagName('textarea');
	for (i=0;i<tareas.length;i++)
	{
		if (tareas[i].getAttribute('required') && tareas[i].getAttribute('required') == 'true' && tareas[i].value == '')
		{
			notinsertedstring = notinsertedstring + tareas[i].parentNode.previousSibling.firstChild.firstChild.nodeValue + ' is required\n'
		}
	}
	for (i=0;i<inputs.length;i++)
	{
		if (inputs[i].getAttribute('contains'))
		{
			if (inputs[i].getAttribute('contains') == 'string' && parseInt(inputs[i].value))
			{
				notinsertedstring = notinsertedstring + inputs[i].parentNode.previousSibling.firstChild.firstChild.nodeValue + ' requires a string\n'
			}
			if (inputs[i].getAttribute('contains') == 'numeric' && !isInteger(inputs[i].value))
			{
				notinsertedstring = notinsertedstring + inputs[i].parentNode.previousSibling.firstChild.firstChild.nodeValue + ' requires an integer\n'
			}
			if (inputs[i].getAttribute('contains') == 'email')
			{
				var emailvalue = inputs[i].value;
				if(!emailvalue.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.it)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi))
				{
					notinsertedstring = notinsertedstring + 'you must insert a valid e-mail address\n';
				}
			}
		}
	}
	if (notinsertedstring != 'SORRY!\n')
	{
		//alert(notinsertedstring);
		var coverDiv = document.createElement('div');
		coverDiv.setAttribute('id','submitcoverdiv')
		
		var myInner = '<a style="border:solid thin green; cursor:pointer; text-decoration:none; font-weight:bold; font-size:12pt; color:green; font-family:Times;"  onClick="document.getElementsByTagName(\'body\')[0].removeChild(document.getElementById(\'submitcoverdiv\'));">Close</a><br><center>';;
		var lines = 0;
		var splitted = notinsertedstring.split('\n');
		for (i in splitted)
		{
			if (!(i == splitted.length - 1))
			{
				myInner = myInner + '<span>' + splitted[i] + '</span>' + '<br>';
				lines++;
			}
			else
			{
				myInner = myInner + '<span>' + splitted[i] + '</span>' + '<br>';
				lines++;
			}
		}
		var divHeight = (lines + 1) * 20;
		var divTop = document.getElementById('bodytable').offsetHeight - divHeight + 23;
		var divLeft = (document.getElementById('bodytable').offsetWidth / 2)- 125;
		
		
		//coverDiv.setAttribute("style","height:" + divHeight + "px; width:250px; color:red; text-align:center; border:solid thin black; background-color:#e2e2e2; position:absolute; top:" + divTop + "; left:" + divLeft);
		coverDiv.style.height = divHeight;
		coverDiv.style.width = '250px';
		coverDiv.style.color = 'red';
		coverDiv.style.border = 'solid thin black';
		coverDiv.style.backgroundColor = '#e2e2e2';
		coverDiv.style.position = 'absolute';
		coverDiv.style.top = divTop;
		coverDiv.style.left = divLeft;
		coverDiv.innerHTML = myInner + '</center>';
		document.getElementsByTagName('body')[0].appendChild(coverDiv);
	}
}

//send the the form values
function send()
{
	var link = 'http://bici.nws.cs.unibo.it/cgi-bin/formsmaker/wolfsubmit.php5?' 
	var inputs = document.getElementsByTagName('input');
	var i;
	for(i=0;i<inputs.length;i++)
	{
		if (inputs[i].getAttributeNode('type').nodeValue != 'button' && inputs[i].getAttributeNode('type').nodeValue != 'reset')
		{
			link = link + inputs[i].getAttributeNode('name').nodeValue + '=' + inputs[i].getAttributeNode('value').nodeValue + '&';
		}
	}
	document.location = link;
}



//redirect to bank
function checkPayment()
{

}

function removeSubmitCover()
{
	//if(document.getElementById('submitcoverid'))
	//{
		document.getElementsByTagName('body')[0].removeChild(document.getElementsByid('submitcoverdiv'));
	//}
}


