/*****************************************
Rollover script
 *****************************************/
function VMaxOver(link,id,overRoll) {
  if(document.images) {
    roll            = document.images[id];
    roll.outRoll      = new Image();
    roll.overRoll     = new Image();
    roll.outRoll.src  = document.images[id].src;
    roll.overRoll.src = overRoll;
    roll.src        =roll.overRoll.src;
    link.onmouseout  = new Function("var roll=document."+id+"; roll.src=roll.outRoll.src;");
    link.onmouseover = new Function("var roll=document."+id+"; roll.src=roll.overRoll.src;");
  }
}

/*****************************************
Popup - no scrollbars
 *****************************************/
function popup(page, windowname, wide, high) {

	window.open(page,windowname,'width=' + wide + ',height=' + high);
}



/*****************************************
BBCODE insert into forms
 *****************************************/

// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or prompts the user for some
// text to apply the tag to
function insertcode(tag, desc)
{
    // our textfield
    var textarea = document.getElementById("page_main_content");

    // our open tag
    var open = "[" + tag + "]";

    // our close tag
    var close = "[/" + tag + "]";

    if(!textarea.setSelectionRange)
    {
        var selected = document.selection.createRange().text; 
        if(selected.length <= 0)
        { 
            // no text was selected so prompt the user for some text
            textarea.value += open + prompt("Please enter the text you'd like to " + desc, "") + close;
        }
        else
        {
            // put the code around the selected text
            document.selection.createRange().text = open + selected + close; 
        }

    }
    else
    {
        // the text before the selection
        var pretext = textarea.value.substring(0, b.selectionStart);
        
        // the selected text with tags before and after
        var codetext = open + textarea.value.substring(b.selectionStart, b.selectionEnd) + close;

        // the text after the selection
        var posttext = textarea.value.substring(b.selectionEnd, textarea.value.length)
        
        // check if there was a selection
        if(codetext == open + close)
        {
            //prompt the user
            codetext = open + prompt("Please enter the text you'd like to " + desc, "") + close;
        }

        // update the text field
        textarea.value = pretext + codetext + posttext;
    }

    // set the focus on the text field
    textarea.focus();
}



// inserts a link by prompting the user for a url
function insertlink()
{
    // our textfield
    var textarea = document.getElementById("page_main_content");

    // our link
    var url = prompt("Please enter the url without the http:// prefix", "www.");
    var link = "[URL=http://" + url + " target=_blank>" + url + "[/URL]";

    if(!textarea.setSelectionRange)
    {
        // get selected text
        var selected = document.selection.createRange().text; 

        if(selected.length <= 0)
        { 
            // no text was selected so add the link to the end
            textarea.value += link;
        }
        else
        {
            // replace the selection with the link
            document.selection.createRange().text = link; 
        }
    }
    else
    {
        // the text before the selection
        var pretext = textarea.value.substring(0, b.selectionStart);

        // the text after the selection
        var posttext = textarea.value.substring(b.selectionEnd, textarea.value.length)

        // update the text field
        textarea.value = pretext + link + posttext;
    }

    // set the focus on the text field
    textarea.focus();
}

/*****************************************
BBCODE insert single items into textarea
 *****************************************/

function addItem(singleitem)
{
document.mainpages.page_main_content.value=document.mainpages.page_main_content.value+singleitem;
document.mainpages.page_main_content.focus();
return;
}


/*****************************************
SIMPLE EMAIL VALIDATOR
 *****************************************/

function checkEmail(email) {

var str = new String(email);
var isOK = true;
rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
if( rExp.test(str) )
isOK = false;
if( str.indexOf('.') == -1 || str.indexOf('@') == -1 )
isOK = false;
if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
isOK = false;
if( str.slice(0,str.indexOf('@')).length < 1 )
isOK = false;
if( str.slice(str.indexOf('@')+1,str.lastIndexOf('.')).length < 1 )
isOK = false;

if( !isOK )
alert( "Please enter a valid email address" );

return isOK;
}

