
//Set big gift image
function setGiftIcon(id,path)
{
	document.frmInvite.giftId.value = id;
}

//Check big gift form
function chkGiveGifts()
{
	var msg = new alertMessage(TEXT_pleasecheckthefollowing);
	var form = document.frmInvite;
	//gift
	if (isNaN(form.giftId.value))
	{
		msg.add(TEXT_giftmissing,'message');
	}
	//message
	if(!isString(form.message,2))
	{
		msg.add(TEXT_messagemissing,'message');
	}
	//submit
	if (msg.length() > 0)
	{
		alert(unescape(msg.get()));
		eval("document.frmInvite." + msg.field()).select();
	}
	else
	{
		form.submit();
	}

}

//Check gift form
function chkGift()
{
	var msg = new alertMessage(TEXT_pleasecheckthefollowing);
	var form = document.frmGift;
	//gift
	if (isNaN(form.giftId.value))
	{
		msg.add(TEXT_giftmissing,'message');
	}
	//message
	if(!isString(form.message,2))
	{
		msg.add(TEXT_messagemissing,'message');
	}
	//name
	if(!isName(form.name))
	{
		msg.add(TEXT_nameincorrect,'name');
	}
	//fromemail
	if(!isEmail(form.fromEmail))
	{
		msg.add(TEXT_emailincorrect,'fromEmail');
	}
	//to name
	if(!isName(form.toName))
	{
		msg.add(TEXT_tonameincorrect,'toName');
	}
	//to email
	if(!isEmail(form.toEmail))
	{
		msg.add(TEXT_toemailincorrect,'toEmail');
	}
	//submit
	if (msg.length() > 0)
	{
		alert(unescape(msg.get()));
		eval("document.frmGift." + msg.field()).select();
	}
	else
	{
		form.submit();
	}
}

//Set gift image
function setIcon(id,path)
{
	document.frmGift.giftId.value = id;
	document.imageGift.src = "/images/gifts/" + path;
}

//Find and invite friends by email
function chkFindbyemail()
{
	var msg = new alertMessage(TEXT_pleasecheckthefollowing);
	var form = document.frmFindbyemail;
	var isFriends = false;
	
	//email
	if(!isEmail(form.userEmail))
	{
		msg.add(TEXT_emailincorrect,'userEmail');
	}
	for(var i=1; i < 6; i++)
	{
		obj = eval("document.frmFindbyemail.emailF"+i);
		if(obj!=undefined)
		{
			eval("document.frmFindbyemail.inviteF"+i).value = "off";
			if(obj.value.length > 0)
			{
				isFriends = true;
				if(!isEmail(obj))
				{
					msg.add(TEXT_emailincorrect,'emailF'+i);
				}
				else if(obj.value == form.userEmail.value)
				{
					msg.add(TEXT_cannotemailyourself,'emailF'+i);
				}
				else
				{
					eval("document.frmFindbyemail.inviteF"+i).value = "on";
				}
			}	
		}
	}
	if(!isFriends)
	{
		msg.add(TEXT_nofriendemails,'emailF1');
	}
	//submit
	if (msg.length() > 0)
	{
		alert(unescape(msg.get()));
		eval("document.frmFindbyemail." + msg.field()).select();
	}
	else
	{
		form.submit();
	}
}

//Find and invite friends by email
function chkFindbyemail2()
{
	var msg = new alertMessage(TEXT_pleasecheckthefollowing);
	var form = document.frmFindbyemail;
	var isFriends = false;
	
	//email
	if(!isEmail(form.userEmail))
	{
		msg.add(TEXT_emailincorrect,'userEmail');
	}
	for(var i=1; i < 6; i++)
	{
		obj = eval("document.frmFindbyemail.emailF"+i);
		if(obj!=undefined)
		{
			eval("document.frmFindbyemail.inviteF"+i).value = "off";
			if(obj.value.length > 0)
			{
				isFriends = true;
				if(!isEmail(obj))
				{
					msg.add(TEXT_emailincorrect,'emailF'+i);
				}
				else if(obj.value == form.userEmail.value)
				{
					msg.add(TEXT_cannotemailyourself,'emailF'+i);
				}
				else
				{
					eval("document.frmFindbyemail.inviteF"+i).value = "on";
				}
			}	
		}
	}
	if(!isFriends)
	{
		msg.add(TEXT_nofriendemails,'emailF1');
	}
	//submit
	if (msg.length() > 0)
	{
		alert(unescape(msg.get()));
		eval("document.frmFindbyemail." + msg.field()).select();
	}
	else
	{
		postGifts();
		//form.submit();
	}
}

