/*function submit form to Delete Card */
function revCall() {
	var delForm = document.getElementById("deleteCardForm");
	delForm.submit();
	
}

/*function to sum offers from offer table */
function sumOffers(id) {
    	var tds = document.getElementById("offerTable").getElementsByTagName("td");
   		var sumOffer = 0;  
   		var sumDonation = 0; 
   		var currency=tds[5].innerHTML.charAt(0);
		
		for(var i = 0;i<tds.length ; i++) {
    		if(tds[i].className == "countCash" && id=="selectCash" ) {
    			var cashAmount = tds[i].innerHTML.replace(/\,/g,'');
     			sumOffer += isNaN(cashAmount.substring(1)) ? 0 : (cashAmount.substring(1))*1;
     			
			}
    		if(tds[i].className == "countAmazon" && id=="selectAmazon" ) {
    			var amazonValue = tds[i].innerHTML.replace(/\,/g,'');
     			sumOffer += isNaN(amazonValue.substring(1)) ? 0 : (amazonValue.substring(1))*1;
      		}
    		
    	}
		
    	document.getElementById("total").innerHTML = currency.concat(sumOffer.toFixed(2));
}// end of sumOffers

/*Function to calculate donation amount */
//Commented as per new UI implementation requirement
/*function calculateDonation(msg){
	var sumOffer = 0;
	var percentDonate=0;
	var remainingamount=0;
	var inputValue=document.EnterCardDetails.percentageCharity.value;
	
	if(inputValue >=0 && inputValue<=100){
		var inputPercent=inputValue/100;
		var tds = document.getElementById("CardDetailTable").getElementsByTagName("td");
		var currency=tds[7].innerHTML.charAt(0);
		for(var i = 0;i<tds.length ; i++) {
			if(tds[i].className == "countCash") {
				var cashAmount = tds[i].innerHTML.replace(/\,/g,'');
				sumOffer += isNaN(cashAmount.substring(1)) ? 0 : (cashAmount.substring(1))*1;
			}
			if(tds[i].className == "countAmazon") {
				var amazonAmount=tds[i].innerHTML.replace(/\,/g,'');
				sumOffer += isNaN(amazonAmount.substring(1)) ? 0 : (amazonAmount.substring(1))*1;
			}
		}
		percentDonate=(inputPercent*sumOffer).toFixed(2);
		remainingamount=(sumOffer-percentDonate).toFixed(2);
		document.getElementById("donationAmount").innerHTML=currency.concat(percentDonate);
		document.getElementById("remainingAmount").innerHTML=currency.concat(remainingamount);
		document.getElementById("percentageLimit").innerHTML="";
	}else{
		document.getElementById("percentageLimit").innerHTML=msg;
	}
}*///end of calculateDonation

/*function to validate check box selection and percentage limit on form submit */
//Commented as per new UI implementation requirement
/*function validateCheckbox(checkboxMsg,percentageLimitMsg,form){
	var inputValue=document.EnterCardDetails.percentageCharity.value;
	if(!document.EnterCardDetails.finalizeMode.checked){
		document.getElementById("checkboxMessage").innerHTML=checkboxMsg;
		return false;
	}else if((document.EnterCardDetails.finalizeMode.checked)&& !(inputValue>=0 && inputValue<=100)) {
		document.getElementById("checkboxMessage").innerHTML="";
		document.getElementById("percentageLimit").innerHTML=percentageLimitMsg;
	    return false;
	}else{
		return true;
	}
}*///end of validateCheckbox

/*function to check the selection of charity organisation*/
//Commented as per new UI implementation requirement
/*function validateCharity(msg,defaultSelection){
	var charityValue = document.EnterCardDetails.percentageCharity.value;
	var index = document.EnterCardDetails.charityOrganization.selectedIndex;
    var selectedValue = document.EnterCardDetails.charityOrganization.options[index].text;
	if(charityValue >0 && charityValue<=100){
		if(selectedValue==defaultSelection){
			 document.getElementById("CharityMessage").innerHTML=msg;
			   return false;
		 }else{
			  document.getElementById("CharityMessage").innerHTML="";
			  return true;
		}
	}
}*///end of validateCharity

/*function to show/hide charity */
function enableDisableCharityDiv(charityDiv,defaultCharity, forMinisite){
	/*var s = forMinisite + "  selectIndex: " + document.sellcardconfform.charityOrg.selectedIndex
				+ " percentage: " + document.sellcardconfform.charityPercent.value;
	alert(s);*/
	if(charityDiv.style.display=="none"){
		charityDiv.style.display="block";
		//reset values
		if(forMinisite != "yes"){
			document.sellcardconfform.charityOrg.selectedIndex = 0;
			document.sellcardconfform.charityOrg.options[0].selected = true;
			document.sellcardconfform.charityPercent.value = 0;
		}
	}else{
		charityDiv.style.display="none";
		document.getElementById("charityErrorMessage").innerHTML="";
	}
}


