//////////////////////////////////////////////////////////////////////////////////
// FLASH DETECTION VARIABLES
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;
// -----------------------------------------------------------------------------

// genetal report bad link function
	function reportBadLink(thisObj){
		var reportLinkControllerURL = contextPath + '/reportURL.htm';
		new Ajax.Request(reportLinkControllerURL,
		  {
			method:'get',
			parameters: {reportURL: window.location.toString()},
			onSuccess: function(transport){ thisObj.innerHTML = 'Reported, thank you!'; },
			onFailure: function(){ alert('Error while reporting link') }
		  });
		return false;
	}

// general popup function
    function popup( url, name, w, h, x, y, scroll, status )
    {   if( ( name == "" ) || ( name == null ) )
        {   var Winname = "ALLOY_POPUP";
        }
        else
        {   var Winname = name;
        }
        if( ( scroll == "" ) || ( scroll == null ) )
        {   var scrollBar = "auto";
        }
        else
        {   var scrollBar = scroll;
        }
        if( ( status == "" ) || ( status == null ) )
        {   var statusBar = "auto";
        }
        else
        {   var statusBar = status;
        }
        window.open( url, Winname, 'resizable=yes,menubar=no,toolbar=no,location=no,status='+statusBar+',scrollbars='+scrollBar+',width='+w+',height='+h+',left='+x+',top='+y );
    }
//////////////////////////////////////////////////////////////////////////////////
// Email.js version 5
var tld_ = new Array()
tld_[0] = "com";
tld_[1] = "org";
tld_[2] = "net";
tld_[3] = "ws";
tld_[4] = "info";
tld_[10] = "co.uk";
tld_[11] = "org.uk";
tld_[12] = "gov.uk";
tld_[13] = "ac.uk";
var topDom_ = 13;
var m_ = "mailto:";
var a_ = "@";
var d_ = ".";

function mail(name, dom, tl, params)
{
	var s = e(name,dom,tl);
	document.write('<a href="'+m_+s+params+'">'+s+'</a>');
}
function mail2(name, dom, tl, params, display)
{
	document.write('<a href="'+m_+e(name,dom,tl)+params+'">'+display+'</a>');
}
function e(name, dom, tl)
{
	var s = name+a_;
	if (tl!=-2)
	{
		s+= dom;
		if (tl>=0)
			s+= d_+tld_[tl];
	}
	else
		s+= swapper(dom);
	return s;
}
function swapper(d)
{
	var s = "";
	for (var i=0; i<d.length; i+=2)
		if (i+1==d.length)
			s+= d.charAt(i)
		else
			s+= d.charAt(i+1)+d.charAt(i);
	return s.replace(/\?/g,'.');
}


//////////////////////////////////////////////////////////////////////////////////
// daily poll popup function
    function submitHomepagePoll( theForm )
    {   formString = Form.serialize( theForm );
        var myAjax = new Ajax.Updater(
            { success: 'pollVoteBloc' },
            theForm.action,
            { method: 'post',
              parameters: formString,
              onFailure: reportError,
              evalScripts: true
            }
        );
    }
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
    function launchSendToFriend( earl )
    {   if( ( contextPath == "" ) || ( contextPath == null ) )
        {   contextPath = "";
        }
        if( ( earl == "" ) || ( earl == null ) )
        {   popup( contextPath + "/friends/email/sendto/" + escape( document.location.href ) + "/" + document.title, "FRIEND_POPUP", 425, 500, 100, 100 );
        }
        else
        {   popup( contextPath + "/friends/email/sendto/" + escape( earl ) + "/" + document.title, "FRIEND_POPUP", 425, 500, 100, 100 );
        }
    }
    function launchSendToFriend()
    {   if( ( contextPath == "" ) || ( contextPath == null ) )
        {   contextPath = "";
        }
        popup( contextPath + "/friends/email/sendto/" + escape( document.location.href ) + "/" + document.title, "FRIEND_POPUP", 425, 500, 100, 100 );
    }
    function openInviteFriend()
    {   //popup( contextPath + "/friends/invite/email", "STF", 500, 570, 50, 100, 0, 1 )
        document.location = contextPath + "/friends/invite/email";
    }
    function openerInviteFriend()
    {   //popup( contextPath + "/friends/invite/email", "STF", 500, 570, 50, 100, 0, 1 )
        window.opener.document.location = contextPath + "/friends/invite/email";
    }
    function openInviteContest() {   
		//popup( contextPath + "/contests/invite/index.htm", "STF", 560, 570, 50, 100, 0, 1 )
		popup( contextPath + "/contests/invite/index.htm", "STF", 560, 400, 50, 100, 0, 1 )
    }
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
// are you sure function
    function areYouSure( url, type, id )
    {   var answer = confirm( "Are you sure you want to delete " + type + " " + id + "?" );
        if( answer )
        {   document.location = url;
        }
    }
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
// ajax form validation
function combineDob()
{   var dobMonth = $F('dobMonth');
    var dobDay = $F('dobDay');
    var dobYear = $F('dobYear');
    if( ( dobMonth != null && dobMonth != "" ) &&
        ( dobDay != null && dobDay != "" ) &&
        ( dobYear != null && dobYear != "" ) )
    {   $('birthDate').value = dobMonth + "/" + dobDay + "/" + dobYear;
    }
}

