if(adus){
}else{
	var	adus = {}
}
/* These variables are used store min and max amount linmit for Gift Card*/
var minAmt;
var maxAmt;
var maxMsgLimit = 150;


adus.previewMsg = function(id)
{
	/* Resets the error to display:none*/
	$('gcMainError'+id).style.display = "none";
	$('gcError'+id).style.display = "none";
	if(adus.prodValid(id)&& adus.msgLimitCheck(id))
	{	
		var box = new Df.Modal()
		box.pars.animateDialog = {
			time: 150,
			pause: 60,
			opacity:.99,
		 selectors:['opacity']
		}
		box.pars.minWidth = document.body
		box.pars.minHeight = document.body
		box.set()
		var previewSmallImageURL, previewToValue, previewFromValue,previewMsgValue;
		previewSmallImageURL = $('smallImageURL'+id);
		previewToValue = $('to_'+id);
		previewFromValue = $('from_'+id);
		previewMsgValue = $('msg_'+id);
		box.setContentString('<div class="dialog">'+
							 '<div id="dialogTop" class="pngfix"><div class="close">close</div></div>'+
							 '<div id="dialogBody" class="pngfix">'+
								 '<div id="gcPreviewContent">'+
									'<div id="gcPreviewImage"><img src="'+previewSmallImageURL.value+'"/></div>'+
									'<div id="gcSummary">'+
										'<div><span>To :</span> '+previewToValue.value+'</div>'+
										'<div><span>From :</span> '+previewFromValue.value+'</div>'+
										'<div><span>Message :</span> '+previewMsgValue.value+'</div>'+
									'</div>' +
								 '</div>'+
							 '</div>'+
							 '<div id="dialogBottom" class="pngfix"></div></div>')
		box.show()
	}
}
/* This function validates the giftCard selection by id */
adus.prodValid = function(id){
	var gcCardValue;
	var gcCardMValue;
	var gcCardMsgValue;
	gcCardValue = $('cardValue'+id);
	gcCardMValue = $('cardMValue'+id);
	gcCardMsgValue = $('msg_'+id);
	if((gcCardValue.value=="" && gcCardMValue.value == "")){
		$('gcMainError'+id).style.display = "block";
		$('gcError'+id).style.display = "block";
		gcCardValue.focus();
		return false;
	}
	/*else{
		if(gcCardMsgValue.length > 350){
			$('gcMsgError'+id).style.class = "gcError";
			$('gcMsgError'+id).style.display = "block";
			gcCardMsgValue.focus();
			return false;
		}else{
			return true;
		}
	}*/
	return true;
}

adus.prodAmtSwitch = function(id, flag){
	var gcCardValue;
	var gcCardMValue;
	gcCardValue = $('cardValue'+id);
	gcCardMValue = $('cardMValue'+id);	
	if(flag){
		if (gcCardValue.selectedIndex > 0 && gcCardMValue.value != '') {
		   gcCardValue.selectedIndex = 0;
	  }
	}else{
		if (gcCardMValue.value != '') {
		   gcCardMValue.value = '';
	  }
	}
}

adus.formValidate = function(gcCnt){
	var gcCardValue;
	var gcCardMValue;
	var flag = true;
	for(var i=0; i< gcCnt && flag; i++){
		/* Resets the error to display:none*/
		$('gcMainError'+i).style.display = "none";
		$('gcError'+i).style.display = "none";
/*		$('gcMsgError'+i).style.class = "";
		$('gcMsgError'+i).style.display = "none";*/		
		flag = adus.prodValid(i) && adus.isNumeric(i) && adus.amountValidate(i)  && adus.msgLimitCheck(i);	
	}
		
	if(flag){
		adus.prepareAmounts(gcCnt);
		document.getElementById("f").submit();
	}
}
adus.prepareAmounts = function(gcCnt){
	var gcCardValue;
	var gcCardMValue;
	var gcCardAmount;
	for(var i=0; i< gcCnt; i++){
		gcCardValue = $('cardValue'+i);
		gcCardMValue = $('cardMValue'+i);
		gcCardAmount = $('amount'+i);
		if(gcCardValue.value >0){
			gcCardAmount.value = gcCardValue.value;
		}else{
			gcCardAmount.value = gcCardMValue.value;		
		}
	}
}

adus.goToOptions = function() {
	var gcQtySelects = document.getElementsByTagName('select');
	var gcQty;
	var hidenEle;
	var numTotalProds = 0;
	var prdIndx=0;
	document.getElementById('gcErrors').style.display = "none";

	if (gcQtySelects != null) {
		for (var i = 0; i < gcQtySelects.length; i++) {
			gcQty = gcQtySelects[i].value;
			if(gcQty != null && gcQty > 0){
				numTotalProds += parseInt(gcQty, 10);
				eval('hidenEle = document.f.gc_'+prdIndx);
				hidenEle.value = ((gcQtySelects[i].id.substr(gcQtySelects[i].id.indexOf("_") + 1)) + "|" + parseInt(gcQty, 10));
				prdIndx = prdIndx+1;
			}
		}
	}
	if( numTotalProds <= 0){
		alert("Please Select at least 1 gift card in order to continue.");
		return false;
	}else {
		document.f.prodCounter.value = numTotalProds;
		document.f.submit();
	}
}

adus.isNumeric = function(id) {
	var value;
	var gcCardMValue;
	var re = /(^\d*\.?\d{1,2}$)/
	$('gcError'+id).style.display = "none";
  $('gcMainError'+id).style.display = "none";
	gcCardMValue = $('cardMValue'+id);
	value = gcCardMValue.value;
	if ((value != null && value != "") && ! re.test(value)){
		$('gcMainError'+id).style.display = "block";
		$('gcError'+id).style.display = "block";
		$('cardMValue'+id).focus();
		return false;
	}
	return true;
}

/* This function validates, if giftCard amount is in the min and max amount limits*/
adus.amountValidate = function(id) {
	var value;
	var gcCardMValue;
	$('gcError'+id).style.display = "none";
  $('gcMainError'+id).style.display = "none";
	gcCardMValue = $('cardMValue'+id);
	value = gcCardMValue.value;
	if ((value != null && value != "") && (value < minAmt || value > maxAmt)){
		$('gcMainError'+id).style.display = "block";
		$('gcError'+id).style.display = "block";
		$('cardMValue'+id).focus();
		return false;
	}
	return true;
}

adus.msgLimitCheck = function(prdIndx) {
	var msg;
	eval('msg = document.f.msg_'+prdIndx);
	if (msg.value.length > maxMsgLimit){
		msg.focus();
		alert("Message exceeds the Max length of 150 characters.");
		return false;
	}
	return true;
}

adus.badWordErrorCheck = function(badWordFields) {
   var indx;
   var indx_prev;
   var elem;
	if (badWordFields && badWordFields.length > 0) {
    	var badWordField;		
        for (var i = 0; i < badWordFields.length; i++) {
            badWordField = document.getElementById(badWordFields[i]);
            if (badWordField) {
	            indx = badWordFields[i].substr(badWordFields[i].indexOf("_") + 1);
				if(indx!= indx_prev){
					elem = document.getElementById('gcMainError'+indx);
					elem.style.display = "none";
		        	elem.innerHTML += "<br>&nbsp;&nbsp;-&nbsp;Inappropriate language will not be accepted.";
		        	elem.style.display = "block";
		        	indx_prev = indx;
		        }
                badWordField.value = "";
                badWordField.style.backgroundColor = "yellow";
                badWordField.focus();
            }
        }		
    }
}
