// JavaScript Document

// setup WhiteSpace variable
  reWhiteSpace = new RegExp(/^\s+$/);


String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};
var miNextPosition;
var msStringToParse;
var msDelimiter;
function parseInit(sInString, sDelimiter) {
	msDelimiter = sDelimiter;
	msStringToParse = sInString + sDelimiter;
	miNextPosition = 0;
}
function parseReturnNext() {
	var sReturnString = '';
	sStringToParse = msStringToParse;
	var x = sStringToParse.indexOf(msDelimiter);
	if (x>0) {
		iLen = x - miNextPosition;
		sReturnString = msStringToParse.substring(miNextPosition,iLen);
		x++;
		msStringToParse = msStringToParse.substring(x,msStringToParse.length);
	}
	return sReturnString;
	
}
function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
		return navigator.userAgent.substr(0,agt.indexOf('\/'));
	} else return 'Netscape';} else if (agt.indexOf(' ') != -1)
		return navigator.userAgent.substr(0,agt.indexOf(' '));
		else return navigator.userAgent;
}
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
function rtrim(argvalue) {

  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }

  return argvalue;
}
function floor(number) {
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}
function formatCurrencyNoComma(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	return (((sign)?'':'-') + num + '.' + cents);
}
function getMonth (object_value) {
	isplit = object_value.indexOf('/');
	splitchr = "/";
	if (isplit == -1) {
		isplit = object_value.indexOf('.');
		splitchr = ".";
	}
		
	if (isplit == -1) {
		isplit = object_value.indexOf('-');
		splitchr = "-";
	}

	var element1 = object_value.substring(0, isplit);
    if( element1.length == 4 ) {
        sYear = object_value.substring(0, isplit);
        isplit = object_value.indexOf(splitchr, isplit + 1);
        if (isplit == -1 || (isplit + 1 ) == object_value.length)
                return false;
        sMonth = object_value.substring((sYear.length + 1), isplit);
        sDay = object_value.substring(isplit + 1);
     }
    else {
        sMonth = object_value.substring(0, isplit);
        isplit = object_value.indexOf(splitchr, isplit + 1);
         if (isplit == -1 || (isplit + 1 ) == object_value.length)
                return false;
        sDay = object_value.substring((sMonth.length + 1), isplit);
        sYear = object_value.substring(isplit + 1);
     }
	 
	 return sMonth;

}
function getYear (object_value) {
	isplit = object_value.indexOf('/');
	splitchr = "/";
	if (isplit == -1) {
		isplit = object_value.indexOf('.');
		splitchr = ".";
	}
		
	if (isplit == -1) {
		isplit = object_value.indexOf('-');
		splitchr = "-";
	}

	var element1 = object_value.substring(0, isplit);
    if( element1.length == 4 ) {
        sYear = object_value.substring(0, isplit);
        isplit = object_value.indexOf(splitchr, isplit + 1);
        if (isplit == -1 || (isplit + 1 ) == object_value.length)
                return false;
        sMonth = object_value.substring((sYear.length + 1), isplit);
        sDay = object_value.substring(isplit + 1);
     }
    else {
        sMonth = object_value.substring(0, isplit);
        isplit = object_value.indexOf(splitchr, isplit + 1);
         if (isplit == -1 || (isplit + 1 ) == object_value.length)
                return false;
        sDay = object_value.substring((sMonth.length + 1), isplit);
        sYear = object_value.substring(isplit + 1);
     }
	 
	 return sYear;

}
function checkdate(object_value, required) {
     //Returns true if value is a date format or is NULL
     //otherwise returns false
    //trim whitespace before we validate

    object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');
    object_value = object_value = object_value.replace(/{d \'/, '').replace(/'}/, '');

    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }


     if (object_value.length == 0)
         return true;


     //Returns true if value is a date in the mm/dd/yyyy format
    isplit = object_value.indexOf('/');
	splitchr = "/";

	if (isplit == -1)
	{
		isplit = object_value.indexOf('.');
		splitchr = ".";
	}

	if (isplit == -1)
	{
		isplit = object_value.indexOf('-');
		splitchr = "-";
	}

	if (isplit == -1 || isplit == object_value.length)
		return false;

    var element1 = object_value.substring(0, isplit);
    // check for yyyy-mm-dd format
    if( element1.length == 4 )
    {
        sYear = object_value.substring(0, isplit);
        isplit = object_value.indexOf(splitchr, isplit + 1);
        if (isplit == -1 || (isplit + 1 ) == object_value.length)
                return false;
        sMonth = object_value.substring((sYear.length + 1), isplit);
        sDay = object_value.substring(isplit + 1);
     }
    else
    {
        sMonth = object_value.substring(0, isplit);
        isplit = object_value.indexOf(splitchr, isplit + 1);
         if (isplit == -1 || (isplit + 1 ) == object_value.length)
                return false;
        sDay = object_value.substring((sMonth.length + 1), isplit);
        sYear = object_value.substring(isplit + 1);
     }

         if (!checkinteger(sMonth)) //check month
                 return false;
         else
         if (!checkrange(sMonth, 1, 12)) //check month
                 return false;
         else
         if (!checkinteger(sYear)) //check year
                 return false;
         else
         if( sYear.length != 1 && sYear.length != 2 && sYear.length != 4 )
                return false;
         else
         if (!checkrange(sYear, 0, 9999)) //check year
                 return false;
         else
         if (!checkinteger(sDay)) //check day
                 return false;
         else
         if (!checkday(sYear, sMonth, sDay)) // check day
                 return false;
         else
                 return true;

 }
