/////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called to process the category name and return a name that is usable by a css  
// class. Of course the resulting class must exist in the Css definition.
// Spaces are replaced by '_' and '&' are simply removed.
/////////////////////////////////////////////////////////////////////////////////////////////////////
function subCatName(url) 
{
	var str=url
	str = str.replace("&", "");
	str = str.replace(" ","_").toLowerCase();
	str = str.replace(" ","").toLowerCase();
	document.write("\"");
	document.write(str);
	document.write("\"");
}

function ValidateNameLength(name, ctrlname, lenLimit)
{
	if(name.length > lenLimit){
		alert( 'There is a ' + lenLimit + ' characters limit for the ' + ctrlname);
		return false;
	}
	return true;
}

function IsEmptyString(StringToCheck)
{
    
    //first remove all spaces using the following regex
    StrToCheck= StringToCheck.replace(/^\s+|\s+$/, '');

    //then we check for the length of the string if its 0 or not
    if( StrToCheck.length==0)
        return true;
    else
        return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
// The intent of this function is to verify that the user selected an option if other fields
// are filled.
// An example beeing the "engraving font" needs to be selected if the customer wrote in the
// engraving fields.
// Input:
// arrayOfOthers: content of the fields we are checking 
// name: content of the field of interest (the engraving font option for example)
// ctrlname : name of the option
// defaultContent : content we want the name to be different from. (name != defaultContent)
/////////////////////////////////////////////////////////////////////////////////////////////////////
function ValidateFieldIfOthersAreUsed(arrayOfOthers, name, ctrlname, defaultContent)
{
    var found = false;
    var x;                                                                                                 

    for (x in arrayOfOthers){        
        if(IsEmptyString(arrayOfOthers[x]) == false ){
            found = true;
        }    
    }

    if(found == true){
        if(name == defaultContent){
            alert( 'Please select an option for the ' + ctrlname);
            return false;
        }
    }
    return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
// This function will update the target element (idTarget)) using data given in the param.
// Input Parameters:
// param : array containing the list of the different options with the associated image
// idTarget : id of the element of the page where we are going to insert the HTML code.
/////////////////////////////////////////////////////////////////////////////////////////////////////
function PopulateOptionsWindow(param, idTarget ) 
{			
	var txt = "";
	var ni = document.getElementById(idTarget); 

	for (var i = 0; i < param.length; i++)
	{
		txt = txt + '<a href="" ><img src="http://www.petitbliss.com/shop/images/Imgs/' + param[i][1] + '" alt="img" /><b>' + param[i][0] +'</b></a>';
	}
	ni.innerHTML = txt;											
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
// Opens a window that points to pFileName.
// Input Parameters:
// pFileName : URL of the document the window will display
// pTitle 	 : Title of the window.
/////////////////////////////////////////////////////////////////////////////////////////////////////
function show_photo( pFileName, pTitle) 
{
  photoWin = window.open( pFileName, pTitle,"width=600,height=450,status,scrollbars,resizable, screenX=20,screenY=40,left=20,top=40");
  return false;
}

function changeSelectionOfSelectControl(controlName, x, inputControlID, optionName) 
{
	var ni = document.getElementById(controlName); 	
	ni.selectedIndex = x;
	
	ni = document.getElementById(inputControlID); 	
	ni.setAttribute('value', optionName);	
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
// This function will update the action attribute of a form.
// Useful when a form submit is triggered outside of the form itself.
// Input Parameters:
// formID : id of the form to target
// actionText : action to perform on submit of the form
/////////////////////////////////////////////////////////////////////////////////////////////////////

function changeActionOnForm( formID, actionText)
{
    var ni = document.getElementById(formID);     
    ni.setAttribute('action', actionText);
    
    return true;

}
/////////////////////////////////////////////////////////////////////////////////////////////////////
// This function will monitor a given textbox and report the number of typed characters in 
// a target control "opt_countBody". 
// If the maximum size is reached, it will cut the text lenght
//
// Example:
// <textarea rows="3" id="msgGiftTxt" name="msgGiftTxt" cols="55" onkeyup="counterUpdate('msgGiftTxt', 'countBody','200');"></textarea>
//
// Input Parameters:
// opt_countedTextBox : id of the control to monitor
// opt_countBody : id of the control where the number of characters is going to be reported to
// opt_maxSize : max amount of characters we allow.
/////////////////////////////////////////////////////////////////////////////////////////////////////
function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) 
{
        var countedTextBox = opt_countedTextBox;
        var countBody = opt_countBody;
        var maxSize = opt_maxSize;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
        if (txtField) 
		{  
                txtField.innerHTML = field.value.length + " characters typed (max "+maxSize+")";
        }
}