/*
	Display page send form
*/
var SendingForm = {

    init_vg:function() {
        dojo.byId('send-to').onfocus = SendingForm.focusSendTo;

        dojo.byId('send-ecard-button2').getElementsByTagName('a')[0].onclick = function(){
            SendingForm.send_vg();
            return false;
        }

        dojo.byId('send-ecard-button1').getElementsByTagName('a')[0].onclick = function(){
            SendingForm.preview_vg();
            return false;
        }

        dojo.byId('close-button').getElementsByTagName('a')[0].onclick = function(){
            VirtualOptions.close();
            return false;
        }
    },

	init: function() {
		dojo.byId('send-to').onfocus = SendingForm.focusSendTo;
		
		for(var i = 1; i <= 16; i++) {
			dojo.byId('colors-' + i).onclick = function() {
				SendingForm.changeColor(this.className);
				return false;
			}
		}
		
		dojo.byId('send-font').onchange = function() {
			SendingForm.changeFont(this.value);
		}
		
		dojo.byId('fli-send-style').getElementsByTagName('input')[0].onclick = function() {
			if(this.checked) {
				SendingForm.setBold();
			}
			else {
				SendingForm.removeBold();
			}
		}
		
		dojo.byId('fli-send-style').getElementsByTagName('input')[1].onclick = function() {
			if(this.checked) {
				SendingForm.setItalic();
			}
			else {
				SendingForm.removeItalic();
			}
		}
		
		dojo.byId('fli-send-size').getElementsByTagName('a')[0].onclick = function(){
			SendingForm.decreaseFontSize();
			return false;
		}
		
		dojo.byId('fli-send-size').getElementsByTagName('a')[1].onclick = function(){
			SendingForm.increaseFontSize();
			return false;
		}
		
		dojo.byId('send-ecard-buttons').getElementsByTagName('a')[1].onclick = function(){
			SendingForm.send();
			return false;
		}
		
		dojo.byId('send-ecard-buttons').getElementsByTagName('a')[0].onclick = function(){
			SendingForm.preview();
			return false;
		}
		
		if(dojo.byId('permissions')) {
			var inputs = dojo.byId('permissions').getElementsByTagName('input');
			for(var i = 0; i < inputs.length; i++) {
				inputs[i].checked = 'checked';
			}
		}
		
		SuggestedMessages.init();
	},
	
	focusSendTo: function() {
		if(this.value == 'enter e-mail addresses separated with either a comma OR semicolon') {
			this.value = '';
		}
	},
	
	changeColor: function(color) {
		dojo.byId('send-message').style.color = color;
		document.orderForm.pn1606.value = color;
	},
	
	increaseFontSize: function(size) {
		if (!dojo.byId('send-message').style.fontSize) {
			dojo.byId('send-message').style.fontSize = '14px';
		}
		dojo.byId('send-message').style.fontSize = (parseInt(dojo.byId('send-message').style.fontSize.replace(/px/, '')) + 2) + 'px';
		document.orderForm.pn1610.value = parseInt(dojo.byId('send-message').style.fontSize.replace(/px/, ''));
	},
	
	decreaseFontSize: function(size) {
		if (!dojo.byId('send-message').style.fontSize) {
			dojo.byId('send-message').style.fontSize = '12px';
		}
		dojo.byId('send-message').style.fontSize = (parseInt(dojo.byId('send-message').style.fontSize.replace(/px/, '')) - 2) + 'px';
		document.orderForm.pn1610.value = parseInt(dojo.byId('send-message').style.fontSize.replace(/px/, ''));
	},
	
	changeFont: function(font) {
		dojo.byId('send-message').style.fontFamily = font;
	},
	
	setBold: function() {
		dojo.byId('send-message').style.fontWeight = 'bold';
	},
	
	removeBold: function() {
		dojo.byId('send-message').style.fontWeight = 'normal';
	},
	
	setItalic: function() {
		dojo.byId('send-message').style.fontStyle = 'italic';
	},
	
	removeItalic: function() {
		dojo.byId('send-message').style.fontStyle = 'normal';
	},
	
	send_vg: function() {
		if(SendingForm.validate() == 'Ok') {
            //dojo.byId('preview_vg').innerHTML = "... sending the gift";
            dojo.byId('preview_vg').style.display = 'none';
			document.orderForm.btnSend.value = 'send';
            ajaxPage = host + "/custom?is_vg=1";
            var ajax = new Requester(ajaxPage, 'POST', false, false);
            var qs = ajax.formToQuery(document.orderForm);
            ajax.sendRequest(qs);
            var respons = ajax.req.responseText; //.replace(/\n/g, '');
            var to_email = document.orderForm.toemail.value;
            var url = dojo.query('.productnumber')[VirtualOptions.index].toString();
            var prodnum = url.substr(url.length-7);
            var patern = /^[0-9]$/;
            if (respons.length < 20) {
                VirtualOptions.close();
                dojo.byId('confirm_vg').style.display = 'block';
                var html3 = '<table border="0" cellspacing="0" cellpadding="0" id="container_overlay-confirm_thankyou">' +
                    '<tr><td class="confirm-thankyou"> <div id="gift_thumb"><img alt="Beginning the Best Years" src="' + imghost + '/product/bigdates/'+prodnum+'/virtualgift.gif"></div></td>' +
                         '<td class="confirm-thankyou" style="width:239px;">' +
                             '<table border="0" cellspacing="0" cellpadding="0">' +
                                 '<tr><td valign="top"><div id="close-button-confirm"><a href="#" class="close_button" title="Close"></a></div></td></tr>' +
                                 '<tr><td style="padding-top:85px;">Thanks for using Egreetings!</td></tr>' +
                             '</table></td></tr>' +
                    '<tr><td colspan="2" style="vertical-align:top;">' +
                      '<table border="0" cellspacing="0" cellpadding="0" style=" width:392px; height:136px; float:left; margin-left:30px;">'+
                        '<tr><td style="height:25px; vertical-align:top;">The Virtual Gift you just sent is on its way.</td></tr>' +
                        '<tr><td style="height:25px; vertical-align:top;">We\'ll send an email to let you know when it reaches </td></tr>' +
                        '<tr><td style="height:25px; vertical-align:top;"><span href="#" style="color:#6B90A5; text-decoration:none; font-style:italic;" >' + to_email  + '</span></td></tr>'+
                        '<tr><td style="height:61px; vertical-align:top;">' +
                           '<div id="close-button-close" style="float:left; margin-left:128px; margin-top:20px;"><a href="#" class="close-link-style">Close</a> this window or </div><div id="browse_button"><a href="http://www.egreetings.com" class="button_gift_send" title="Send">Browse ecards</a></div></td></tr>'+
                        '</table></td></tr></table>'; 
                dojo.byId('confirm_vg').innerHTML = html3;

                dojo.byId('close-button-confirm').getElementsByTagName('a')[0].onclick = function(){
                    VirtualOptions.close();
                    return false;
                }
                dojo.byId('close-button-close').getElementsByTagName('a')[0].onclick = function(){
                    VirtualOptions.close();
                    return false;
                }
            } else {
                VirtualOptions.close();
                dojo.byId('confirm_vg').style.display = 'block';
                dojo.byId('confirm_vg').innerHTML = '<center style="padding: 20px 0;">There was a problem in sending the ecard</center>';
            }
			//document.orderForm.submit();
		}
		else { //de scos asta
			alert(SendingForm.validate());
		}
	},

    preview_vg: function() {
        if (SendingForm.validate() == 'Ok') {
             var message = dojo.byId('send-message').value.replace(/\n/g, "<BR>");
             var url = dojo.query('.productnumber')[VirtualOptions.index].toString();
             var prodnum = url.substr(url.length-7);
             var from_name = document.orderForm.fromname.value;
             VirtualOptions.close();
             dojo.byId('preview_vg').style.display = 'block';
             var html2 = '<div id="preview-vgift" style="display: block;">' +
                         '<div id="container_overlay-preview-vgift-vgift">' +
                         '<div id="close-button-preview" style="height: 26px; vertical-align: top;"><a href="#" class="close_buttonx" title="Close"></a></div>' +
                         '<div id="gift_preview-vgift" style="height: 300px; vertical-align: top;"><object width="400" height="300"><embed src="'+imghost+'/product/full/el/3280080.swf?ahost='+ahost+'&ihost='+imghost+'&productNumber=/product/bigdates/'+prodnum+'/graphic1&mode=preview" width="400" height="300"></embed></object></div>' +
                         '<div id="gift-message-show-style">' +
                         //'<div id="your_gift_message"><p>' + message  + '</p><p>' + from_name + '</p></div>' +
                         '<div id="your_gift_message"><p>' + message  + '</p></div>' +
                         '</div>' +
                         '<div id="gift-preview-vgift-bottom" valign="top">' +
                         '<div id="go-back" style="float: left; margin-top: 20px;"><a title="Go back" class="button_gift_send" href="#">Go back</a></div>' +
                         '<div id="send-pw" style="float: left; margin-left: 220px; margin-top: 20px;"><a title="Send" class="button_gift_send" href="#">Send</a></div>' +
                         '</div>' +
                         '</div>' +
                         '</div>';
            dojo.byId('preview_vg').innerHTML = html2;
        dojo.byId('close-button-preview').getElementsByTagName('a')[0].onclick = function(){
            VirtualOptions.close();
            return false;
        }
        dojo.byId('go-back').getElementsByTagName('a')[0].onclick = function(){
            dojo.byId('send_vg').style.display = 'block';
            dojo.byId('preview_vg').style.display = 'none';
            return false;
        }        
        dojo.byId('send-pw').getElementsByTagName('a')[0].onclick = function(){
            SendingForm.send_vg();
            return false;
        }

             //dojo.byId('preview_vg').innerHTML = '<center style="padding: 100px;">Preview - reference to product number: ' + prodnum + ' and to message: ' + message + '<br/> Having these, the preview page overlay can be constructed properly</center>';
            //document.orderForm.btnPreview.value = 'preview';
            //document.orderForm.submit();
        }
        else { // de scos asta
            alert(SendingForm.validate());
        }
    },    

    send: function() {
        if(SendingForm.validate() == 'Ok') {
            document.orderForm.btnSend.value = 'send';
            document.orderForm.submit();
        }
        else {
            alert(SendingForm.validate());
        }
    },
	
	preview: function() {
		if (SendingForm.validate() == 'Ok') {
			document.orderForm.btnPreview.value = 'preview';
			document.orderForm.submit();
		}
		else {
			alert(SendingForm.validate());
		}
	},
	
	validate: function() {
		var email_pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var error = '';
		
		if(dojo.byId('send-from').value == '') {
			error += 'Please enter your name.\n';
		}
		
		if(dojo.byId('send-email').value == '') {
			error += 'Please enter your email address.\n';
		}
		
		if(dojo.byId('send-email').value != '' && !email_pattern.test(dojo.byId('send-email').value)) {
			error += 'Please enter a valid email address for your email.\n';
		}
		
		if(dojo.byId('send-to').value == '' || dojo.byId('send-to').value == 'enter e-mail addresses separated with either a comma OR semicolon') {
			error += 'Please specify at least one recipient\'s email for your card.\n';
		}
		else {
			var e = new Array();
			var a = dojo.byId('send-to').value.split(',');
			
			for(var i = 0; i < a.length; i++) {
				if(a[i].split(';').length == 1) {
					e.push(a[i].replace(/ /g, ''));
				}
				else {
					var s = a[i].split(';')
					for(var j = 0; j < s.length; j++) {
						e.push(s[j].replace(/ /g, ''));
					}
				}
			}
			
			for(var i = 0; i < e.length; i++) {
				if(!email_pattern.test(e[i])) {
					error += '"' + e[i] + '" is not a valid email address.\n';
				}
			}
		}
		
		if(error != '') {
			return error;
		}
		else {
			return 'Ok';
		}
	},
	
	setFontStyleFromForm: function() {
		var ffamily = dojo.byId('send-message').style.fontFamily;
		var fweight = dojo.byId('send-message').style.fontWeight;
		var fstyle = dojo.byId('send-message').style.fontStyle;
		var options = dojo.byId('send-font').getElementsByTagName('option');
		
		for(var i = 0; i < options.length; i++) {
			if(options[i].value == ffamily) {
				options[i].selected = 'selected';
				break;
			}
		}
		
		if(fweight == 'bold') {
			document.orderForm.pn1608.checked = 'checked';
		}
		
		if(fstyle == 'italic') {
			document.orderForm.pn1609.checked = 'checked';
		}
	}
}


var SuggestedMessages = {
	opened_window: null,
	
	init: function() {
		dojo.byId('suggested-messages').onclick = SuggestedMessages.show;
	},
	
	show: function() {
		SuggestedMessages.opened_window = window.open(host + '/share/message?path=' + path, 'suggested_messages', 'width=500, height=400, status=0, toolbar=0, scrollbars=1');
		return false;
	},
	
	insert: function(message) {
		dojo.byId('send-message').value += message;
		SuggestedMessages.opened_window.close();
	}
}


if(dojo.byId('custom-page')) {
	SendingForm.init();
	SendingForm.setFontStyleFromForm();
}

