function brandSelect(brandid)
{
	document.getElementById('mainbrand').value = brandid;
	document.getElementById('brands').submit();
}

function getXMLHttpObject()
{
    var xmlHttp;
    try
    {
	    // Firefox, Opera 8.0+, Safari
	    xmlHttp=new XMLHttpRequest();
	    return xmlHttp;
    }
    catch (e)
    {
	    // Internet Explorer
	    try
	    {
		    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		    return xmlHttp;
	    }
	    catch (e)
	    {
		    try
		    {
			    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			    return xmlHttp;
		    }
		    catch (e)
		    {
			    alert("Your browser does not support AJAX!");
			    return null;
		    }
	    }
    }
}


function download(paper_id)
{
	var xmlHttp = getXMLHttpObject();

	if(xmlHttp == null)
		return;

	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			//document.getElementById("link" + labelId).innerHTML = xmlHttp.responseText;
			window.location.href(xmlHttp.responseText);
		}
	};
	url = "./index2.php?option=com_papers&task=downloadlogos&id=" + paper_id;


	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

 function checkEmail(str)
    {
	    var at="@";
	    var dot=".";
	    var lat=str.indexOf(at);
	    var lstr=str.length;
	    var ldot=str.indexOf(dot);
	    if (str.indexOf(at)==-1)
	    {
	       //alert("Invalid E-mail ID");
	       return false;
	    }

	    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	    {
	       //alert("Invalid E-mail ID");
	       return false;
	    }

	    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	    {
	        //alert("Invalid E-mail ID");
	        return false;
	    }

	    if (str.indexOf(at,(lat+1))!=-1)
	    {
	       //alert("Invalid E-mail ID");
	       return false;
	    }

	    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	    {
	       //alert("Invalid E-mail ID")
	       return false;
	    }

	    if (str.indexOf(dot,(lat+2))==-1)
	    {
	       //alert("Invalid E-mail ID");
	       return false;
	    }

	    if (str.indexOf(" ")!=-1)
	    {
	       //alert("Invalid E-mail ID");
	       return false;
	    }

	    return true;
    }

function nl2br (str, is_xhtml) {
	var breakTag = '';
	breakTag = '<br />';
	if (typeof is_xhtml != 'undefined' && !is_xhtml) {
		breakTag = '<br>';
	}
	return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

window.addEvent('domready', function(){


	var mask_height = $('brands-container').getStyle('height').toInt();
	var total_height = ($('brands').getStyle('height').toInt());
	var current_pos = 0;
	var row_height = 23;

	if (total_height > mask_height)
	{
		$('paper-profile-selections-down-link').setStyle('visibility', 'visible');
		$('paper-profile-selections-up-link').setStyle('visibility', 'visible');
	}



	$('paper-profile-selections-down-link').addEvent('click', function(e) {

		new Event(e).stop();

		/*
		var scroll = new Fx.Scroll($('brands-container'), {
			wait:false,
			duration: 400,
			transition: Fx.Transitions.Quad.easeInOut

		});

		//fx.start(current_pos*20, 20);
		scroll.start(0, 200);
		//alert('brands');
		*/
		current_pos++;
		var new_pos = (current_pos * row_height);

		//we need a few extra pixels (3) for this to show the last row.
		if (new_pos > ((total_height-mask_height)+3)) {
			current_pos--;
			new_pos = (current_pos * row_height);
		}
		$('brands').setStyle('margin-top', '-'+new_pos+'px');

	});


	$('paper-profile-selections-up-link').addEvent('click', function(e) {

		new Event(e).stop();

		/*
		var scroll = new Fx.Scroll($('brands-container'), {
			wait:false,
			duration: 400,
			transition: Fx.Transitions.Quad.easeInOut

		});

		//fx.start(current_pos*20, 20);
		scroll.start(0, 200);
		//alert('brands');
		*/

		current_pos--;
		if (current_pos < 0) current_pos = 0;
		var new_pos = (current_pos * row_height);
		$('brands').setStyle('margin-top', '-'+new_pos+'px');

	});

	var email_page_panel = new Fx.Slide('email-page-panel', {duration: 300}).hide();

	$('paper-profile-email-link').addEvent('click', function(e) {
		new Event(e).stop();
		email_page_panel.toggle();
	});

	$('cmdSendFriendSubmit').addEvent('click', function(e) {
		new Event(e).stop();

		var err = false;

		if ($('txtName').getValue() == '')
		{
			window.alert('Please fill in your name.');
			$('txtName').focus();
			err = true;
		}
		if ($('txtEmail').getValue() == '' && !err)
		{
			window.alert('Please fill in your email address.');
			$('txtEmail').focus();
			err = true;
		}
		else if (checkEmail($('txtEmail').getValue())==false && !err)
		{
			window.alert('Please enter a valid email address.');
			$('txtEmail').focus();
			err = true;
		}

		if ($('txtFriendName').getValue() == '' && !err)
		{
			window.alert('Please fill in your friend\'s name.');
			$('txtName').focus();
			err = true;
		}
		if ($('txtFriendEmail').getValue() == '' && !err)
		{
			window.alert('Please fill in your friend\'s email address.');
			$('txtFriendEmail').focus();
			err = true;
		}
		else if (checkEmail($('txtFriendEmail').getValue())==false && !err)
		{
			window.alert('Please enter a valid email address for your friend.');
			$('txtFriendEmail').focus();
			err = true;
		}

		if (!err) {

			$('log_res').setHTML('<img src="http://www.kwdoggett.com.au/components/com_papers/views/sendfriend/tmpl/images/ajaxload.gif" />');
			url = '/index2.php?option=com_papers&task=sendfriend';

			$('frmSendFriend').getFormElements().each(function(el){
				var name = el.name;
				var value = nl2br(el.getValue(), true);
				url += '&' + name + '=' + value;

			});

			email_page_panel.slideOut();

			new Ajax(url, {
				method: 'get',
				update: $('log_res')
			}).request();

			$('log_res').setStyle('padding-bottom', '15px');
		}

	});

});


