﻿// JScript File

// globals
var field_length=0;
var old_value = 0;
var numericCheck = "0123456789-.,";

function checkValidation ( addressField ) {
    if ( stringEmpty ( addressField.value ) )
        alert ( "Error! There is no E-Mail address entered" );
    else if ( noAtSign ( addressField.value ) )
        alert ( "Error! The E-Mail address does not contain an '@' character" );
    else if ( nothingBeforeAt ( addressField.value ) )
        alert ( "Error! An E-Mail address must contain at least one character before the '@' character" );
    else if ( noLeftBracket ( addressField.value ) )
        alert ( "Error! The E-Mail address contains a right square bracket ']',\nbut no corresponding left square bracket '['" );
    else if ( noRightBracket ( addressField.value ) )
        alert ( "Error! The E-Mail address contains a left square bracket '[',\nbut no corresponding right square bracket ']'" );
    else if ( noValidPeriod ( addressField.value ) )
        alert ( "Error! An E-Mail address must contain a period ('.') character" );
    else if ( noValidSuffix ( addressField.value ) )
        alert ( "Error! An E-Mail address must contain a two or three character suffix" );
    else
        return (true);

    return ( false );
}

function linkCheckValidation ( formField ) {
    if ( checkValidation ( formField ) == true ) {
        alert ( 'E-Mail Address Validates OK' );
    }

    return ( false );
}

function stringEmpty ( address ) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( address.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign ( address ) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if ( address.indexOf ( '@', 0 ) == -1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt ( address ) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( address.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket ( address ) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( address.indexOf ( '[', 0 ) == -1 && address.charAt ( address.length - 1 ) == ']' ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket ( address ) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if ( address.indexOf ( '[', 0 ) > -1 && address.charAt ( address.length - 1 ) != ']' ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod ( address ) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if ( address.indexOf ( '.', 0 ) == -1 )
        return ( true );

    return ( false );
}

function noValidSuffix ( address ) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )
        return ( false );

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = address.length;
    var pos = address.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 3 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function validateUserEmail ()
{
	var emailField = document.getElementById ("user_email");
	if (emailField) {
	    if (emailField.value.length > 0) 
	       return checkValidation (emailField);
	    
//			var matchResult = emailField.value.match('([a-z]|[0-9])+@([a-z]|[0-9])+\.com');
//			if (matchResult == null)
//			{
//				alert ("User's email address is formatted incorrectly.")
//				return false;
//			}
		
	}
	return true;
}

function validateEmail(emailField, errorField)
{
	var emailField = document.getElementById ("Ret_Email");
	if (emailField) {
	    if (emailField.value.length == 0) {
			alert ("Return email address must contain a valid email address");
			return false;
		}
		if (! checkValidation (emailField))
		    return false;
//		var matchResult = emailField.value.match('([a-z]|[0-9])+@([a-z]|[0-9])+\.com');
//		if (matchResult == null)
//		{
//			alert ("Return email address is not in valid format")
//			return false;
//		}
	}
	var messageField = document.getElementById ("msg");
	if (messageField) {
	    if (messageField.value.length == 0) {
			alert ("Email must contain a message");
			return false;
		}
	}
	alert ("Your message has been sent");
	return true;
}

// store original value of the field selected. 
// if the value is changed to something invalid, we
// can set value back to its original value, instead if zero. 
function set_old_val (id)
{
	old_value = id.value;
}
// numeric_percent and numeric_noneg were created
// for modifiable grid_view values. we don't know the 
// exact field id holding the variables. We have to pass
// them as a string


// percentages should be positive, and limited to 
// a range: 0 to 100 

function numeric_percent (str, currency) 
{
  var id = document.getElementById (str);
  if (numeric (id, currency)) {
	 if (Number (id.value) < 0 || Number (id.value) > 100) {
		  alert ("Percentage: >= to 0 or <= 100");
		  id.value = "0"; 
		  id.focus ();
		  id.select ();
	 }
  }
}

function numeric_noneg (str, currency)
{
  var id = document.getElementById (str);
  if (numeric (id, currency)) {
     if (Number (id.value) < 0) {
		alert ("Value can not be less than 0.");
		id.value = "0"; 
		id.focus ();
		id.select ();
     }
  }
}

// test currency and numberic fields
function numeric (fld,currency)
{
	var er = 0;
	for (var i = 0; i < fld.value.length; i++) {
		var fval = fld.value.substr (i,1);	
		if (fval == '.' || fval == '-' || fval == ',') {
			if (currency == 1)
				continue;
			else 
			   er = 1;
		}
		if (er == 0) {
			if (numericCheck.indexOf(fval) == -1){
				er = 1;
			}
		}
		if (er == 1) {
		  var msg = "";
		  if (currency == 1)
		      msg = "Invalid: Currency required";
		  else
			  msg = "Invalid: Numeric required";
		  alert (msg);
		  fld.value = old_value; 
		  fld.focus ();
		  fld.select ();
		  return false;
		}
	}
	return true;
}

