//********************************************************************
//*-------------------------------------------------------------------
//* 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;
}

	function trim(s) {
		var l=0; var r=s.length -1;
		while(l < s.length && s.charAt(l) == ' ') { l++; }
		while(r > l && s.charAt(r) == ' ') { r-=1;	}
		var sTrimmed = s.substring(l, r+1);
		return sTrimmed;
	}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "ext()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }
 
function populateDate(inForm) {
	var temp=0;
	
	var today = new Date();
	var day = today.getDate();		// value 1 - 28/29/30/31
	var month = today.getMonth();	// value from 0 (January) to 11
	var year = today.getFullYear();		// 4-digit year value
	
	// year is correct for IE (i.e. its value is 2006 if the current year were 2006).
	// For Mozilla, however, the value of year if it were 2006 would be 106. It needs
	// 1900 to be added to it.
	//if (year < 1000)
	//	year += 1900;
	
	t2= 7;

	var t3=0;
	if (month == 1) {
		if (year/4 == parseInt(year/4))
			t3 = 29;
		else t3=28;
	} else if(month==8 || month==3 || month==5 || month==10)
		t3=30;
	else
		t3=31;
		
   //if day is the last fortnight of a month, addding 14 days to RFQ response doesn't select the
   //date properly in the drop down causing 'index out of bound' errors. The following logic
   //handles the issue - Kannan Ayyah 08/31/2007
   
   var RFQresponseDays = 14;
   var rfqRespDate = 1;
      
   if( day <= 14)
   		rfqRespDate = day + RFQresponseDays;
   else
   {
   		rfqRespDate = (RFQresponseDays - (t3 - day) );
   		month = month+1; //rollover to the next month
   		if( month == 12) month = 0;
   }
   
	for (var i=0; i <t3 ; i++) {
		var x= String(i+1);	
		inForm.day.options[i] = new Option(x,x);
	}
	
	if ( rfqRespDate < 0 ) rfqRespDate = 0;
	
	inForm.day.options[rfqRespDate].selected=true;

	for (var i=0,j=year; i <t2 ; i++, j++){
		var y= String(j);
		inForm.year.options[i] = new Option(y,y);		
	}
	
	for(var i=0;i<12;i++) {
		if(i==month) {
			inForm.month.options[i].selected=true;
			break;
		}
	}
}


function populateDate2(inForm) {
	var t3=0;
	if(inForm.month.options[1].selected) {
		var year = parseInt(inForm.year.options[inForm.year.selectedIndex].value);
		if (year/4 == parseInt(year/4))
			t3 = 29;
		else t3=28;
	} else if(inForm.month.options[8].selected||inForm.month.options[3].selected||inForm.month.options[5].selected||inForm.month.options[10].selected)
		t3=30;
	else
		t3=31;

	for(i=0;i<31;i++) {
		inForm.day.options[i]=null;
	}

	for (var i=0; i <t3 ; i++) {
		var x= String(i+1);
		inForm.day.options[i] = new Option(x);
	}
}

/*************************************************************************\
boolean isDateFuture([int year, int month, int day])
return true if the date is past today's date, else return false.
\*************************************************************************/
function isDateFuture(yy, mm, dd) {
	selectedYear = parseInt(yy);
	selectedMonth = parseInt(mm);
	selectedDay = parseInt(dd);

	if (!isInteger(selectedYear))
		return false;
	if (!isInteger(selectedMonth))
		return false;
	if (!isInteger(selectedDay))
		return false;

	today = new Date();
	expiry = new Date(yy, mm, dd);
	if (expiry.getTime() > today.getTime())
		return true;
	else
		return false;
}


//////////////////////////////////////////////////////////
// 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;
}

/**
//////////////////////////////////////////////////////////
// This function submit the distributor search form passed in
//////////////////////////////////////////////////////////
  function distributorSearchFormSubmit(form, searchType)
  {
 	var zip = form.distSearchZip.value;
 	var ctry = form.distSearchCountry.value;
    var urlPrm = form.URLparam.value;
    
	if (searchType == 'zip') {
    	if (!validateZIP(zip)) {
	    	alert('A valid U.S. zip code is required if you are search by zip');
	    } else {
	    	//form.searchType.value='zip';
	    	//form.action = "DistributorSearch?zip=60123&searchType=zip&langId=-1&URL=OmronFindDistributorView&storeId=10051&catalogId=10051";
	    	form.action = urlPrm+"&distSearchZip="+zip+"&searchType=zip";
		    form.submit();
	    }
    } else  {
    	if (ctry == 'United States' && !validateZIP(zip)) {
	    	alert('A valid U.S. zip code is required if you are selecting USA as a country');
	    } else {
	   	 	//form.searchType.value='country';
   	 		form.action = urlPrm+"&distSearchCountry="+ctry+"&searchType=country";
		    form.submit();
	    }
    }  
  }
**/
 
