//********************************************************************
//*-------------------------------------------------------------------
//* 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 randomize an array of file names and 
// returns an array or 9 file of which the first
// one is always big and rest 8 are all small sizes.
// It assumes the image files names either ends
// with _LS or _LL in upload/image directory of the 
// store.
/////////////////////////////////////////////////////////


function randomizeArray(arrayObj, largeImageIndex, maxImageNum, documentObjectId)
{
	//alert("randomize "+arrayObj.length);
	
	//Following line if uncommented will randomize the imageCollection. It's commented as requested by Client to stop randomizing
	arrayObj.sort(randOrd);
	
	if(arrayObj==null)
	{
		return null ;
	}
	
	newArrayObj = new Array(maxImageNum);	
	
	//check if largeImageIndex supplied is more then length, if yes make it 0
	if(largeImageIndex > arrayObj.length)
		largeImageIndex = 0;
	
	
	// pick maxImageNum elements of the array and return a new arrayObject.	
	iterateLength = maxImageNum;
	
	if(maxImageNum>arrayObj.length)
		iterateLength=arrayObj.length;
		
		
	for(i=0;i<iterateLength;i++)
	{
		//take out only itereateLength elements and create a new array.
		newArrayObj[i] = arrayObj[i];
	}
	
	
	// check if numberof images supplied are less then largeImageIndex
	if(largeImageIndex > arrayObj.length)
	{
		startIndex=arrayObj.length;
		endIndex=largeImageIndex;
		urlLink="";
		emptyFileName=fileName.substring(0,fileName.lastIndexOf('/'));
		
		finalEmptyFileName = emptyFileName + "no-image_LS.gif";
		
		for(;startIndex<endIndex;startIndex++)
		{
			newArrayObj[startIndex]=urlLink+'#'+finalEmptyFileName+'#'+'No image Available';
		}
	}
	
	//
	//randomize array before returning
	//Following line if uncommented will randomize the imageCollection. It's commented as requested by Client to stop randomizing
	newArrayObj.sort(randOrd);
	
	return newArrayObj;
}



function displayCategoryImages(arrayObj, largeImageIndex, maxImageNum, documentObjectId)
{
	//maxImageNum is the number of images to be shown.
	//alert(arrayObj);	
	randomizedArray = randomizeArray(arrayObj, largeImageIndex, maxImageNum, documentObjectId);	
	
	//check if largeImage is empty existing
	image_large = randomizedArray[largeImageIndex];
	
	if (typeof(image_large) == 'undefined' ) 
		return;
		
	//check largeIndex image for no-image
	if(image_large.indexOf('no-image_LS.gif') > -1)
	{
		//check if any image has a large version
		for(cntr=0; cntr<randomizedArray.length; cntr++)
		{						
			if(cntr!=largeImageIndex)
			{					
				other_image = randomizedArray[cntr];
				if(other_image.indexOf('no-image_LS.gif')==-1)
				{
					//image found swap places and break loop
					randomizedArray[largeImageIndex]=randomizedArray[cntr];
					randomizedArray[cntr]=image_large;
					break;						
				}
			}
		}
	}
	
	//check again if largeImage is still no-image
	// this condition will only be true if all images are no-image.gif
	image_large = randomizedArray[largeImageIndex];
	if(image_large.indexOf('no-image_LS.gif') > -1)
	{
		regEx=/_LS/
		image_large=image_large.replace(regEx, '_LL');		
		randomizedArray[largeImageIndex]=image_large;
	}
	
	bigImgDivStrtText = '<div class="row-1-col-b1-b">';
	smlImgDivStrtText = '<div class="row-1-col-b1-a">';
	
	urlLinkStrtText = '<a href="';
	urlLinkEndText = '">';
	
	imgStrtText='<img onError="changeImage(this)" src="';
	bigImgClsngText = '" alt="altText" width="234" height="330" border="0" /></a></div>';
	smlImgClsngText = '" alt="altText" width="117" height="165" border="0" /></a></div>';	
	
	fullRowText ="";
	
	for(i=0;i<maxImageNum;i++)
	{
		arrElement = randomizedArray[i];		
		if (typeof(arrElement) == 'undefined' ) 
			return;
		
		link_name_altText = arrElement.split('#');		
		
		urlLink="";
		fileName="";
		altText="No image available";
		
		try
		{
			if(link_name_altText.length==2)
			{
				urlLink = link_name_altText[0];
				fileName = link_name_altText[1];
			}
			else
			{
				urlLink = link_name_altText[0];
				fileName = link_name_altText[1];
				altText = link_name_altText[2];
				
				if(altText.length==0)
					altText='No short description available';
			}
		}
		catch(err)
		{
			// do nothing
			
		}
		
					
		/*if(i == largeImageIndex)
		{*/	
			//change altText
			regEx=/altText/
			bigImgClsngText_1=bigImgClsngText.replace(regEx, altText);
			
			fullRowText = fullRowText + bigImgDivStrtText + urlLinkStrtText + urlLink + urlLinkEndText + imgStrtText + fileName + bigImgClsngText_1;
		/*}
		else
		{
			regEx1=/_LL/
			regEx2=/altText/
			// assuming the filename will always have the _LL type, we will replace _LL with _LS for small images
			fileName=fileName.replace(regEx1, '_LS');
			
			//change altText 
			smlImgClsngText_1=smlImgClsngText.replace(regEx2, altText);
			
			fullRowText = fullRowText + smlImgDivStrtText + urlLinkStrtText + urlLink + urlLinkEndText + imgStrtText + fileName + smlImgClsngText_1;
		}	*/
		
					
	}
	fullDivText = fullRowText;
	//alert(fullDivText);
	document.getElementById(documentObjectId).innerHTML = fullDivText;
}

