// Browser detection
// Internet Explorer
var ie = document.all != null; //ie4 and above
var ie5 = document.getElementById && document.all;
var ie6 = document.getElementById && document.all&&(navigator.appVersion.indexOf("MSIE 6.")>=0);       
            
// Firefox
var ff = !document.layers && !document.all;

// Opera
var op = navigator.userAgent.indexOf("opera")>0;
var op7 = op && operaVersion() <= 7;
var op8 = op && operaVersion() >= 8;

//alert("ie5"+ie5+"ie6"+ie6+" ff"+ff);

function submitform()
{
	  error=false;
	  txtelements=document.getElementsByTagName("input");
		for(i=0;i<txtelements.length;i++)
		{
			//alert(txtelements[i].type+" " +txtelements[i].id+" "+txtelements[i].value)
			if(txtelements[i].id.indexOf("tbox")!=-1)
			{			
				if(txtelements[i].value=="" || parseInt(txtelements[i].value)==0)
				{
					document.getElementById("currentSpendError").style.display="block";
					document.getElementById("maxlimit").style.display="none";
					txtelements[i].focus();
					error=true;
					break;
				}else if(isNaN(parseInt(txtelements[i].value)))
				{	
					document.getElementById("currentSpendError").style.display="block";
					document.getElementById("maxlimit").style.display="none";
					txtelements[i].focus();
					error=true;
					break;
				}
				if(parseInt(txtelements[i].value)< 0)
				{	
					document.getElementById("currentSpendError").style.display="block";
					document.getElementById("maxlimit").style.display="none";
					txtelements[i].focus();
					error=true;
					break;
				}	
				if(parseInt(txtelements[i].value) > 99999)
				{
					document.getElementById("maxlimit").style.display="block";
					document.getElementById("currentSpendError").style.display="none";
					txtelements[i].focus();
					error=true;
					break;
				}
				
			}
			
		}	
	
	if(!error)	
	document.calculator.submit();
}

function isInteger(s) {
	var regEx = /^\d+$/;
	return regEx.test(s);
}

function isFloat(s) {
//	var regEx = /^\d+$/;
var regEx=/^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
	return regEx.test(s);
}



/* page1 */

var counttext;
var	boxId=2;

function additem(itemname)
{
	newBoxName=null;
	//alert(itemname);
	if(itemname!="")
	{
		customValue=itemname;
	 	blankIndex=customValue.indexOf(" ");		
		if(blankIndex>0)
			newBoxName=customValue.substring(0,blankIndex);		
		else
			newBoxName=customValue	;							
	}else		
	{
		customValue=document.getElementById("newitemtext").value		
	 	blankIndex=customValue.indexOf(" ");
		if(blankIndex>0)
			newBoxName=customValue.substring(0,blankIndex);
		else
			newBoxName=customValue;
	}	
	
	//alert(newBoxName);
	
	newdiv=document.createElement("div");
	if(document.getElementById(newBoxName))
	{	
		newBoxName=newBoxName+"1";
		newdiv.id=newBoxName;
	}
	else
		newdiv.id=newBoxName;
	
	//newdiv.style.cssText =	"";
	newdiv.className="addtextfieldnew ";
	//newdiv.cssText="cursor:move;clear:both;";
	newdiv.innerHTML=customValue;
	newnode=document.createTextNode("\n\n");
	document.getElementById("dropContent").appendChild(newnode);	
	document.getElementById("dropContent").appendChild(newdiv);	
	
	
	//alert(newBoxId);
	dragDropObj.addSource(newBoxName,true,true,true,false,'onDragFunction');
	dragDropObj.addTarget('cart','dropItems');	// Set <div id="cart"> as a drop target. Call function dropItems on drop
	dragDropObj.addTarget('product-items','dropItems'); // Set <div id="product-items"> as a drop target. Call function dropItems on drop
	dragDropObj.init();
	
}


