// JavaScript Document
/**
 *				# Outer Senshi Realm #
 *  @Filename	# static/formInputValidator.js #

 *	@Written	2009-02-18 @ 22:07
 *	@LastRev	2009-02-18 @ 22:07
 *
 * 	@Author 	Martin Th Sonne
 *	@Email
 * 	@Copyright	2009 Martin Th Sonne
 */
 
 
 
/**********************************************************************/
// Alt hvad der er markered med en stjerne(*) gälder for mere end en side
// Indeksne er sat efter formular navnet. Et formular navn skal altid v're
// som følgene: *Form
// Stjernen kan väre hvad som helst, bare det er en streng
		
// Gælder for side: .php	
// Formular: 	
// Hvis RequestAction er lig med (?) ...
	
/**********************************************************************/
// Index: A
//
//
		

	
/**********************************************************************/
// Index: B
//
//
	


/**********************************************************************/
// Index: C
//
//



	

/**********************************************************************/
// Index: D
//
//
	
function dE(n, s){
	var e = document.getElementById(n);

	if (!s){
		s = (e.style.display == '' || e.style.display == 'block') ? -1 : 1;
	}
	e.style.display = (s == 1) ? 'block' : 'none';
}

/**********************************************************************/
// Index: E
//
//


//
//	Evaluate E-mail Address
//
function EvaluateEmail(str) {

	var at="@";
	var dot=".";
	
	var sAlertMessage = "Artemis\n\nnIvalid e-mail address provided.\n\nPlease provide a valid e-mail address!\n E.g. fnale.lname@domain.com";
	
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	
	if (str.indexOf(at)==-1){
	   alert(sAlertMessage);
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert(sAlertMessage);
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert(sAlertMessage);
	    return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert(sAlertMessage)
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert(sAlertMessage);
	    return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert(sAlertMessage);
	    return false;
	 }
		
	 if (str.indexOf(" ")!=-1){
	    alert(sAlertMessage);
	    return false;
	 }

	 return true;					
}



