/* 
 * by Mahe 
 * requires prototype.js for ajax
 *
 */

// Removes leading whitespaces
function LTrim( value ) {	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}

// Removes ending whitespaces
function RTrim( value ) {	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

// Removes leading and ending whitespaces
function trim( value ) {	
	return LTrim(RTrim(value));	
}

function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}


// Validates text fields. Returns true if a field is empty.
function is_fieldempty(fields) {
	var v = false;
	hotfield_off(fields);
	
	for (i=0;i<fields.length;i++) {
		if (trim($(fields[i]).getValue()) == '') {
			hotfield(fields[i]);
			v = true;
		}
	} 
	return v;
}

// Validates numeric fields. Returns true if a field is not numeric.
function is_notnumeric(fields) {
	var v = false;
	hotfield_off(fields);
	
	for (t=0;t<fields.length;t++) {
		num_field = fields[t];
		if (!is_numeric(removeSpaces($(num_field).getValue()))) {						
			hotfield(num_field);
			v = true;
		}
	} 
	return v;
}

// Validates numeric fields. Returns true if a field is not numeric.
function is_notdigit(fields) {
	var v = false;
	hotfield_off(fields);

	for (t=0;t<fields.length;t++) {
		num_field = fields[t];
		if (!is_digit(removeSpaces($(num_field).getValue()))) {
			hotfield(num_field);
			v = true;
		}
	}
	return v;
}


// Validates email fields. Returns true if a field is not valid.
function is_invalidemail(fields) {
	var v = false;
	hotfield_off(fields);
	
	for (r=0;r<fields.length;r++) {		
		if (!checkemail($(fields[r]).getValue())) {			
			hotfield(fields[r]);
			v = true;
		}
	} 
	return v;
}

// Colors and highlights the invalid form fields
function hotfield(f) {
	//$(f).style.background = 'd2ed2ae';
	$(f).setStyle({ background:'#cde4f5' });
}

// Turns off color in the form fields
function hotfield_off(fields) {
	for (i=0;i<fields.length;i++) {
		//$(fields[i]).style.background = '#ffffff';
		$(fields[i]).setStyle({ background:'#ffffff' });
	}
}

// Displays alert when form validation fails
function validation_alert(msgid,msg) {
	$(msgid).update(msg);
}


// Validates username
function is_notvalidusername(fields) {
	var v = false;
	hotfield_off(fields);
	
	for (t=0;t<fields.length;t++) {
		num_field = fields[t];
		if (!is_validusername($(num_field).getValue())) {						
			hotfield(num_field);
			v = true;
		}
	} 
	return v;
}

// Validates form field for username value 
function is_validusername(strString) {
   var strValidChars = "1234567890_.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }       
      }
   return blnResult;
}

// Validates form field for numeric value 
function is_numeric(strString) {
   var strValidChars = "+0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

// Validates form field for digits
function is_digit(strString) {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

// Validates email address
function validate_email(email_address) {
    //Assumes that valid email addresses consist of user_name@domain.tld
    at = email_address.indexOf('@');
    dot = email_address.indexOf('.');
         
    if(at == -1 || 
      dot == -1 || 
      dot <= at + 1 ||
      dot == 0 || 
      dot == email_address.length - 1)
      return(false);
            
    user_name = email_address.substr(0, at);
    domain_name = email_address.substr(at + 1, email_address.length);                  
         
    if(validate_string(user_name) === false || 
      validate_string(domain_name) === false)
      return(false);                     
         
	return(true);
}

function checkemail(str){
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str)) {
  return true;
} else {
  return false;
}
}
// Helper function for validate_email


function validate_string(string, return_invalid_chars)
         {
         valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         invalid_chars = '';
         
         if(string == null || string == '')
            return(true);
         
         //For every character on the string.   
         for(index = 0; index < string.length; index++)
            {
            char = string.substr(index, 1);                        
            
            //Is it a valid character?
            if(valid_chars.indexOf(char) == -1)
              {
              //If not, is it already on the list of invalid characters?
              if(invalid_chars.indexOf(char) == -1)
                {
                //If it's not, add it.
                if(invalid_chars == '')
                   invalid_chars += char;
                else
                   invalid_chars += ', ' + char;
                }
              }
            }                     
            
         //If the string does not contain invalid characters, the function will return true.
         //If it does, it will either return false or a list of the invalid characters used
         //in the string, depending on the value of the second parameter.
         if(return_invalid_chars == true && invalid_chars != '')
           {
           last_comma = invalid_chars.lastIndexOf(',');
           
           if(last_comma != -1)
              invalid_chars = invalid_chars.substr(0, $last_comma) + 
              ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
                      
           return(invalid_chars);
           }
         else
           return(invalid_chars == ''); 
         }

function resizeImage(image, maxwidth, maxheight)
{
  if (image.className == "Thumbnail")
    {
        w = image.width;
        h = image.height;

        if( w == 0 || h == 0 )
        {
            image.width = maxwidth;
            image.height = maxheight;
        }
        else if (w > h)
        {
            if (w > maxwidth) image.width = maxwidth;
        }
        else
        {
            if (h > maxheight) image.height = maxheight;
        }

        image.className = "ScaledThumbnail";
    }
}