function ajaxValidate( theForm )
{   combineDob();
    var elements = Form.getElements( theForm );
    for( i = 0; i < elements.length; i++ )
    {   if( elements[i].name != "" )
        {   if( $( elements[i].name + "Error" ) )
            {   $( elements[i].name + "Error" ).innerHTML = "";
            }
        }
    }
    $( 'ajaxForm' ).value = "true";
    formString = Form.serialize( theForm );
    Form.disable( theForm );
    var myAjax = new Ajax.Updater(
        { success: 'validationHolder' },
        theForm.action,
        { method: 'post',
          parameters: formString,
          onFailure: reportError,
          evalScripts: true
        }
    );
}

function ajaxFilter( theForm )
{   var elements = Form.getElements( theForm );
    $( 'groupResults' ).innerHTML = "";
    $( 'ajaxForm' ).value = "true";
    formString = Form.serialize( theForm );
    Form.disable( theForm );
    var myAjax = new Ajax.Updater(
        { success: 'filterResponse' },
        theForm.action,
        { method: 'post',
          parameters: formString,
          onFailure: reportFilterError,
          evalScripts: true
        }
    );
}
function ajaxPostMessage( theForm, theResponseDivId )
{
    var elements = Form.getElements( theForm );

    $( 'ajaxForm' ).value = "true";
    formString = Form.serialize( theForm );
    Form.disable( theForm );
    var myAjax = new Ajax.Updater(
        { success: theResponseDivId },
        theForm.action,
        { method: 'post',
          parameters: formString,
          evalScripts: true
        }
    );
}

function ajaxRate( theForm, ratingId, systemId, theValue )
{

    theForm.elements["value"].value=theValue;
    formString = Form.serialize( theForm );
    Form.disable( theForm );
    //$("ratingWorking-" + ratingId + "-" + systemId).style.display="";
    var myAjax = new Ajax.Updater(
        { success: 'ratingSuccess-' + ratingId + '-' + systemId },
        theForm.action,
        { method: theForm.method,
          parameters: formString,
          onFailure: reportRateError,
          evalScripts: true
        }
    );
    //$("ratingWorking-" + ratingId + "-" + systemId).style.display="none";
    //Form.enable( theForm );
}

function ajaxSubmit( theForm )
{   combineDob();
    $( 'ajaxForm' ).value = "false";
    formString = Form.serialize( theForm );
    var myAjax = new Ajax.Request(
        theForm.action,
        { method: 'post',
          parameters: formString
        }
    );
}

var myGlobalHandlers = {
    onCreate: function(){
        Element.show( 'systemWorking' );
    },
    onComplete: function() {
        if( Ajax.activeRequestCount == 0 ){
            Element.hide( 'systemWorking' );
        }
    }
};
Ajax.Responders.register( myGlobalHandlers );