function randOrd(){
return (Math.round(Math.random())-0.9); }

var objectsParsed=new Array();
var objectCounter = 0;

function changeImage(id)
{
	linkParsed = false;
	//check if object is parsed already
	for(i=0;i<objectsParsed.length;i++)
	{
		obj = objectsParsed[i]
		linkParsed = obj===id
		
		if(linkParsed) 
			break;		
	}
		
	if(linkParsed)
		return;
	
	//check big or small image	
	imageName = id.getAttribute("src");
	
	bigImage = false;
	if(imageName.indexOf('_LL'))
		bigImage=true;	
	
	tempNewImage = imageName.substring(0, imageName.lastIndexOf('/'));	
	
	if(bigImage)
	{
		newImageName = tempNewImage+"/no-image_LL.gif";
	}
	else
	{
		newImageName = tempNewImage+"/no-image_LS.gif";
	}	
	
	try{	
	//assign new image
	id.src=newImageName;	
	}
	catch(er)
	{
		//do nothing
	}
	
	objectsParsed[objectCounter++]=id;
}

function showDynamicURLsForContactUs(obj,ContactViewURL)
{	anch = document.getElementsByTagName("a");
	for(i=0;i<anch.length;i++)
	{				
		element = anch[i];		
		if(element.getAttribute("id")==obj)
		{			 
			actualText = element.getAttribute("href");
			actualText = "_"+actualText.substring(actualText.lastIndexOf('/')+1,actualText.length);	
			urlText = ContactViewURL;	
			urlText = urlText+actualText;
			element.setAttribute("href", urlText);
		}
	}	
}

function showDynamicFooter(documentObjectId)
{
	body = document.getElementsByTagName("body");
	attributes=document.body.attributes;	
	
	cls=attributes["class"];
	if (cls != undefined)
	{
		if(cls.value!="box")
		{	
			newText = " <div id='footer-col-2'>&nbsp;</div><!--/footer-col-2-->";
			oldText = document.getElementById(documentObjectId).innerHTML;
			document.getElementById(documentObjectId).innerHTML = oldText + newText;
		}	
	}
}