//Count and restrict textfield
function countChrs(obj,max,display)
{
	var chrColor;
	if (max - obj.value.length < 11)
	{
		chrColor = "FF0000";
	}
	else
	{
		chrColor = "000000";
	}
	if (obj.value.length > max)
	{
		obj.value = obj.value.substr(0,max);
	}
	lookupObject(display).innerHTML = "<font color=\"#" + chrColor + "\">" + (max-obj.value.length) + "</font>";
}




function chkSwapEmail()
{
	form = document.frmSwapemail;
	var msg = new alertMessage(TEXT_pleasecheckthefollowing);
	if(!isEmail(form.emailold))
	{
		msg.add(TEXT_emailincorrect,'emailold');
		alert(unescape(msg.get()));
		eval("document.frmSwapemail." + msg.field()).select();
	}
	else if (!isEmail(form.emailnew))
	{
		msg.add(TEXT_emailincorrect,'emailnew');
		alert(unescape(msg.get()));
		eval("document.frmSwapemail." + msg.field()).select();
	}
	else
	{
		form.submit();
	}
}


function chkSignout()
{
	form = document.frmSignout;
	var msg = new alertMessage(TEXT_pleasecheckthefollowing);
	//email
	if(!isEmail(form.emailconfirm))
	{
		msg.add(TEXT_emailincorrect,'emailconfirm');
	}
	//submit
	if (msg.length() > 0)
	{
		alert(unescape(msg.get()));
		eval("document.frmSignout." + msg.field()).select();
	}
	else
	{
		form.submit();
	}

}