function showResponse( request )
{   //put returned XML in the textarea
    if( request.readyState == 4 )
    {   if( request.status == 200 )
        {   eval( request.responseText );
        }
        else
        {   alert( "There was a problem validating this form:\n" + request.statusText );
        }
    }
    //$('validationHolder').value = request.responseText;
}

function reportError( request )
{   alert( "ERROR: There was a problem validating this form:\n" + request.statusText );
}

function reportFilterError( request )
{   alert( "ERROR: There was a problem filtering:\n" + request.statusText );
}

function reportRateError( request )
{   alert( "ERROR: There was a problem rating this group:\n" + request.statusText );
}


function swapImage( imageId, newSrc )
{   $( imageId ).src = newSrc;
}
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
// Login page show/hide function
function toggleThisElement( elemToToggle, elemToFocus )
{   Element.toggle( $( elemToToggle ) );
    if( Element.visible( $( elemToToggle ) ) )
    {   $( elemToFocus ).focus();
    }
}
//////////////////////////////////////////////////////////////////////////////////
function lastMod(time) {
    var x = new Date (time);

    Modif = new Date(x.toGMTString());
    Year = takeYear(Modif);
    Month = Modif.getMonth();
    Day = Modif.getDate();
    Hours = Modif.getHours();
    Minutes = Modif.getMinutes();
    Seconds = Modif.getSeconds();

    Mod = (Date.UTC(Year,Month,Day,Hours,Minutes,Seconds))/1000;
    x = new Date();
    today = new Date(x.toGMTString());
    Year2 = takeYear(today);
    Month2 = today.getMonth();
    Day2 = today.getDate();
    Hours2 = today.getHours();
    Minutes2 = today.getMinutes();
    Seconds2 = today.getSeconds();
    now = (Date.UTC(Year2,Month2,Day2,Hours2,Minutes2,Seconds2))/1000;

    secondsago = now - Mod;

    if (secondsago < 1) return 'less than an second ago';
    unit = 'seconds';

    if (secondsago > 29030400)  {
        secondsago = Math.floor(secondsago/29030400);
        if(secondsago == 1)
        {
                unit='year'
        }else
        {
            unit = 'years';
        }
    }
    else if (secondsago > 2419200) {
        secondsago = Math.floor(secondsago/2419200);
        if(secondsago == 1)
        {
                unit='month'
        }else
        {
            unit = 'months';
        }
    }
    else if (secondsago > 604800) {
        secondsago = Math.floor(secondsago/604800);
        if(secondsago == 1)
        {
            unit='week'
        }else
        {
            unit = 'weeks';
        }

    }else if (secondsago > 86400) {
        secondsago = Math.floor(secondsago/86400);
        if(secondsago == 1)
        {
            unit='day'
        }else
        {
            unit = 'days';
        }
    }else if (secondsago > 3600 ) {
        secondsago = Math.floor(secondsago/3600);
        if(secondsago == 1)
        {
            unit='hour'
        }else
        {
            unit = 'hours';
        }
    }
    else if (secondsago > 60 ) {
        secondsago = Math.floor(secondsago/60);
        if(secondsago == 1)
        {
            unit='minute'
        }else
        {
            unit = 'minutes';
        }
    }

    var towrite = '';

    towrite += secondsago + ' ' + unit + ' ago';
    return towrite;
}

//////////////////////////////////////////////////////////////////////////////////
// 
function takeYear(theDate) {
    x = theDate.getYear();
    var y = x % 100;
    y += (y < 38) ? 2000 : 1900;
    return y;
}


//////////////////////////////////////////////////////////////////////////////////
// chop the string with the "..."
function dotDotDotString(s, l) {
	if (s.length > l) {
		return s.substring(0, l) + "...";
	} else {
		return s;					
	}
}

//////////////////////////////////////////////////////////////////////////////////
// Text fields clear onfocus
function clearTxt(thefield) {
	if (thefield.defaultValue==thefield.value)
	document.lg_srch.query.value = "";
}
function clearTxtNav(thefield) {
	if (thefield.defaultValue==thefield.value)
	document.lg_nav_srch2.query.value = "";
}

//////////////////////////////////////////////////////////////////////////////////