/*function to open pop up window */
function winOpen(url_add)
{
window.open(url_add,'shippingLabel','width=600,height=500,menubar=yes,status=yes,location=yes,toolbar=yes,scrollbars=yes');
}



//get Charity Organisation and Amount
function getCharity(defaultCharity,minCharity,DonationAmountMessage,selectCharityMessage,invalidDonationAmount){
	var percentDonate=0;
	var remainingamount=0;
	var inputPercent=0;
	var totalOfferAmount = document.getElementById("totalAmount").innerHTML.replace(/\,/g,'');
	var totalAmount = totalOfferAmount.substring(1);
	var currencySymbol = document.getElementById("totalAmount").innerHTML.charAt(0);
	var charityOrgIndex = document.sellcardconfform.charityOrg.selectedIndex;
	var selectedCharityValue = document.sellcardconfform.charityOrg.options[charityOrgIndex].value;
	var selectedCharityText = document.sellcardconfform.charityOrg.options[charityOrgIndex].text;
	var inputCharityValue = document.sellcardconfform.charityPercent.value;
	var compareCharity=(selectedCharityText==defaultCharity);
	if(inputCharityValue > 0 && inputCharityValue <= 100){
		inputPercent = inputCharityValue/100;
	}
	//calculate charity donation amount
	percentDonate=(inputPercent*totalAmount).toFixed(2);
	remainingamount=(totalAmount-percentDonate).toFixed(2);
	
	if(!compareCharity && percentDonate >= minCharity){
		document.getElementById("charityOrganization").value = selectedCharityValue;
		document.getElementById("percentCharity").value = inputCharityValue;
		document.getElementById("percentageCharity").value = percentDonate;
		document.getElementById("charityAmount").innerHTML = currencySymbol+percentDonate;
		document.getElementById("charityName").innerHTML = "<h5>We'll Send "+ selectedCharityText +" :</h5>";
		document.getElementById("remainingAmount").innerHTML = currencySymbol+remainingamount;
		document.getElementById("charityErrorMessage").innerHTML = "";
	}else{
		document.getElementById("charityOrganization").value = "";
		document.getElementById("percentageCharity").value = "";
		
	}
	//display Charity block only if user has selected Charity Organisation and Charity Amount
	if(compareCharity){
		document.getElementById("displayCharity").style.display="none";
		document.getElementById("remainingAmount").innerHTML=currencySymbol+totalAmount;
		document.getElementById("charityErrorMessage").innerHTML=selectCharityMessage;
		
	}else if(!(inputCharityValue >= 0 && inputCharityValue <= 100)){
		document.getElementById("displayCharity").style.display="none";
		document.getElementById("remainingAmount").innerHTML=currencySymbol+totalAmount;
		document.getElementById("charityErrorMessage").innerHTML = invalidDonationAmount;
	}else if(percentDonate < minCharity){
		document.getElementById("displayCharity").style.display="none";
		document.getElementById("remainingAmount").innerHTML=currencySymbol+totalAmount;
		document.getElementById("charityErrorMessage").innerHTML = DonationAmountMessage+" "+currencySymbol+minCharity;
	}else{
		document.getElementById("displayCharity").style.display="block";
}
	//hide Cash/Card Offers if user donates 100% charity
	if(!compareCharity && inputCharityValue == 100 && percentDonate > minCharity ){
			document.getElementById("hideOffer").style.display = "none";
			document.getElementById("selectOffer").style.display = "block";
	}else{
		document.getElementById("hideOffer").style.display = "block";
		document.getElementById("selectOffer").style.display = "none";
	}
	
}


