var req;

function computeTotals(){
	
	var finalLineCount = parseInt(document.getElementById("finalLineCount").value);
	
	var priceOfBook;
	var qtyOrdered;
	
	var totalForBook;
	
	var totalForOrder;
	
	var totalDiv;
	
	totalForOrder = 0.0;
	
	for (var i=0; i<finalLineCount; i++){
		
		priceOfBook = 0.0;
		
		if(document.getElementById('costOfBook_'+i))
			priceOfBook = parseFloat(document.getElementById('costOfBook_'+i).value);
		
		qtyOrdered = 0;
		if(document.getElementById('qtyOrdered_'+i))
			qtyOrdered = parseFloat(document.getElementById('qtyOrdered_'+i).value);
		
		totalForBook = 0.0;
		
		totalForBook = priceOfBook*qtyOrdered;
		
		if(document.getElementById('costxqty_'+i)) {
			
			totalDiv = document.getElementById('costxqty_'+i);
			totalDiv.innerHTML = "<span>Rs. </span>"+totalForBook.toFixed(2);
			
			if(document.getElementById('lineCost_'+i))
				(document.getElementById('lineCost_'+i)).value = totalForBook;
		}
		
		totalForOrder += totalForBook;
		
		if(document.getElementById('totalAmtSpent'))
			(document.getElementById('totalAmtSpent')).value = totalForOrder;
		 
		totalSpend = document.getElementById('totalSpend');
		totalSpend.innerHTML = "<span>Rs. </span>"+totalForOrder.toFixed(2);
		
	}
	
	computeShippingCost(totalForOrder);
	
}

function prepareSearch(){
	
	var searchString = "";
	searchString = document.main_search_form.query.value;
	if (searchString!='') document.main_search_form.hquery.value=searchString;
	
	//document.main_search_form.submit();
}

function searchText(searchString){
	
	document.main_search_form.hquery.value=searchString;
	
	document.main_search_form.submit();
}

function openSesame(userLogin){
	
	var sVariable = document.sign_in_form.txtLogin.value;
	if(sVariable=='') {
	
		alert("please enter your login email");
		
	} else {
		
		document.sign_in_form.action="doRecover.jsp";
		document.sign_in_form.submit();
		
	}
	

}

function computeShippingCost(purchaseCost){
	
	var shipCountry =  parseInt(document.getElementById("shipCntry").value);
		
	var sellerCountry;
	
	var finalLineCount;
	
	var qtyOrdered;

	var shippingCost;
	shippingCost = 0.0;
	
	var totalCost;
	totalCost = 0.0;
	
	var rad_val;
	
	for (var i=0; i < document.frmCheckout.DeliveryOption.length; i++)
	   {
	
		if (document.frmCheckout.DeliveryOption[i].checked)
	      
			rad_val = document.frmCheckout.DeliveryOption[i].value;
		
	   }
	
		finalLineCount = parseInt(document.getElementById("finalLineCount").value);
		
		var thisShippingCost = 0.0;
	
		for (var i=0; i<finalLineCount; i++){
			
			qtyOrdered = 0;
			if(document.getElementById('qtyOrdered_'+i)) {
				qtyOrdered = parseFloat(document.getElementById('qtyOrdered_'+i).value);
				
				//alert ('before: qtyOrdered_'+i+' = '+qtyOrdered);
				
				if(qtyOrdered==0){
					
					qtyOrdered = 0; mustReload = true; } else {
						
						qtyOrdered = (qtyOrdered-1)*0.5 + 1;
						
				}
				//alert ('after: qtyOrdered_'+i+' = '+qtyOrdered);
				
			}
			
			sellerCountry = parseInt(document.getElementById('sellerCountry_'+i).value);
			
			if (shipCountry==sellerCountry){
				
				//<%=DefaultVar.POST_NORMAL%> = 877
				//<%=DefaultVar.POST_EXPEDITED%> = 878
				
				if (rad_val==877) {
					
					thisShippingCost = parseFloat(document.getElementById('inlandShipping_'+i).value);
					
					thisShippingCost = thisShippingCost*qtyOrdered
					
					shippingCost = shippingCost + thisShippingCost;
					
				} else {
					
					thisShippingCost = parseFloat(document.getElementById('inlandExp_'+i).value);
					
					thisShippingCost = thisShippingCost*qtyOrdered
					
					shippingCost = shippingCost + thisShippingCost;
					
				}
			
			} else {
				
				if (rad_val==877) {
					
					thisShippingCost = parseFloat(document.getElementById('overseasShipping_'+i).value)
					
					thisShippingCost = thisShippingCost*qtyOrdered
					
					shippingCost = shippingCost + thisShippingCost;
					
				} else {
					
					thisShippingCost = parseFloat(document.getElementById('overseasExp_'+i).value)
					
					thisShippingCost = thisShippingCost*qtyOrdered
					
					shippingCost = shippingCost + thisShippingCost;
					
				}					
			
			}//if(buyerCountry==sellerCountry)...else
			
			if(document.getElementById('costOfShipping_'+i))
				(document.getElementById('costOfShipping_'+i)).value = thisShippingCost;
			
			//alert('costOfShipping_'+i+' = '+thisShippingCost);
			 
		}//for
		
		if(document.getElementById('shCost'))
			(document.getElementById('shCost')).value = shippingCost;
		
		//alert ('final shipping cost = '+shippingCost);
		 
		var shipCharge = document.getElementById('shipCharge');
		shipCharge.innerHTML = "<span>Rs. </span>"+shippingCost.toFixed(2);
		
		totalCost = shippingCost + purchaseCost;

		if(document.getElementById('finalAmt'))
			(document.getElementById('finalAmt')).value = totalCost;
		 
		var finalCharge = document.getElementById('finalCharge');
		finalCharge.innerHTML = "<span>Rs. </span>"+totalCost.toFixed(2);
		
		//alert ('final cost = '+totalCost);
		 
		//if(totalCost<=0) document.frmCheckout.submit_order_button.disabled=true;
		
		if(totalCost<=0)window.location.reload();
		
}

function addToBasket(resourceID){
	
	var elResource = document.getElementById("resource_id");
	
	elResource.value = resourceID;
	
	//alert(elResource.value);
	
	document.buy_this_form.submit();
	
}

function deleteRow(tableID, i) {
    try {
    	var table = document.getElementById(tableID);
    	table.deleteRow(i);
    }catch(e) {
        alert(e);
    }
}
/************************************************************************************************************
(C) www.dhtmlgoodies.com, November 2005
************************************************************************************************************/

var ct_slideSpeed = 10;	// Higher value = faster
var ct_timer = 10;	// Lower value = faster

var objectIdToSlideDown = false;
var ct_activeId = false;
var ct_slideInProgress = false;
function showHideContent(e,inputId)
{
	if(ct_slideInProgress)return;
	ct_slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('de_a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(ct_activeId &&  ct_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(ct_activeId,(ct_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,ct_slideSpeed);
		}
	}else{
		slideContent(numericId,(ct_slideSpeed*-1));
		ct_activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	
	var obj =document.getElementById('de_a' + inputId);
	var contentObj = document.getElementById('de_ac' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',ct_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('de_a' + objectIdToSlideDown).style.display='block';
				document.getElementById('de_a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,ct_slideSpeed);				
			}else{
				ct_slideInProgress = false;
			}
		}else{
			ct_activeId = inputId;
			ct_slideInProgress = false;
		}
	}
}


function initShowHideDivs()
{
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='de_question'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'de_q'+divCounter;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'de_a'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='de_answer_content';
			contentDiv.id = 'de_ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
}
window.onload = initShowHideDivs;
