﻿
var c9 = Class.create();
c9.visitSite = Class.create();


c9.visitSite.misc = {
    getCursor: function(dDom) {
        if (document.all)
            dDom.style.cursor = 'hand';
        else
            dDom.style.cursor = 'pointer';
    },
    returnFalse: function()
    { return false; }
}

c9.visitSite.sendToFriend = {
	sYourName : 'Your name',
	sYourEmail : 'Your e-mail',
	sFriendsName : 'Friends name',
	sFriendsEmail : 'Friends e-mail',
	iFriendsCount : 1,
	rEmailPattern : new RegExp('^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[.]([a-zA-Z]{2,4})$'),

	iTextboxRows : 15,
	iTextboxCharsPerRow : 45,
	
	handleKeys : function(e, dDom)
	{
		var evt = (e) ? e : window.event;
		var key = (evt.keyCode) ? evt.keyCode : evt.which;
		
		var iTextboxMax = this.iTextboxRows * this.iTextboxCharsPerRow;
		
		if(key != null) {
			key = parseInt(key, 10);

			if(this.countChars(dDom) >= iTextboxMax && !this.isUserFriendlyChar(key)) 
			{
				return false;
			}
		}

		return true;		
	},
	
	isUserFriendlyChar : function(val) 
	{
		// Backspace, Tab, Insert, and Delete
		if(val == 8 || val == 9 || val == 45 || val == 46)
			return true;

		// Ctrl, Alt, CapsLock, Home, End, and Arrows
		if((val > 16 && val < 21) || (val > 34 && val < 41))
			return true;

		// The rest
		return false;
    },
    
	countChars : function(dDom)
	{
		var rows = dDom.value.toString().split('\n')
		var r = 0;
		
		if (rows.length > 0)
		{
			for (i=0;i<rows.length;i++)
			{
				if ((i == rows.length -1))
				{
					r += rows[i].length;
				}
				else if (rows[i].length < this.iTextboxCharsPerRow)
				{
					r += this.iTextboxCharsPerRow;
				}
				else if (rows[i].length >= this.iTextboxCharsPerRow)
				{
					r += rows[i].length;
				}
			}
		}
		else
		{
			r = rows[0].length;
		}
		
		
		return r
	},
	
	handleFocus : function(dDom, bFocus, sMessage)
	{
		if (bFocus && dDom.value == sMessage)
		{
			dDom.value = '';
		}
		else if (!bFocus && dDom.value.length == 0)
		{
			dDom.value = sMessage;
		}
	},
	
	addFriends : function()
	{
		var ieAdd = 0;
		if (document.all)
			ieAdd = 4;
	
		if(c9.visitSite.sendToFriend.iFriendsCount < 3)
		{
			document.getElementById('friends').style.marginTop = (235 - ((58 + ieAdd) * c9.visitSite.sendToFriend.iFriendsCount)) + "px";
			c9.visitSite.sendToFriend.iFriendsCount++;
			document.getElementById('FriendEmail' + this.iFriendsCount).style.display = 'block';
			document.getElementById('FriendName' + this.iFriendsCount).style.display = 'block';
		}
		if(c9.visitSite.sendToFriend.iFriendsCount == 3)
		{
			document.getElementById('addFriendLink').style.visibility = 'hidden';
		}
	},
	
	validateString : function(dDom, sMatch)
	{
		if (dDom.value == sMatch || dDom.value.length > 0)
		{
			dDom.style.backgroundColor='white';
			dDom.style.color='#646464';
		}
	},
	
	validateEmail : function(dDom, sMatch)
	{ 
		if ((dDom.value == sMatch || dDom.value.length > 0) && this.rEmailPattern.test(dDom.value))
		{
			dDom.style.backgroundColor='white';
			dDom.style.color='#646464';
		}
		else
		{
			this.alertInput(dDom);
		}
	},
	
	alertInput : function(dDom)
	{
		dDom.style.backgroundColor = 'red';
		dDom.style.color = 'white';
	},
	
	validatePostcard : function()
	{
		var r = true;
		
		if (document.getElementById('SenderName').value == this.sYourName)
		{
			this.alertInput(document.getElementById('SenderName'));
			r = false;
		}
		
		if(document.getElementById('SenderEmail').value == this.sYourEmail)
		{
			this.alertInput(document.getElementById('SenderEmail'));
			r = false;
		}
		
		if (!this.rEmailPattern.test(document.getElementById('SenderEmail').value))
		{
			this.alertInput(document.getElementById('SenderEmail'));
			r = false;
		}
		
		for (i=1;i<=this.iFriendsCount;i++)
		{
			if(document.getElementById('FriendName' + i.toString()).value == this.sFriendsName)
			{
				this.alertInput(document.getElementById('FriendName' + i.toString()));
				r = false;
			}
			
			if(document.getElementById('FriendEmail' + i.toString()).value == this.sFriendsEmail)
			{
				this.alertInput(document.getElementById('FriendEmail' + i.toString()));
				r = false;
				
			}
			else
			{
				if (!this.rEmailPattern.test(document.getElementById('FriendEmail' + i.toString()).value))
				{
					this.alertInput(document.getElementById('FriendEmail' + i.toString()));
					r = false;
				}
			}
		}
		
		if(r)
		    broken.visitSite.page.sentPostCard();
		
		return r;
	}
}