//Check if a valid date
function isDate(day,month,year)
{
	if(! (day < 1 || day > 31 || isNaN(parseInt(day))) )
	{
		tmp = new Date(year,month,day);
		if (tmp.getMonth() != month)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}



function showTopNav()
{
	if (top.location == document.location)
	{
		showObject("topnav");
	}
}


function chkFind()
{

	var msg = new alertMessage(TEXT_pleasecheckthefollowing);
	var form = document.frmFind;

	//username
	if(!isString(form.username,3))
	{
		msg.add(TEXT_usernameincorrect,'username');
	}
	//Password
	if(!isString(form.Password,3))
	{
		msg.add(TEXT_passwordincorrect,'Password');
	}
	//submit
	if (msg.length() > 0)
	{
		alert(unescape(msg.get()));
		eval("document.frmFind." + msg.field()).select();
		return false;
	}
	else {
	    alert('sending');
		togglePlaceholder('addressForm','none');
		togglePlaceholder('loading','block');
		__doPostBack('Submit','');
	}
}

function pickDomain(obj)
{
	var str;
	str = obj.options[obj.selectedIndex].text;
	if (obj.selectedIndex == obj.length-1)
	{
		obj.selectedIndex = 0;
		document.location.href = 'findbyemail.asp?' + window.location.search.substring(1);
	}
	lookupObject('formTable').style.backgroundImage = 'url(/images/find_' + str.substring(0,str.indexOf('.')) + '.gif)';
}


function alertAndDo(type)
{
	lookupObject('formTable').style.backgroundImage = 'url(/images/find_' + domain.substring(0,domain.indexOf('.')) + '.gif)';
	switch(type)
	{
		case 1:
			document.forms[0].username.focus();
		break;
		case 2:
			alert(unescape(TEXT_alertanddo2));
			document.forms[0].Password.focus();
		break;
		case 3:
			alert(unescape(TEXT_alertanddo3));
			document.forms[0].domainlist.selectedIndex = 0;
			pickDomain(document.forms[0].domainlist);
			togglePlaceholder('findFriends','block');
		break;
		case 4:
			document.forms[0].Password.focus();
		break;
		case 5:
			alert(unescape(TEXT_alertanddo5));
			document.forms[0].Password.focus();
		break;
		case 6:
			alert(unescape(TEXT_nofriendsinyouraddressbook));
			document.forms[0].username.focus();
		break;
	}
}


function toggleAll(cb)
{
	var val = cb.checked;
	var frm = document.forms[0];
	var len = frm.elements.length;
	var i=0;
	for( i=0 ; i<len ; i++)
	{
		if (frm.elements[i].type=="checkbox" && frm.elements[i] != cb )
		{
			if (!frm.elements[i].disabled)
			{
				frm.elements[i].checked=val;
			}
		}
	}
}

function amendEmail()
{
	var obj = document.forms[0].username;
	var domain;
	var	tmpArr;
	if (isEmail(obj))
	{
		tmpArr = obj.value.split("@");
		domain = tmpArr[1];
		for (n = 0; n < document.forms[0].domainlist.length; n++ )
		{			
			if (domain == document.forms[0].domainlist.options[n].value)
			{
				obj.value = tmpArr[0];
				document.forms[0].domainlist.selectedIndex = n;
				document.forms[0].Password.focus();
				break;
			}
		}
	}
	pickDomain(document.forms[0].domainlist);
}


function togglePlaceholder(placeholder,visible)
{
	var obj, vis;
	if (document.getElementById)
	{
		obj = document.getElementById(placeholder);
	}
	else if (document.all)
	{
		obj = document.all[placeholder];
	}
	else if (document.layers)
	{
		obj = document.layers[placeholder];
		
	}
	vis = obj.style;
	vis.display = visible;
}


//submit forms on return key
function chkReturn(e, action)
{
	if (((window.event) ?  e.keyCode : e.which) == 13)
	{
		switch(action)
		{
			case 'findbyemail':
				chkFindbyemail();
			break;
			case 'signup':
				chkSignup();
			break;    
			case 'find':
				chkFind();
			break;
			case 'invite':
				document.frmInvite.submit();
			break;
		} 
	}
}

//Signup
function chkSignup()
{
	var msg = new alertMessage(TEXT_pleasecheckthefollowing);
	var form = document.frmSignup;
	//name
	if(!isName(form.name))
	{
		msg.add(TEXT_nameincorrect,'name');
	}
	//email
	if(!isEmail(form.email))
	{
		msg.add(TEXT_emailincorrect,'email');
	}
	//city

	//if (lookupObject("obj_cityOther").style.display == "block" && (form.cityOther.value == form.TEXT_cityother.value || form.cityOther.value.length < 2))
	//{
	//	msg.add(TEXT_cityempty,'cityOther');
	//}

	//submit
	if (msg.length() > 0)
	{
		alert(unescape(msg.get()));
		eval("document.frmSignup." + msg.field()).select();
	}
	else
	{
		form.submit();
	}
}

//Other city
function expandOtherCity(obj)
{
	var form = document.frmSignup;
	if(obj.options[obj.selectedIndex].value == "other")
	{
		form.cityOther.value = form.TEXT_cityother.value;
		showObject("obj_cityOther");
		document.frmSignup.cityOther.focus();
		document.frmSignup.cityOther.select();
	}
	else
	{
		hideObject("obj_cityOther");
	}
}






//Alerts
function alertMessage(message)
{
	var num = 0;
	this.message = message + '\n';
	this.length = function() {
		return num;
	}
	this.get = function()
	{
		return this.message + "\n\n" + TEXT_pleasetryagain;
	}
	this.field = function()
	{
		return this.focus;
	}
	this.add = function(str,focus)
	{
		num ++;
		if (this.focus == undefined)
		{	
			this.focus = focus;
		}
		this.message += '\n' + num + ') ' + str;
	}
}


//Clean up email address
function fixEmail(str)
{
	var arrDomains = [['yaaho.dk','yahoo.dk'],['yahooo.dk','yahoo.dk'],['yaaho.com','yahoo.com'],['yahooo.com','yahoo.com'],['gmale.com','gmail.com'],['gmial.com','gmail.com'],['gmali.com','gmail.com'],['yhaoo.com','yahoo.com'],['yhaoo.com','yahoo.com'],['yagoo.com','yahoo.com'],['homtail.com','hotmail.com'],['otmail.com','hotmail.com'],['fotmail.com','hotmail.com'],['hatmail.com','hotmail.com'],['hoitmail.com','hotmail.com'],['homail.com','hotmail.com'],['homal.com','hotmail.com'],['homial.com','hotmail.com'],['homil.com','hotmail.com'],['hormail.com','hotmail.com'],['hotail.com','hotmail.com'],['hotamail.com','hotmail.com'],['hotamal.com','hotmail.com'],['hotamil.com','hotmail.com'],['hotmael.com','hotmail.com'],['hotmai.com','hotmail.com'],['hotmaiil.com','hotmail.com'],['hotmail.com.uk','hotmail.co.uk'],['hotmail.uk','hotmail.co.uk'],['hotmail.con','hotmail.com'],['hotmail.is','hotmail.com'],['hotmail.vom','hotmail.com'],['hotmail.xom','hotmail.com'],['hotmaill.com','hotmail.com'],['hotmal.com','hotmail.com'],['hotmali.com','hotmail.com'],['hotmeil.is','hotmail.com'],['hotmial.com','hotmail.com'],['hotmil.com','hotmail.com'],['hotml.com','hotmail.com'],['hotmlai.com','hotmail.com'],['hotmsil.com','hotmail.com'],['hotnail.com','hotmail.com'],['hoymail.com','hotmail.com'],['htmail.com','hotmail.com'],['htomail.com','hotmail.com'],['motmail.com','hotmail.com'],['yaho.com','yahoo.com'],['gamil.com','gmail.com'],['gmil.com','gmail.com']];
	objEmail = str.split("@");
	domain = objEmail[1].toLowerCase();
	for(i = 0; i < arrDomains.length; i ++)
	{
		if(domain == arrDomains[i][0])
		{
			objEmail[1] = arrDomains[i][1];
			break;
		}
	}
	return (objEmail[0] + "@" + objEmail[1]);
}


//Check if a valid email address
function isEmail(obj)
{
	obj.value = (obj.value.replace(/[\ ]/g, '')).toLowerCase();
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(obj.value))
	{	
		obj.value = fixEmail(obj.value);
		return true;
	}
	else
	{
		return false;
	}
}