//
//	Adv. Email Validation
//
function checkValidation(formInput) {

    if (typeof(formInput) != "object") {
        alert("Validation not supported on this browser.");
        return(false);
    }

    var message;

    if (stringEmpty(formInput.value)) {
        message = "Error! There is no input value entered.";
        alert(message);
    } else if (noAtSign( formInput.value )) {
        message = "Error! The address \"" + formInput.value + "\" does not contain an '@' character.";
        alert(message);
    } else if (nothingBeforeAt(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain at least one character before the '@' character";
        alert(message);
    } else if (noLeftBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
        alert(message);
    } else if (noRightBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
        alert( message);
    } else if (noValidPeriod(formInput.value)) {
        message = "Error! The address \"" + formInput.value + "\" must contain a period ('.') character.";
        alert(message);
    } else if (noValidSuffix(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain a two, three or four character suffix.";
        alert(message);
    } else {
        message = "Success! The email address \"" + formInput.value + "\" validates OK.";
        alert(message);
    }

    var objType = typeof(formInput.focus);
    if (objType == "object" || objType == "function") {
         formInput.focus();
    }

    return (false);
}

function checkValid (formField) {
    if ( checkValidation ( formField ) == true ) {
        alert ( 'E-Mail Address Validates OK' );
    }

    return ( false );
}

function stringEmpty (formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}


	

/**********************************************************************/
// Index: F
//
//
	
	

/**********************************************************************/
// Index: G
//
//
	
	

/**********************************************************************/
// Index: H
//
//
	
	

/**********************************************************************/
// Index: I
//
//
	
	

/**********************************************************************/
// Index: J
//
//

function jumpto() {
	var page = prompt(jump_page, on_page);

	if((page !== null) && (!isNaN(page)) && (page > 0)){
		document.location.href = base_url.replace(/&amp;/g, '&') + '&start=' + ((page - 1) * per_page);
	}
}
	
	

/**********************************************************************/
// Index: K
//
//
	

/**********************************************************************/
// Index: L
//
//

	
	
/**********************************************************************/
// Index: M
//
//

function marklist(id, name, state) {
	var parent = document.getElementById(id);
	if (!parent){
		eval('parent = document.' + id);
	}

	if (!parent) {
		return;
	}

	var rb = parent.getElementsByTagName('input');
	
	for (var r = 0; r < rb.length; r++){	
		if (rb[r].name.substr(0, name.length) == name) {
			rb[r].checked = state;
		}
	}
}


	
/**********************************************************************/
// Index: N
//
	
	

/**********************************************************************/
// Index: O
//
//
	
	

/**********************************************************************/
// Index: P
//
//

function popup(url, width, height, name) {
	if (!name) {
		name = '_popup';
	}

	window.open(url.replace(/&amp;/g, '&'), name, 'height=' + height + ',resizable=yes,scrollbars=yes, width=' + width);
	return false;
}
	
	

/**********************************************************************/
// Index: Q
//
//
	
	

/**********************************************************************/
// Index: R
//
//

	
	
	
/**********************************************************************/
// Index: S
//
//	
	

//
//	Show Catagory
//
function ShowCatagory(strFCID) {
	if(strFCID == "0") {
		window.location.replace("/forum/");
		return true;
	}
	else {
		if(strFCID == "-1") {
			window.alert('Artemis:\n\nPlease select a catagory');
			return false;
		}
		else {
			window.location.replace("/forum/?fcid="+strFCID);
			return true;
		}
	}
}


//
//	Show Forum
//
function ShowForum(strFID) {
	if(strFID == "0") {
		window.location.replace("/forum/");
		return true;
	}
	else {
		if(strFID == "-1") {
			window.alert('Artemis:\n\nPlease select a forum!');
			return false;
		}
		else {
			window.location.replace("/forum/topiclist/?fid="+strFID);
			return true;
		}
	}
}



//
//	Show Forum Rules
//
function ShowForumRules(strFID, strReturnTo) {
	if(strFID == "-1") {
		if(strReturnTo != "") {
			strReturnTo = "?returnto="+strReturnTo;	
		}
		
		window.location.replace("/forum/rules/"+strReturnTo);
		return false;
	}
	else {
		if(strReturnTo != "") {
			strReturnTo = "&returnto="+strReturnTo;	
		}		
		
		window.location.replace("/forum/rules/?fid="+strFID+strReturnTo);
		return true;
	}
}

/**
 *
 */
function selectCode(a)
{
	// Get ID of code block
	var e = a.parentNode.parentNode.getElementsByTagName('CODE')[0];

	// Not IE
	if (window.getSelection) {
		
		var s = window.getSelection();
		// Safari
		if (s.setBaseAndExtent) {
			s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
		}	// Firefox and Opera
		else
		{
			var r = document.createRange();
			r.selectNodeContents(e);
			s.removeAllRanges();
			s.addRange(r);
		}
		
	}	// Some older browsers
	else if(document.getSelection) {
		
		var s = document.getSelection();
		var r = document.createRange();
		r.selectNodeContents(e);
		s.removeAllRanges();
		s.addRange(r);
	}	// IE
	else if (document.selection) {
		
		var r = document.body.createTextRange();
		r.moveToElementText(e);
		r.select();
	}
} 


function subPanels(p)
{
	var i, e, t;

	if (typeof(p) == 'string')
	{
		show_panel = p;
	}

	for (i = 0; i < panels.length; i++)
	{
		e = document.getElementById(panels[i]);
		t = document.getElementById(panels[i] + '-tab');
		tt = document.getElementById(panels[i] + '-tab-selected');

		if (e)
		{
			if (panels[i] == show_panel)
			{
				e.style.display = 'block';
				if (t)
				{
					t.className = '';
					tt.className = 'tab_selected';
				}
			}
			else
			{
				e.style.display = 'none';
				if (t)
				{
					t.className = '';
					tt.className = 'tab_nonselected';
				}
			}
		}
	}
}
	
/**********************************************************************/
// Index: T
//	
//
	
	

/**********************************************************************/
// Index: U
//
//


/**********************************************************************/
// Index: U
//
//
function viewableArea(e, itself) {
	if (!e) return;
	if (!itself) {
		e = e.parentNode;
	}
	
	if (!e.vaHeight){
		// Store viewable area height before changing style to auto
		e.vaHeight = e.offsetHeight;
		e.vaMaxHeight = e.style.maxHeight;
		e.style.height = 'auto';
		e.style.maxHeight = 'none';
		e.style.overflow = 'visible';
	}
	else {
		// Restore viewable area height to the default
		e.style.height = e.vaHeight + 'px';
		e.style.overflow = 'auto';
		e.style.maxHeight = e.vaMaxHeight;
		e.vaHeight = false;
	}
}