var trus = {};
trus.dgcStep2Validate = function (dgcForm){
	var eleType, eleValue, elementIndex;
	var dgc_cardValueFlag = false;
	var dgc_rEmail = '';
	var elementValid = new Array(dgcForm.elements.length);
	var regex = /^[-_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+\.[a-zA-Z0-9]+$/;
	var dgcStep2Valid = true;
	for(i=0; i<dgcForm.elements.length; i++){
		elementValid[i] = true;
	}
	$$('.hasError').each(function(el){
		el.removeClassName('hasError');
	});
	$$('.highLightRow').each(function(el){
		el.removeClassName('highLightRow');
	});
	for(i=0; i<dgcForm.elements.length; i++)
	{
		if(dgcForm.elements[i].type == "text" || dgcForm.elements[i].type == "select-one")
		{
			eleType = dgcForm.elements[i].name.substr(0, dgcForm.elements[i].name.indexOf("["));
			eleValue = dgcForm.elements[i].value;
			elementIndex = dgcForm.elements[i].id.substr(dgcForm.elements[i].id.lastIndexOf("_")+1);
			switch(eleType)
			{
				case "dgc_selCardVal":
					dgc_cardValueFlag = false;
					if(!isNaN(eleValue) && eleValue > 0)
						dgc_cardValueFlag = true;
					break;
				case "dgc_custCardVal":
					if(!dgc_cardValueFlag && !isNaN(eleValue) && eleValue > 0)
					{	
						dgc_cardValueFlag = true;
					}
					if(!dgc_cardValueFlag)
					{	// HighLight this cardValue row
						$('dgc_cardValue_'+elementIndex+'_DIV').addClassName('highLightRow');
						elementValid[elementIndex] = false;
					}
					break;
				case "dgc_rName":
				case "dgc_sName":
					if(eleValue.length <= 0)
					{	// HighLight this Recipient's name or Sender's name row
						$(dgcForm.elements[i].id+'_DIV').addClassName('highLightRow');
						elementValid[elementIndex] = false;
					}
					break;
				case "dgc_rEmail":
					if(eleValue.length <= 0 || !regex.test(eleValue))
					{	// HighLight this Recipient's email row
						$(dgcForm.elements[i].id+'_DIV').addClassName('highLightRow');
						elementValid[elementIndex] = false;
					}else
						dgc_rEmail = eleValue;
					break;
				case "dgc_rEmailConfirm":
					if(dgc_rEmail.length > 0)
					{ 
						if(eleValue.length <=0 || !regex.test(eleValue) || eleValue != dgc_rEmail)
						{	// HighLight this Recipient's confirm email row
							$(dgcForm.elements[i].id+'_DIV').addClassName('highLightRow');
							elementValid[elementIndex] = false;
						}else
							dgc_rEmail = '';
					}else if(eleValue.length <=0 || !regex.test(eleValue))
					{	
						$(dgcForm.elements[i].id+'_DIV').addClassName('highLightRow');
						elementValid[elementIndex] = false;
					}
					break;
				default:	
					break;
			}
		}
	}
	for(i=0; i<dgcForm.elements.length; i++){
		if(elementValid[i] == false)
		{	$('dgc_customize_'+i).addClassName('hasError');
			dgcStep2Valid = false;
		}
	}
	if(dgcStep2Valid)
		dgcForm.submit();

};

trus.dgcPreviewInit = function(instanceId){
	$('PPDGCRecipientName').innerHTML = "[Recipient's name]";
	if($("digitalGiftCardQuantity"+instanceId+".toName").getValue())
		$('PPDGCRecipientName').innerHTML = $("digitalGiftCardQuantity"+instanceId+".toName").getValue();
	$('PPDGCSenderName').innerHTML = "[sender's name]";
	if($("digitalGiftCardQuantity"+instanceId+".fromName").getValue())
		$('PPDGCSenderName').innerHTML = $("digitalGiftCardQuantity"+instanceId+".fromName").getValue();
	$('PPDGCGiftMessage').innerHTML = "[Optional personalized gift message field: You may include a personal message to the recipient, or you may leave it blank, if you prefer.]";
	if($("digitalGiftCardQuantity"+instanceId+".message").getValue())
		$('PPDGCGiftMessage').innerHTML = $("digitalGiftCardQuantity"+instanceId+".message").getValue();
	$('PPDGCAmount').innerHTML = "$xxx";
	if($("digitalGiftCardQuantity"+instanceId+".giftAmount").getValue() != -1)
		$('PPDGCAmount').innerHTML = "$"+eval($("digitalGiftCardQuantity"+instanceId+".giftAmount").getValue());
	else if($("digitalGiftCardQuantity"+instanceId+".giftAmountAdHoc").getValue())
		$('PPDGCAmount').innerHTML = "$"+eval($("digitalGiftCardQuantity"+instanceId+".giftAmountAdHoc").getValue());
	$('PPDGCImg').src = $$("#dgc_customize_"+instanceId+" .dgcImage img")[0].src;
};


trus.dgcRemove = function(instanceId){
	var box = Df.thinBox({ 
		selector: '.removePopup', 
		modal: true, 
		anchor: 'viewport',
		left: '50%',
		top: '50%',
		events: { 
			'close': function (evt) {
				delete box;
			},
			'.remove': function (evt) {
				$('removeInstanceId').writeAttribute('value', instanceId+1);
				$('digitalGiftCard').insert(new Element('input', { type: "hidden", name: "_target1"}));
				$('digitalGiftCard').submit();
				box.hide();
				Event.stop(evt);
				return false;
			},
			'.close': function (evt) {
				box.hide();
				Event.stop(evt);
				return false;
			}
		} 
	}).show();
	return false;
};


trus.dgcPreview = function(instanceId){
	trus.dgcPreviewInit(instanceId);
	var box = Df.thinBox({ 
		selector: '.previewPopup', 
		modal: true, 
		anchor: '#previewBtn_'+instanceId,
		left: '64%',
		top: '102%',		
		events: { 
			'.close': function (evt) { 
				box.hide(); 
				Event.stop(evt);
				return false;

			},
			'close': function (evt) {
				delete box;
			}
		} 
	}).show();
	return false;
};


Event.observe(document, 'dom:loaded', function () {
	var validationErrors = 	$$("div.highLightRow input", "div.highLightRow select", "div.highLightRow textarea");
	if (validationErrors.size() > 0)
		validationErrors[0].focus();
	$$('.previewBtn').each(function (el) {
		var instanceId=parseInt(el.id.replace(/^[^\d_]*_(\d+)$/,'$1'));
		el.observe('click',function(e) { trus.dgcPreview(instanceId); Event.stop(e); return false; });
	});
	$$('.removeBtn').each(function (el) {
		var instanceId=parseInt(el.id.replace(/^[^\d_]*_(\d+)$/,'$1'));
		el.observe('click',function(e) { trus.dgcRemove(instanceId); Event.stop(e); return false; });
	});
});


	    