//<!--
function dropItems(idOfDraggedItem,targetId,x,y)
{
	//alert(idOfDraggedItem+" "+targetId);
	
	keyArr = new Array('magazine','music','clothes','cinema','Makeup','lunchtimes','takeaways','Restaurants','Coffee','Smoking','ThePub','Clubbing','Books','Treats');
	
	isStandardItem=false;
	var cht=document.getElementById('dropContent2').getAttribute("height");
	
	if(cht==null||typeof cht == null)
	 cht="42px";
	//console.log("CHT>"+cht);
	/*document.getElementById('dropContent2').style.height=parseInt(cpos)+32+"px";
	document.getElementById('dropContent2').setAttribute("height",(parseInt(cpos)+32+"px"));*/
	
	for(i=0;i<keyArr.length;i++)
	{
			if(keyArr[i]==idOfDraggedItem)
			{
				isStandardItem=true;
				break;
			}
			
	}	
	
	if(targetId=='cart'){	// Item dropped on <div id="cart">
		var obj = document.getElementById(idOfDraggedItem);
		
	if(obj.parentNode.id=='dropContent2')return;		
		
	
	
		if(isStandardItem)
		{
			//alert("stand item")
			obj.className="dragableBoxRight";			
		}	
		else
		{
			//alert("new item")
			obj.className="addtextfieldsecond";
			obj.style.cssText="";
		 }		
		 
		document.getElementById("dropfritter").style.display="none";
		document.getElementById("startsavinglink").style.display="block";
		document.getElementById("takeaguess").style.display="block";
		
		document.getElementById('dropContent2').appendChild(obj);	// Appending dragged element as child of target box
		
	}
	
	
	/*alert(isStandardItem);
	alert(targetId+" "+idOfDraggedItem);*/
	
	
	if(targetId=='product-items'){	// Item dropped on <div id="product-items">
		var obj = document.getElementById(idOfDraggedItem);
		if(obj.parentNode.id=='dropContent') return;	
		
			if(isStandardItem)
				obj.className="products";			
			else
				obj.className="products addtextfield2";
				
		document.getElementById('dropContent').appendChild(obj);	// Appending dragged element as child of target box
	}	
	
	var cpos=cht.substr(0,cht.indexOf('p'));	
	//console.log(cht+"--"+cpos);	
	
	if(!isAdded(idOfDraggedItem))
	{		
		generateTextBox(idOfDraggedItem);		
	}else
	{
		document.getElementById('dropContent2').style.height=parseInt(cpos)-32+"px";
		removeTextBox(idOfDraggedItem);		
	}
}
function isAdded(idOfDraggedItem)
{
	//uid=idOfDraggedItem.charAt(idOfDraggedItem.length-1);
	var uid=idOfDraggedItem;
	try{
		if(document.getElementById("tbox"+uid))	
			return true;
	}catch(e)
	{
		return false;	
	}
	
}


function evalValue()
{
	//obj.setAttribute('autocomplete','off'); 
	var val=this.value;
	isIntFlag=isFloat(val);
	calcTotal();
	
	if((val != "") && (isIntFlag))
	{				
				document.getElementById("currentSpendError").style.display="none";
	}
	else
	{		


			document.getElementById("currentSpendError").style.display="";
			this.focus();
			return false;
	}
}

