//********************************************************************
//*-------------------------------------------------------------------
//* 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;
}

//////////////////////////////////////////////////////////
// This function will check if the check box is checked
// if checked then will return true else false
//
//////////////////////////////////////////////////////////
function validateCheckBox(){
	var form = document.getElementById("card_details");
	if(form.TERMSANDCONDITIONS_OPTIN.checked){
		//For Removing spaces between credit card numbers (cardNumberStar for TextField & cardNumber for Hidden/QuickCheckout)
		form.cardNumberStar.value = form.cardNumberStar.value.replace(/\s/g, '');
  		form.cardNumber.value = form.cardNumber.value.replace(/\s/g, '');
  		//End Removing spaces
		//form.BTN_ORDER_NOW.disabled = true;
		return true;
	}
}

function enableOrderNow() {
	var form = document.getElementById("card_details");
	form.BTN_ORDER_NOW.disabled = false;
}

// Method to write in Cookie just pass the key
	function putCookie(cookieKey, cookieValue)
	{
			if(cookieValue && ( 0 != cookieValue.length))
			{
	    		var today = new Date();
	    		var oneMonths = new Date(today.getYear(), today.getMonth()+1, today.getDate());
	    		setCookie(cookieKey, cookieValue, oneMonths);
				
			}
	}
//Method to write in 	                            
	function setCookie(szName, szValue, szExpires, szPath, szDomain, bSecure)
	{
	 	var szCookieText = 	   escape(szName) + '=' + escape(szValue);
		szCookieText +=	 	   (szExpires ? '; EXPIRES=' + szExpires.toGMTString() : '');
		document.cookie = szCookieText;
	}
	
	//Method to change promo code in UpperCase
	// in Order Summary page (Checkout page)
	
	//function makeUppercase() {
	//	document.PromotionCodeForm.promoCode.value = document.PromotionCodeForm.promoCode.value.toUpperCase();
	//}	
	
	//Method will insert a space in Post code in return/ResturnsSearch.jsp
	//Shoud insert space only if there is space enterd.
	//if length is ==6 then insert space after 3rd char.
	//Post code lenght should be more then 5.
	//if length is > 6 then insert space after 4th char.
	function insertSpaceInPostCode(){
		var pstCode = document.ReturnsForm.postCode.value;
		if(0 != pstCode.lenght &&  pstCode.indexOf(" ") > 0 ){			
		}else if(0 != pstCode.lenght && 6 == pstCode.length){
			var tmpPostCd = pstCode.substr(0,3)+" " + pstCode.substr(3,pstCode.length-1);
			document.ReturnsForm.postCode.value =tmpPostCd;
		}else if(0 != pstCode.lenght && 6 <= pstCode.length){
			var tmpPostCd = pstCode.substr(0,4)+" " + pstCode.substr(4,pstCode.length-1);
			document.ReturnsForm.postCode.value =tmpPostCd;
		}
	}
	
	//Function to set cookie with cookieAge in seconds
	function putCookieWithSeconds(cookieKey, cookieValue, cookieAge)
	{
			
			if(cookieValue && ( 0 != cookieValue.length))
			{
				var futdate = new Date();		//Get the current time and date
			    var expdate = futdate.getTime();  //Get the milliseconds since Jan 1, 1970
			    
			    expdate += cookieAge * 1000  //expires in cookieage seconds (cookieage * 1000 milliseconds)
			    futdate.setTime(expdate)
	    		setCookie(cookieKey, cookieValue, futdate);
			}
	}
		
	/**
	*  author : syed sadhik
	*/
		function replaceStringInUrl(url, keyInURL, newURLKeyValue) {
		
		if(url.indexOf(keyInURL) > -1){
				var startIndex = url.indexOf(keyInURL);
			
				// find immideate '&' in url
				var stopIndex = url.indexOf("&",startIndex);
				// if key is the last param in url then find the last char 
				if( !stopIndex > -1 ){
					//this key is the last key in url
						stopIndex = url.length;
				}
				
				if(	null != keyInURL && null != newURLKeyValue)	{
					var temp = keyInURL+"="+newURLKeyValue;
					url =url.replace(url.substring(startIndex,stopIndex), temp);
				}	
				
		}
		return url;
	}
	
	//Start Menu Code
	var vMenu = "";
	function changeDisplay( elementId, setTo ) {

		if( document.getElementById ) {
			//DOM
			var theElement = document.getElementById( elementId );
		} else {
			if( document.all ) {
				//Proprietary DOM
				var theElement = document.all[ elementId ];
			} else {
				//Create an object to prevent errors further on
				var theElement = new Object();
			}
		}
		if( !theElement ) {
			/* The page has not loaded or the browser claims to support
			document.getElementById or document.all but cannot actually
			use either */
			return;
		}
		//Reference the style ...
		if( theElement.style ) { theElement = theElement.style; }
		if( typeof( theElement.display ) == 'undefined' &&
		!( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) ) {
		  //The browser does not allow us to change the display style
		  //Alert something sensible (not what I have here ...)
		  window.alert( 'Your browser does not support this' );
		  return;
		}
		//Change the display style
		theElement.display = setTo;
	}


	function changeDrop(fID){

	  sMenu = fID
		
	  if (vMenu == sMenu){
		MenuHide(sMenu) //Hide existing menu
	  }else{
		if (vMenu != "") //Hide previous Menu
		{
		  MenuHide(vMenu);
		}
		MenuShow(sMenu); //Show menu
	  }
	}

	function MenuHide(sMenu) {
	  changeDisplay(sMenu, "none")
	  vMenu = "";
	}

	function MenuShow(sMenu) {
	  changeDisplay(sMenu, "block")
	  vMenu = sMenu
	}
	
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