//Check if string is minimum length
function isString(obj,minlen)
{
	if (obj.value.length < minlen)
	{
		return false;
	}
	else
	{	
		return true;
	}
}


//Check CVC2
function isCVC2(obj)
{
	obj.value = obj.value.replace(/[\D]/g, '');
	if (obj.value.length != 3)
	{
		return false;
	}	
	else
	{
		return true;
	}
}


//Check credit card
function isCardnumber(obj)
{
	obj.value = obj.value.replace(/[\D]/g, '');
	if (obj.value.length > 10 && obj.value.length < 20)
	{
		return true;
	}	
	else
	{
		return false;
	}
}


//Check if a valid name (first name and last name)
function isName(obj)
{	
	obj.value = obj.value.replace(/^[ ]+|[ ]+$|[\_@'"]/g, '').toLowerCase();
	if(/^[^0-9]+[ ]+[^0-9]+$/.test(obj.value))
	{
		arr = obj.value.split(' ');
		str = '';
		for (n = 0; n < arr.length ; n++)
		{
			str += formatAsName(arr[n]) + ' ';
		}
		obj.value = str.replace(/[ ]$/,'');
		if(obj.value.length < 6)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}


//Format string as name
function formatAsName(str)
{
	tmp =  str.charAt(0).toUpperCase() + str.substring(1,str.length).toLowerCase();
	return tmp;
}


//Switch locale
function switchLanguage(language)
{
	document.location.href = 'http://' + language.options[language.selectedIndex].value + '.skoost.com';
}


//Popup window
function popWin(url)
{
	if(screen){popLeft = screen.width-500;popTop = 100;}
	pop = window.open(url,'popup','width=400,height=300,left='+popLeft+',top='+popTop+',scrollbars=yes')
	pop.focus();
}


function lookupObject(id)
{
	var obj;
	if (document.getElementById)
	{
		obj = document.getElementById(id);
	}
	else if (document.all)
	{
		obj = document.all[id];
	}
	else if (document.layers)
	{
		obj = document.layers[id];
	}
	return obj;
}


function showObject(id)
{
	var obj = lookupObject(id);
	if (obj)
	{
		obj.style.display = "block";
	}
}


function hideObject(id)
{
	var obj = lookupObject(id);
	if (obj)
	{
		obj.style.display = "none";
	}
}

function showPop(url)
{
	showObject("popbackground");
	showObject("popforeground");
	window.frames["ipop"].location.href = url;
}

function hidePop()
{
	hideObject('popbackground');
	hideObject('popforeground');
}
