/**

 */
 
 /**
 
 TODO:// make functions iswhitespace etc to String prototypes
 
 make fixBlankImages a prototype of Image.
 
 **/
 

/** Whitespace characters. */
var whitespace = " \t\n\r";

/** function trim ; created @ 07.07.'03; last modified @ 25.08.2003 */
String.prototype.trim = function() {
  var temp = this.replace( /^(\s)*|(\s)*$/ , ""  ); /* remove leading and trailing spaces. */
      temp = temp.replace( /(\s)+/g        , " " ); /* remove inner duplicate spaces. */
	  temp = temp.replace( /\t|\r|\n/g     , ""  ); /* remove tabs newlines and carriage returns. */
      if( temp == " " ) temp = "";                  /* if what in hand is only " " remove it as well. */
	  return temp;
}

/** gets the last part of the string after "/" or "\" */
String.prototype.getLastPortion = function() {
	var RegEx = /\\|\//;
	var aryResult = this.split( RegEx );
	var temp;
	for( i = 0; i<aryResult.length; i++ ) {
		temp = aryResult[ i ];
	}
	return temp;
}

/** complement of getLastPortion */
String.prototype.getFirstPortion = function() {
	var RegEx = /\\|\//;
	var aryResult = this.split( RegEx );
	if( aryResult.length <= 0 ) return aryResult;
	if( aryResult.length == 1 ) return aryResult[ 0 ];
	var temp = "";
	for( i = 0; i<aryResult.length-1; i++ ) {
		if( i == 0 )
			temp = aryResult[ i ];
		else
			temp += "/" + aryResult[ i ];
	}
	return temp;
}

/** replaces the regular expression pattern -strRegEx- with "" */
String.prototype.remove = function( strRegEx ) {
	var temp = this;
	var regEx = new RegExp( strRegEx, "g" );
	temp = temp.replace( regEx, "" );
	return temp;
}

/**
 * If there is a blank image in the parent folder( i.e. img.src == "[parentfolder]" )
 * then replace it with strNewURL
 * @param objImage - Object reference of the image
 * @param strParentFolder - The name of the parent folder of the image. (in the image URL )
 * @param strNewURL - The new URL to be replaced.
 */
function fixBlankImages( objImage, strParentFolder, strNewURL ) {
	var strTest   = getLastPortion( objImage.src ).toLowerCase();
	var strParent = strParentFolder.toLowerCase();
	if( strTest == strParent || strTest == stParent + "\\" || stTest == strParent + "/" ) {
	 	objImage.src = strNewURL;
	}
}

/** @return true if s is null or empty.*/
function isEmpty(s) { return ((s == null) || (s.length == 0)) }

/** @return true if s is composed of only whitespace characters.*/
function isWhitespace (s) {   
	var i;
    /* Is s empty? */
    if (isEmpty(s)) return true;
	/*
	 * Search through string's characters one by one
     * until we find a non-whitespace character.
     * When we do, return false; if we don't, return true.
	 */
    for ( i = 0; i < s.length; i++ ) {   
        /* Check that current character isn't whitespace. */
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    /* All characters are whitespace. */
    return true;
}

/** @return true if c is a non-numeric letter.*/
function isLetter (c) { return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) ); }

/** @return true if c is a digit.*/
function isDigit (c) { return ((c >= "0") && (c <= "9")) }

/** @return true if c is a non-alphanumeric but valid e-mail character.*/
function isEmailChar (c) { return ((c == ".") || (c == "_") || (c == "@") || (c == "-")); }

/** @return true if s is a string composed of alphanumeric or e-mail characters only.*/
function hasValidChars (s) {
	var i;
    /*
	 * Search through string's characters one by one
     * until we find a non-alphanumeric character.
     * When we do, return false; if we don't, return true.
	 */
    for (i = 0; i < s.length; i++) {
        /* Check that current character is a number or letter. */
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) || isEmailChar(c)) )
        return false;
    }
    /* All characters are numbers or letters. */
    return true;
}

/** @return true if s is a valid e-mail. */
function isEmail (s) {
	/*
	 * v.o.: The validation is not fully strict.
	 * but works OK if the client does not want to crash the program on purpose.
	 * The following cases are unhandled and will be evaluated as "true".
	 *	- someone@@identity@.com ( more than 1 @ sign )
	 *	- someone@my..com..net ( consequitive dots.)
	 * (the code may be patched upon request)
	 */
    /* is s whitespace? */
    if (isWhitespace(s)) return false;
    if (!hasValidChars(s)) return false;
    /*
     * there must be >= 1 characters before @, so we
     * start looking at character position 1 
     * (i.e. second character)
	 */
    var i = 1;
    var sLength = s.length;
    /* look for @ */
    while ((i < sLength) && (s.charAt(i) != "@")) { 
		i++;
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    /* look for . */
    while ((i < sLength) && (s.charAt(i) != ".")){ 
		i++;
    }
    /* there must be at least one character after the . */
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}