// JavaScript Document

var surface = -1;

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showLayer(whichLayer)
{
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
	elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
	elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
	elem = document.layers[whichLayer];
	vis = elem.style;
	vis.display = 'block';
}

function hideLayer(whichLayer)
{
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
	elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
	elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
	elem = document.layers[whichLayer];
	vis = elem.style;
	vis.display = 'none';
}

function shareWithClient()
{
	$('#shareDiv').show();	
}

function shareSurface(_surfaceId)
{
	surface = _surfaceId;
	$('#shareDiv').show();	
	$('#friendsDiv').hide();
}

function askAFriend(_surfaceId)
{
	surface = _surfaceId;
	$('#friendsDiv').show();
	$('#shareDiv').hide();	
}

// Close button for any window
$('a.close-popup').live('click', function(){
	$(this).parents('.popup-box').hide();
	return false;
});

function submitShareForm()
{
	var f_name = document.getElementById("yourname").value;
	var f_email = document.getElementById("youremail").value;
	//var r_name = document.getElementById("recipientsname").value;
	var r_email = document.getElementById("recipientsemail").value;
	var subject = document.getElementById("subject").value;
	var message = document.getElementById("message").value;
	var type = getCheckedValue(document.getElementById("linktype"));
	var linkUrl = (type == "page") ? document.location.href : "http://www.formica180fx.com";
	var errorMsg = "";
	
	if (f_name == "" || f_email == "" || r_email == "")
	{
		errorMsg = "You haven't filled out all the required fields.";
	}
	else if (!areValidEmails(r_email))
	{
		errorMsg = "Emails entered are invalid.";
	}
	
	if (errorMsg != "")
	{
		alert(errorMsg);
	}
	else
	{
		if (surface != -1)
		{
			linkUrl += "?item=" + surface;
		}
		
		var allGood = true;
		
		if (allGood)
		{
			hideLayer("shareDiv");	
			
			//rname: r_name,
			
			$.ajax(
			{ 
				type:"GET", 
				url: "share_submit.php", 
				datatype:"html", 
				data:
				{
					name: f_name,
					email: f_email,
					remail: r_email,
					subject: subject,
					message: message,
					linkUrl: linkUrl,
					pageToken: pageToken
				}, 
				complete:function( data ) 
				{ 
					//$("address").html( data );
					//alert( "Ajax call complete.");
				}
			});
		}
	}
}

function areValidEmails(emails)
{
	var arSplit = emails.split(",");
	
	for (var i = 0; i < arSplit.length; i++)
	{
		if (!checkEmail(stringTrim(arSplit[i]))) return false;
	}
	
	return true;
}

function checkEmail(email) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) 
	{
		return false;
	}
	return true;
}

function stringTrim(w) 
{
	return w.replace(/^\s+|\s+$/g,"");
}

function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function initPage()
{
	MM_preloadImages('assets/images/template/nav_learn_1.gif','assets/images/template/nav_browse_1.gif','assets/images/template/nav_buy_1.gif');
}