//********************************************************************
//*-------------------------------------------------------------------
//* Licensed Materials - Property of IBM
//*
//* WebSphere Commerce
//*
//* (c) Copyright International Business Machines Corporation. 2003
//*     All rights reserved.
//*
//* US Government Users Restricted Rights - Use, duplication or
//* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//*
//*-------------------------------------------------------------------
//*

//////////////////////////////////////////////////////////
// Checks whether a string contains a double byte character
// target = the string to be checked
//
// Return true if target contains a double byte char; false otherwise
//////////////////////////////////////////////////////////
function containsDoubleByte (target) {
     var str = new String(target);
     var oneByteMax = 0x007F;

     for (var i=0; i < str.length; i++){
        chr = str.charCodeAt(i);
        if (chr > oneByteMax) {return true;}
     }
     return false;
}

//////////////////////////////////////////////////////////
// A simple function to validate an email address
// It does not allow double byte characters
// strEmail = the email address string to be validated
//
// Return true if the email address is valid; false otherwise
//////////////////////////////////////////////////////////
function isValidEmail(strEmail){
	// check if email contains dbcs chars
	if (containsDoubleByte(strEmail)){
		return false;
	}
	
	if(strEmail.length == 0) {
		return true;
	} else if (strEmail.length < 5) {
             return false;
       	}else{
           	if (strEmail.indexOf(" ") > 0){
                      	return false;
               	}else{
                  	if (strEmail.indexOf("@") < 1) {
                            	return false;
                     	}else{
                           	if (strEmail.lastIndexOf(".") < (strEmail.indexOf("@") + 2)){
                                     	return false;
                                }else{
                                        if (strEmail.lastIndexOf(".") >= strEmail.length-2){
                                        	return false;
                                        }
                              	}
                       	}
              	}
       	}
      	return true;
}



//////////////////////////////////////////////////////////
// This function will count the number of bytes
// represented in a UTF-8 string
//
// arg1 = the UTF-16 string
// arg2 = the maximum number of bytes allowed in your input field
// Return false is this input string is larger then arg2
// Otherwise return true...
//////////////////////////////////////////////////////////
function isValidUTF8length(UTF16String, maxlength) {
    if (utf8StringByteLength(UTF16String) > maxlength) return false;
    else return true;
}

//////////////////////////////////////////////////////////
// This function will count the number of bytes
// represented in a UTF-8 string
//
// arg1 = the UTF-16 string you want a byte count of...
// Return the integer number of bytes represented in a UTF-8 string
//////////////////////////////////////////////////////////
function utf8StringByteLength(UTF16String) {
  if (UTF16String === null) return 0;
  var str = String(UTF16String);
  var oneByteMax = 0x007F;
  var twoByteMax = 0x07FF;
  var byteSize = str.length;

  for (i = 0; i < str.length; i++) {
    chr = str.charCodeAt(i);
    if (chr > oneByteMax) byteSize = byteSize + 1;
    if (chr > twoByteMax) byteSize = byteSize + 1;
  }  
  return byteSize;
}


//////////////////////////////////////////////////////////
//The function will return the generated address nickname.
//example of calling this method - generateAddressNickName(form.firstName.value, form.lastName.value, form.address1.value, form.city.value, form.state.value);
//The Nickname, instead of being a user specified value, will be a generated value based on the following algorithm:
//1st 5 characters of the first name +
//1st 5 characters of the last name +
//1st 10 characters of address1 +
//1st 5 characters of city +
//2-char State
//With a single blank space in between.
//
//e.g.
//FirstName = Remy 
//LastName = Nisbet
//Address1 = 67 Ridgeway Drive
//City = Irvington
//State = NY
//Nickname = Remy  Nisbe 67 Ridgewa Irvin NY
//////////////////////////////////////////////////////////
function generateAddressNickName(firstName, lastName, address1, city, state) {
	var nickName;
	
	nickName = firstName.substring(0,5) + " " + lastName.substring(0,5) + " " + address1.substring(0,10) + " " + city.substring(0,5) + " " + state.substring(0,2);
		
	return trim(nickName);

}

////////////////////////////////////////////////////
//BEGIN Date functionality to display month, day, year fields that correct for number of days in a month
//Assumptions: the name of the fields will be: "birthDay", "birthMonth", "birthYear"
////////////////////////////////////////////////////
Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();
if (NowYear < 2000) NowYear += 1900; //for Netscape


NowYearAccount =NowYear-15; //subtract 14 years for legal reasons

if (NowYearAccount < 1992) NowYearAccount += 1900; //for Netscape


//function for returning how many days there are in a month including leap years
function DaysInMonth(month, year)
{
  var DaysInMonth = 31;
  if (month == 4 || month == 6 || month == 9 || month == 11) DaysInMonth = 30;
  if (month == 2 && (year/4) != Math.floor(year/4))	DaysInMonth = 28;
  if (month == 2 && (year/4) == Math.floor(year/4))	DaysInMonth = 29;
  return DaysInMonth;
}

//function to change the available days in a months
function ChangeOptionDays(form)
{
  DaysObject = form.birthDay;
  MonthObject = form.birthMonth;
  YearObject = form.birthYear;

  Month = MonthObject[MonthObject.selectedIndex].value;
  Year = YearObject[YearObject.selectedIndex].value;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length - 1;

  if (CurrentDaysInSelection > DaysForThisSelection)
  {
  	currentDateDiff = CurrentDaysInSelection - DaysForThisSelection;
    for (i=0; i<(currentDateDiff); i++)
    {
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
  	daysDateDiff = DaysForThisSelection - CurrentDaysInSelection;
    for (i=0; i<(daysDateDiff); i++)
    {
      //length includes the default option (-1, Day)
      nextDay = DaysObject.options.length;
      NewOption = new Option(nextDay, nextDay);

	  try {
		DaysObject.add(NewOption,null);
	  } catch(ex) {
	    DaysObject.add(NewOption);
	  }
    }
  }
  if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

//function to set options to today
function SetToToday(form)
{
  DaysObject = form.birthDay;
  MonthObject = form.birthMonth;
  YearObject = form.birthYear;

  YearObject[0].selected = true;
  MonthObject[NowMonth].selected = true;

  ChangeOptionDays(form);

  DaysObject[NowDay-1].selected = true;
}

//function to write option years minus x
function WriteYearOptions(YearsBehind)
{
  line = "";
  for (i=0; i<YearsBehind; i++)
  {
    line += "<option>";
    line += NowYear - i;
  }
  return line;
}

//function to write option years minus x
function WriteAccountYearOptions(YearsBehind)
{
  line = "";
  for (i=0; i<YearsBehind; i++)
  {
    line += "<option>";
    line += NowYearAccount - i;
  }
  return line;
}

function WriteYearOptionsWithSelected(YearsBehind, YearSelected)
{
  line = "";
  for (i=0; i<YearsBehind; i++)
  {
  	var tYear = NowYear - i;
  	if(tYear == YearSelected){ line += "<option selected>"; }
  	else{ line += "<option>"; }
    line += NowYear - i;
  }
  return line;
}

function WriteYearAccountOptionsWithSelected(YearsBehind, YearSelected)
{
  line = "";
  for (i=0; i<YearsBehind; i++)
  {
  	var tYear = NowYearAccount - i;
  	if(tYear == YearSelected){ line += "<option selected>"; }
  	else{ line += "<option>"; }
    line += NowYearAccount - i;
  }
  return line;
}
////////////////////////////////////////////////////
//END Date functionality to display month, day, year fields that correct for number of days in a month
////////////////////////////////////////////////////