function formatspend()
{
	  txtelements=document.getElementsByTagName("input");
	  for(i=0;i<txtelements.length;i++)
		{
			//alert(txtelements[i].type+" " +txtelements[i].id+" "+txtelements[i].value)
			if(txtelements[i].id.indexOf("tbox")!=-1)
			{				
					if(!isNaN(parseInt(txtelements[i].value)))
				{	
					roundvalue=Math.round(txtelements[i].value);					
						if(roundvalue==0)
							txtelements[i].value="";	
						else if(!isNaN(roundvalue))					
							txtelements[i].value=roundvalue;												
				}else
				{
					txtelements[i].value="";	
				}
			}
		}	
}
function calcTotal()
{
	var spend;
	var currentweeklySpend=0;
	var currentannualSpend=0;
	   txtelements=document.getElementsByTagName("input");
		for(i=0;i<txtelements.length;i++)
		{
			//alert(txtelements[i].type+" " +txtelements[i].id+" "+txtelements[i].value)
			if(txtelements[i].id.indexOf("tbox")!=-1)
			{				
					if(!isNaN(parseInt(txtelements[i].value)))
				{	
							if(currentannualSpend==0)
							currentannualSpend=1;
							
							currentweeklySpend+=parseInt(txtelements[i].value);
							currentannualSpend=52 * currentweeklySpend;
				}	
			}
		}	
		document.getElementById("currentAnnualSpenAmount").innerHTML="&pound; "+parseInt(currentannualSpend);
}
function generateTextBox(idOfDraggedItem)
{		

	/*boxCount++;	
	if(boxCount>3)
	{
		padding=padding+35;
		document.getElementById("startsavingImage").style.cssText="padding-top:"+padding+"px;";
	}*/	
		
	
	keyArr = new Array('magazine','music','clothes','cinema','Makeup','lunchtimes','takeaways','Restaurants','Coffee','Smoking','ThePub','Clubbing','Books','Treats');
	
	
	
	isStandardItem=false;
	for(i=0;i<keyArr.length;i++)
	{
			if(keyArr[i]==idOfDraggedItem)
			{
				isStandardItem=true;
				break;
			}
			
	}
	
	if(isStandardItem)	
	{
		var uid=idOfDraggedItem;
		uvalue=idOfDraggedItem;
	}	
	else
	{
		var uid=document.getElementById(idOfDraggedItem).innerHTML;		
		var uvalue=uid;
		blankIndex=uid.indexOf(" ");
		if(blankIndex>0)
			uid=uid.substring(0,blankIndex);			
			
	}	

	//alert(uid);
	
	col3=document.getElementById("cart1");	
	var chld=col3.childNodes;
	dragdiv=document.createElement("DIV");	
	dragdiv.className="dragableTxtRight1";
	
	if(document.getElementById("divfld_"+uid))
		dragdiv.id="divfld_"+uid+"1";	
	else
			dragdiv.id="divfld_"+uid;
	
	textObj=document.createElement("input");	
	poundContainer=document.createElement("DIV");
	poundContainer.style.cssText="float:left;font-weight:bold;";
	
	if(ff) 
		poundContainer.style.cssText="padding-top:2px;float:left;font-weight:bold;margin-right:2px;";
	else if(ie6) 
		poundContainer.style.cssText="float:left;font-weight:bold;";
	
	pound=document.createTextNode("£");
	poundContainer.appendChild(pound);			
	
	if(document.getElementsByName("tbox"+uid).length>0)
		textObj.name="tbox"+uid+"1";	
	else
		textObj.name="tbox"+uid;	
	
	textObj.type="text";
	
	if(document.getElementById("tbox"+uid))
		textObj.id="tbox"+uid+"1";	
	else
		textObj.id="tbox"+uid;		
	
 	textObj.style.cssText="width:50px;height:18px;margin-bottom:3px;";
	textObj.setAttribute("maxLength",5);
   	textObj.onchange=calcTotal;
	textObj.onblur=formatspend;
	//uid=idOfDraggedItem.charAt(idOfDraggedItem.length-1);	
  	hiddenObj=document.createElement("input");
	hiddenObj.type="hidden";
	hiddenObj.name="hidden[]"; //+uid;
	hiddenObj.value=uvalue;
	//textObj.onblur=evalValue;
	textObj.setAttribute('autocomplete','off');
	textObj.onkeyup=evalValue;
	col4=document.getElementById("startsavinglink");

	poundContainer.appendChild(textObj);
	dragdiv.appendChild(poundContainer);
	//dragdiv.appendChild(textObj);	
	dragdiv.appendChild(hiddenObj);	
	//col3.insertBefore(dragdiv,col4);
	col3.appendChild(dragdiv);
	//	alert(chld.length);
	var num=0;
	do{		
		num++;
		var defa="defa"+num;
		defa=document.getElementById(defa);		
		if(defa==null)
	   		continue;	
		cart.removeChild(defa);	
		break;		
	}while(num<20);
}
function removeTextBox(idOfDraggedItem)
{
	col3=document.getElementById("cart1");
	//uid=idOfDraggedItem.charAt(idOfDraggedItem.length-1);
	var uid=idOfDraggedItem;
	var dropdiv=document.getElementById("divfld_"+uid);
		   dropdiv.className="products";
		   col3.removeChild(dropdiv);
/*
	var num=0;	
	do{		
		num++;
		var defa="defa"+num;
		defa=document.getElementById(defa);		
		if(defa!=null)
	   		continue;			
		dragdiv=document.createElement("DIV")
		dragdiv.id=defa;
		dragdiv.className="dragableBoxRight";
		cart.appendChild(dragdiv);	// Appending dragged element as child of target box		
		break;		
	}while(num<20);*/
	calcTotal();
}

function onDragFunction(cloneId,origId)
{
	window.status = 'Started dragging element with id ' + cloneId;
	var obj = document.getElementById(cloneId);
	try{
	obj.style.width='330px';
	}catch(e)
	{}
	num=0;		
}