//////////////////////////////////////////////////////////
// This function validates zip code
////////////////////////////////////////////////////////// 
// Modified to validate Canadian postal codes.
function validateZIP(field) {
	var validCA = "0123456789ABCEGHJKLMNPRSTVWXYZ";
	var validUS = "0123456789";
	
	// A postal code takes one of two forms: US, or Canada.
	// Is valid, US?
	if (field.length == 5) {
		for (var i=0; i<field.length; i++) {
			temp = "" + field.substring(i, i+1);
			// If we encounter any bad character, this is a bad zipcode.
			if (validUS.indexOf(temp) == "-1")
		return false;
	}
		// If we've gotten this far, then it's a good zipcode.
		return true;
	}
	//
	// Is valid, Canada?
	if (field.length == 6 || field.length == 7) {
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
			// If we encounter any bad character, this is a bad zipcode.
			// Also: if we encounter a space *and* we're not at position 3, this is bad.
			if (validCA.indexOf(temp) != -1 ||
				(temp == " " && i == 3 && field.length == 7))
				// do nothing -- is a good character!
				;
			else
			return false;
		}
		// If we've gotten this far, then it's a good zipcode.
		return true;
	}
	
			return false;
		}

/**
function hitEnterDistribSrch(myField,event,form,searchType){ event = event || window.event;    
 var code = event.keyCode || event.which;        if(code=='13'){
  var zip = form.distSearchZip.value;
   var ctry = form.distSearchCountry.value;
     var urlPrm = form.URLparam.value;
    
  if (searchType == 'zip') {
      if (!validateZIP(zip)) {
       alert('A valid U.S. zip code is required if you are search by zip');
       return !(code == 13 ); 
      } else {
       //form.searchType.value='zip';
       //form.action = "DistributorSearch?zip=60123&searchType=zip&langId=-1&URL=OmronFindDistributorView&storeId=10051&catalogId=10051";
       form.action = urlPrm+"&distSearchZip="+zip+"&searchType=zip";
       form.submit();
      }
     } else  {
      if (ctry == 'United States' && !validateZIP(zip)) {
       alert('A valid U.S. zip code is required if you are selecting USA as a country');
      } else {
        //form.searchType.value='country';
        form.action = urlPrm+"&distSearchCountry="+ctry+"&searchType=country";
       form.submit();
      }
     }
 }
}
**/
function gsaSearchHitEnter(event,form){ event = event || window.event;    
 var code = event.keyCode || event.which; 
 if(code=='13'){
  if (form.searchInput.value.length==0 || form.searchInput.value=="Search Again"){
   alert("The Search box is empty. Enter a search value.");
   form.searchInput.focus();
   return !(code == 13 );
  }else{
   form.submit();
  }
 }
}

function gsaSearch(myForm, myObj){

  myFormObj = document.getElementById(myForm);
  myObjectElement = document.getElementById(myObj);

 if (myObjectElement.value.length==0 || myObjectElement.value=="Search Again"){
  alert("The Search box is empty. Enter a search value.");
  myObjectElement.focus();
 }else{
  myFormObj.submit();
 }
}

/*
function gsaSearch(form){
 if (form.searchInput.value.length==0 || form.searchInput.value=="Search Again"){
  alert("The Search box is empty. Enter a search value.");
  form.searchInput.focus();
 }else{
  form.submit();
 }
}*/
	
	function hitEnterDistribSrch(myField,event,form,searchType){ event = event || window.event;    
	 var code = event.keyCode || event.which;        if(code=='13'){
	  var zip = form.distSearchZip.value;
	   var ctry = form.distSearchCountry.value;
		 var urlPrm = form.URLparam.value;
		
	  if (searchType == 'zip') {
			
		  if (zip.length == 0 || zip == 'Enter Zip/Postal Code') {
		   alert('Please provide a valid zip/postal code.');
		   return !(code == 13 ); 
		  } else {
		   //form.searchType.value='zip';
		   //form.action = "DistributorSearch?zip=60123&searchType=zip&langId=-1&URL=OmronFindDistributorView&storeId=10051&catalogId=10051";
		   form.action = urlPrm+"&distSearchZip="+zip+"&searchType=zip";
		   form.submit();
		  }
		 } else  {
			form.action = urlPrm+"&distSearchCountry="+ctry+"&searchType=country";
			form.submit();
		  
		 }
	 }
	}

		//////////////////////////////////////////////////////////
		// This function submit the distributor search form passed in
		//////////////////////////////////////////////////////////
		  function distributorSearchFormSubmit(form, searchType)
		  {
			var zip = form.distSearchZip.value;
			var ctry = form.distSearchCountry.value;
			var urlPrm = form.URLparam.value;
		 
			if (searchType == 'zip') {
				if ( zip.length == 0 || zip == 'Enter Zip/Postal Code' ) {
					alert('Please provide a valid zip/postal code.');
					
			  } else {
					//form.searchType.value='zip';
					//form.action = "DistributorSearch?zip=60123&searchType=zip&langId=-1&URL=OmronFindDistributorView&storeId=10051&catalogId=10051";
					form.action = urlPrm+"&distSearchZip="+zip+"&searchType=zip";
					form.submit();
				}
			} else  {
					
					form.action = urlPrm+"&distSearchCountry="+ctry+"&searchType=country";
					form.submit();
				
			}  
		  }


	
	