function checkinteger(object_value, required)
{
	//trim whitespace before we validate
    object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');
	//remove numeric noise
	object_value = object_value.replace(/[$£¥€,~+]?/g, '');


    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }


	var decimal_format = ".";
	var check_char = object_value.indexOf(decimal_format);

	if (check_char == -1)
		return checknumber(object_value);
	else
		return false;
}
function checknumber(object_value, required)
{
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	//trim whitespace before we validate
    object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');
	//remove numeric noise
	object_value = object_value.replace(/[$£¥€,~+]?/g, '');


    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }



	check_char = start_format.indexOf(object_value.charAt(0));

	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;

	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i));
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
		}
		else if (trailing_blank)
			return false;
		else
			digits = true;
	}	

	return true
}


function checkrange(object_value, min_value, max_value, required)
{
    object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');

    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }


	if (!checknumber(object_value))
		return false;
	else
		return (numberrange((eval(object_value)), min_value, max_value));

	return true;
}
function checkrange(object_value, min_value, max_value, required)
{
    object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');

    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }


	if (!checknumber(object_value))
		return false;
	else
		return (numberrange((eval(object_value)), min_value, max_value));

	return true;
}

function numberrange(object_value, min_value, max_value, required)
{
    //trim whitespace before we validate
    //object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');

    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }


	if (min_value != null)
	{
		if (object_value < min_value)
			return false;
	}

	if (max_value != null)
	{
		if (object_value > max_value)
			return false;
	}

	return true;
}
function checkday(checkYear, checkMonth, checkDay)
{
	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
		checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return checkrange(checkDay, 1, maxDay);
}
function isEmail(emailAddr){   
	// Return false if e-mail field does not contain a '@' and '.' .   
	if (emailAddr.indexOf ('@',0) == -1 || emailAddr.indexOf ('.',0) == -1){      
		return false;      
	}   
	else {      
		return true;      
	}   
}
function getRadioValue(radioObject) {

	var value = null
//	alert ("Radio Value Check");
	
	for (var i=0; i<radioObject.length; i++) {	
		if (radioObject[i].checked) {
			value = radioObject[i].value
			break
		}
	}
	return value;
}
function checkcreditcard(object_value, required)
{
	//trim whitespace before we validate
    object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');

    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }

	if (object_value.length == 0)
		return true;
	var white_space = " -";
	var creditcard_string="";
	var check_char;

	
	for (var i = 0; i < object_value.length; i++)
	{
		check_char = white_space.indexOf(object_value.charAt(i));
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}	

	if (creditcard_string.length < 13 || creditcard_string.length > 19)
		return false;

	if (creditcard_string.charAt(0) == "+")
		return false;

	if (!checkinteger(creditcard_string))
		return false;
	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i));

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
				checkdigit++;

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	
	if ((checkdigit % 10)==0) {
		return true;
	} else {
		return false;
	}

//	return (checkdigit % 10) == 0 ? true : false;
}