//get Charity Organisation and Amount for Minisite
function getCharityForMinisite(minCharity,DonationAmountMessage,invalidDonationAmount,minisitePartner){
	var percentDonate=0;
	var remainingamount=0;
	var inputPercent=0;
	var totalOfferAmount = document.getElementById("totalAmount").innerHTML.replace(/\,/g,'');
	var totalAmount = totalOfferAmount.substring(1);
	var currencySymbol = document.getElementById("totalAmount").innerHTML.charAt(0);
	var inputCharityValue = document.sellcardconfform.charityPercentMinisite.value;
	
	if(inputCharityValue > 0 && inputCharityValue <= 100){
		inputPercent = inputCharityValue/100;
	}
	//calculate charity donation amount
	percentDonate=(inputPercent*totalAmount).toFixed(2);
	remainingamount=(totalAmount-percentDonate).toFixed(2);
	
	if(percentDonate >= minCharity){
		//document.getElementById("charityOrganization").value = selectedCharityValue;
		document.getElementById("percentCharity").value = inputCharityValue;
		document.getElementById("percentageCharity").value = percentDonate;
		document.getElementById("charityAmount").innerHTML = currencySymbol+percentDonate;
		document.getElementById("charityName").innerHTML = "<h5>We'll Send "+ minisitePartner +" :</h5>";
		document.getElementById("remainingAmount").innerHTML = currencySymbol+remainingamount;
		document.getElementById("charityErrorMessage").innerHTML = "";
		
	}else{
		document.getElementById("percentageCharity").value = "";
		
	}
	
	//display Charity block only if user has selected Charity Organisation and Charity Amount
	 if(!(inputCharityValue >= 0 && inputCharityValue <= 100)){
		
		document.getElementById("displayCharity").style.display="none";
		document.getElementById("remainingAmount").innerHTML=currencySymbol+totalAmount;
		document.getElementById("charityErrorMessage").innerHTML = invalidDonationAmount;
	}else if(percentDonate < minCharity){
		
		document.getElementById("displayCharity").style.display="none";
		document.getElementById("remainingAmount").innerHTML=currencySymbol+totalAmount;
		document.getElementById("charityErrorMessage").innerHTML = DonationAmountMessage+" "+currencySymbol+minCharity;
	}else{
		
		document.getElementById("displayCharity").style.display="block";
}

	//hide Cash/Card Offers if user donates 100% charity
	if(inputCharityValue == 100 && percentDonate > minCharity ){
			document.getElementById("hideOffer").style.display = "none";
			document.getElementById("selectOffer").style.display = "block";
	}else{
		document.getElementById("hideOffer").style.display = "block";
		document.getElementById("selectOffer").style.display = "none";
	}
	
}



/*Show Card Details in Delete Modal window and open Modal Window in Sell Flow*/
function showCardDetails(cardIndex,imageUrl,productName,cardValue,cardNumber,currencySymbol){
	document.getElementById("indexOfItemToDelete").value=cardIndex;
	document.getElementById("image").src = imageUrl;
	document.getElementById("image").alt = productName;
	document.getElementById("productName").innerHTML = productName;
	document.getElementById("cardValue").innerHTML =currencySymbol+cardValue;
	if(cardNumber.length>0 && cardNumber!=0 ){
		document.getElementById("cardNumber").innerHTML = "Card Number: <b>"+cardNumber+" </b>";
	}
	showModalWindow('mw_delete_notification');
	
}// end of showCardDetails


//GiftsCardIOwn 

/*function to sumbit the form when user tries to sell card from GiftsCardIOwn  */
function submitformAll( typeOfAction ){
	 	document.getElementById("selectionType").value = "multiple";
		document.getElementById("typeOfAction").value = typeOfAction;
	 	document.getElementById("WallatCardsFormID").submit();
	 	
}//end of submitformAll

/*function to edit Card in  CardsIOwn */
function submitform( selectedItemId, shoppingListItemSeqId, cardNumber, cardValue, cardPin, productId, typeOfAction,imageUrl,productName){	
	   	var frm = document.editCard;
	   	frm.shoppingListId.value = selectedItemId;
	   	frm.shoppingListItemSeqId.value = shoppingListItemSeqId;
	   	frm.typeOfAction.value = typeOfAction;
	   	frm.cardNumber.value = cardNumber;
	   	frm.cardValue.value = cardValue;
	   	frm.cardPin.value = cardPin;
	   	frm.productId.value = productId;
	   	document.getElementById("merchant").innerHTML = productName;
	   	document.getElementById("merchantsmallImage").src = imageUrl;
	 	showModalWindow('mw_edit_card');
	 	
}//end of submitform


/* function to display card details in Modal Window in CardsIOwn*/
function deleteCard(selectedItemId,shoppingListItemSeqId,imageUrl,productName,cardValue,cardNumber,currencySymbol){
 	var frm = document.deleteCardsOwnForm;
	frm.shoppingListId.value = selectedItemId;
 	frm.shoppingListItemSeqId.value = shoppingListItemSeqId;
	document.getElementById("merchantImage").src=imageUrl;
	document.getElementById("merchantName").innerHTML=productName;
	document.getElementById("cardCurrencyValue").innerHTML = currencySymbol+cardValue;
	if(cardNumber.length > 0 && cardNumber != 0 ){
		document.getElementById("giftCardNumber").innerHTML = "Card Number: <b>"+cardNumber+" </b>";
    }
 	showModalWindow('mw_delete_cards_own');
	
}//end of deleteCard


/*function to redirect the user to sell Main page when he deletes last card  */
function redirectUser(sellURL){
	redirectURL = location.protocol + "//" + location.host + sellURL;
	window.location.href = redirectURL;
}//end of redirectUser



/*function to restrict user  submit form  multiple times*/
	function checksubmit(){
		document.AddToCart.submit();
		checksubmit = false;
		return false;
 }// end of checksubmit


	function submitCardConfirmationForm(sellcardconfform){
		showProgressImg();
		sellcardconfform.submit();
	}