jQuery(function(){
    var config = {    
         sensitivity: 10,	// number = sensitivity threshold (must be 1 or higher)    
         interval: 0,		// number = milliseconds for onMouseOver polling interval    
         over: doOpen,		// function = onMouseOver callback (REQUIRED)    
         timeout: 200,		// number = milliseconds delay before onMouseOut    
         out: doClose		// function = onMouseOut callback (REQUIRED)    
    };
    
	function doOpen() {
		//get the position of the placeholder element
	    var pos = jQuery(this).offset();  
	    var height = jQuery(this).height();
	 
	    //show the menu directly over the placeholder
	    jQuery("#"+jQuery(this).attr('id')+"_Box").css( { "left": (pos.left + 5) + "px", "top": (pos.top + 32) + "px", "z-index":40} );
	    jQuery("#"+jQuery(this).attr('id')+"_Box").fadeIn('fast');
	}
	 
	function doClose() {
		CatID = jQuery(this).attr('id').split('_')[1];
		
		if (!CategoryObj[CatID]['HoverStatus']) {
			jQuery("#" + jQuery(this).attr('id') + "_Box").fadeOut('fast');
		}
	}

    jQuery("td[id^='CatID_']").hoverIntent(config);
	
	jQuery("div[id^='CatID_']").mouseenter(function(){
		CategoryObj[jQuery(this).attr('id').split('_')[1]]['HoverStatus'] = true;
	}).mouseleave(function(){
		CategoryObj[jQuery(this).attr('id').split('_')[1]]['HoverStatus'] = false;
		jQuery(this).fadeOut('fast');
	});

	if(jQuery("#LeftNav, #LeftNavSpecial").height() < jQuery("#ProductDiv").height()){
		jQuery("#LeftNav, #LeftNavSpecial").height(jQuery('#ProductDiv').height());
	}

	if(jQuery("#LeftNav, #LeftNavSpecial").height() < jQuery("#ShowProductDiv").height()){
		jQuery("#LeftNav, #LeftNavSpecial").height(jQuery('#ShowProductDiv').height());
	}

	jQuery("#SearchValue").autocomplete({
		source: function( request, response ) {
			jQuery.ajax({
				url: "index.cfm",
				dataType: "json",
				type:"POST",
				data: {
					fuseaction: "StoreDisplay.SearchAutoComplete",
					SearchValue: request.term
				},
				success: function( data ) {
					response( jQuery.map( data.terms[1], function(item, idx) {
						if (idx % 2 == 0) {
							return {
								label: item
							}
						}
					}));
				}
			});
		},
		select: function(event, ui) {
			window.location.hash = 'page=0';
			jQuery('#SearchValue').val(ui.item.label);
			jQuery('#SearchForm').submit();				
		},
		minLength: 3
	});

});

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

function clearSearch(inEl) {
	if(inEl.value == "What are you looking for?" || inEl.value == "Email Address") {
		inEl.value = "";
	}
}

/* Function to hide and show element */
function ShowHide(ElementToHide){
	ele = document.getElementById(ElementToHide).style;
	if(ele.visibility == 'hidden'){
		ele.visibility = 'visible';
	} else {
		ele.visibility = 'hidden';
	}
}

/* Function to hide and show content of a fieldset */
function OpenCloseFieldSet(ID,Fuseaction,ExtraVar){
	//Check if form element is null or not
	EleCheck = document.getElementById('FormDiv_'+ID);

	if (EleCheck != null){
		ele = document.getElementById('FormDiv_'+ID);
	} else {
		ele = document.getElementById(ID);
	}

	fie = document.getElementById('FieldSet_'+ID);
	img = document.getElementById('Image_'+ID);

	if (ele.style.display == 'block'){
		ele.style.display = 'none';
		img.src = img.src.replace('minus','plus');
	} else {
		ele.style.display = 'block';
		img.src = img.src.replace('plus','minus');
		if(ele.innerHTML.length < 10 && Fuseaction.length != 0){
			LoadDiv(ID,Fuseaction,'&'+ExtraVar)
		}
	}
}

/* Function to hide and show a Tab element */
function swithTab(TabID,bShow)
{
	Tabobj = document.getElementById(TabID);
	TabBtnobj = document.getElementById(TabID+"Btn");

	 if(bShow) {
		Tabobj.style.display="inline";
		Tabobj.style.visibility="visible";
		TabBtnobj.className ='active';
	} else{
		Tabobj.style.visibility="hidden";
		Tabobj.style.display="none";
		TabBtnobj.className ='';
	}
}

/* Function to hide and show a Tab element */
function showTab(ShowTabID, TabIDList)
{
	// Show the tab
	Tabobj = document.getElementById("tab" + ShowTabID);
	TabBtnobj = document.getElementById("tab" + ShowTabID+"Btn");

	Tabobj.style.display="inline";
	Tabobj.style.visibility="visible";
	TabBtnobj.className ='active';

	for(i=0; i<TabIDList.length; i++)
	{
		if (ShowTabID != TabIDList[i] )
		{
			Tabobj = document.getElementById("tab" + TabIDList[i]);
			TabBtnobj = document.getElementById("tab" + TabIDList[i]+"Btn");

			Tabobj.style.visibility="hidden";
			Tabobj.style.display="none";
			TabBtnobj.className ='';
		}
	}
}

/* Time Functions START*/
function padlength(what){
	var output=(what.toString().length==1)? "0"+what : what;
	return output;
}

function displaytime(){
	var ThisDate = new Date();
	var timestring=padlength(ThisDate.getHours())+":"+padlength(ThisDate.getMinutes())+":"+padlength(ThisDate.getSeconds());
	document.getElementById("TimeID").innerHTML = timestring;
}

function ShowTime(){
	setInterval("displaytime()", 1);
}
/* Time Function END */

/* Sliding Tab START */
var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var px = document.layers ? "" : "px";
function JSFX_FloatDiv(id, sx, sy)
{
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};
	el.flt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth :
		document.documentElement && document.documentElement.clientWidth ?
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ?
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0)
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ?
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/6;this.cy += (pY + this.sy - this.cy)/6;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.flt()", 10);
	}
	return el;
}

function SlideTab(){
	JSFX_FloatDiv("SideTab", 214, 200).flt();
}
/* Sliding Tab END */

function SideNavTween(Position) {
	if (Position == "0px"){
		From = 0;
		To = -214;

		MarginFrom = 240;
		MarginTo = 26;

		WidthFrom = iW - 255;
		WidthTo = iW - 41;

		OpFrom = 0;
		OpTo = 100;
	} else {
		From = -214;
		To = 0;

		MarginFrom = 26;
		MarginTo = 240;

		WidthFrom = iW - 41;
		WidthTo = iW - 255;

		OpFrom = 100;
		OpTo = 0;
	}

	t1 = new Tween(document.getElementById('LeftNav').style,'left',Tween.strongEaseInOut,From,To,.75,'px');
	t2 = new Tween(document.getElementById('MainContainer').style,'marginLeft',Tween.strongEaseInOut,MarginFrom,MarginTo,.75,'px');
	t3 = new Tween(document.getElementById('MainContainer').style,'width',Tween.strongEaseInOut,WidthFrom,WidthTo,.75,'px');
	t4 = new OpacityTween(document.getElementById('SideTabOpen'),Tween.strongEaseInOut, OpFrom, OpTo, .75);
	t5 = new Tween(document.getElementById('Loading').style,'marginLeft',Tween.strongEaseInOut,MarginFrom,MarginTo,.75,'px');

	t1.start();
	t2.start();
	t3.start();
	t4.start();
	t5.start();
}

function TopSectionTween(Position) {
	if (Position == "0px"){
		From = 0;
		To = -53;

		MarginMainFrom = 90;
		MarginMainTo = 36;

		NavFrom = 48;
		NavTo = -5;

		OpFrom = 100;
		OpTo = 0;
	} else {
		From = -53;
		To = 0;

		MarginMainFrom = 36;
		MarginMainTo = 90;

		NavFrom = -5;
		NavTo = 48;

		OpFrom = 0;
		OpTo = 100;
	}

	t1 = new Tween(document.getElementById('TopDiv').style,'top',Tween.strongEaseInOut,From,To,.75,'px');
	t2 = new Tween(document.getElementById('MainContainer').style,'marginTop',Tween.strongEaseInOut,MarginMainFrom,MarginMainTo,.75,'px');
	t3 = new Tween(document.getElementById('LeftNav').style,'top',Tween.strongEaseInOut,NavFrom,NavTo,.75,'px');
	t4 = new OpacityTween(document.getElementById('TopTabImage'),Tween.strongEaseInOut, OpFrom, OpTo, .75);
	t5 = new Tween(document.getElementById('Loading').style,'marginTop',Tween.strongEaseInOut,MarginMainFrom,MarginMainTo,.75,'px');

	t1.start();
	t2.start();
	t3.start();
	t4.start();
	t5.start();
}

function Resize(){
	iW = document.documentElement.offsetWidth;

	document.getElementById('TopCenter').style.width = iW - 316 + "px";
	document.getElementById('MainContainer').style.width = iW - 255 + "px";

	McH = document.getElementById('MainContainer').offsetHeight;

	document.getElementById('LeftNav').style.height = McH + 39 + "px";
}

// Ajax Call
function LoadPages(fuseaction,VariableString) {
	if (!VariableString){
		VariableString = "";
	}
	obj = new Ajax();
	obj.loadValues('index.cfm?fa='+fuseaction+VariableString,'Loading','MainContainer');
}

function LoadDiv(DivName,fuseaction,VariableString) {
	if (!VariableString){
		VariableString = "";
	}

	Obj = new Ajax();
	Obj.loadValues('index.cfm?fa='+fuseaction+VariableString,'Loading',DivName);
}

function LoadNewScript(fuseaction,VariableString,ScriptId) {
	if (!VariableString){
		VariableString = "";
	}
	obj = new Ajax();
	obj.LoadScriptValues('index.cfm?fa='+fuseaction+VariableString,'Loading',ScriptId);
}

/* Load the additional script, only once. */
function LoadScript(sPath, sSRC) {
	var head = document.getElementsByTagName("head")[0];
	sSRCArray = sSRC.split(',');

	for (i=0; i<document.getElementsByTagName("script").length; i++){
		// Get the name of the script
		NameArray = document.getElementsByTagName("script")[i].src.split('/');
		FileName = NameArray[NameArray.length-1];

		for (j=sSRCArray.length; j>=0; j--){
			if (sSRCArray[j] == FileName) {
				sSRCArray.splice(j);
			}
		}
	}

	//Add Scripts to the head of the HTML document
	for (i=0; i<sSRCArray.length; i++){
		NewScript = document.createElement('script');
		NewScript.src = '../public/Module/'+sPath+'/Includes/'+sSRCArray[i]; //ScriptPath
		NewScript.type = 'text/javascript'; //Script Type

		//Append the script to the head of the document
		head.appendChild(NewScript);
	}
}

/* Load a dynamic function */
function LoadDynamicFunction(sScriptCode) {
	var head = document.getElementsByTagName("head")[0];
	NewScript = document.createElement('script');
	NewScript.type = 'text/javascript';
	NewScript.innerHTML = sScriptCode;
	head.appendChild(NewScript);
}


function trim(sString){
	if (sString != null)
	{
		var regEx = /^[ \t\r\n\f]+|[ \t\r\n\f]+$/g;
		var ReturnString = sString.replace(regEx, "");
		return ReturnString;
	} else
		return "";
}

function DeleteUrlFunction(sItem,fuseaction,VariableString,DivName){
	var answer = confirm ("Are you sure you wish to delete this "+sItem+"?");

	document.location.href = 'index.cfm?fa=' + fuseaction + VariableString;
}

function DeleteFunction(sItem,fuseaction,VariableString,DivName){
	var answer = confirm ("Are you sure you wish to delete this "+sItem+"?");

	if (answer == true) {
		if (DivName != null) {
			LoadDiv(DivName,fuseaction,VariableString);
		} else	{
			LoadPages(fuseaction,VariableString);
		}
	}
}

function ConfirmFunction(sMsg,fuseaction,VariableString,DivName){
	var answer = confirm (sMsg);

	if (answer == true) {
		if (DivName != null) {
			LoadDiv(DivName,fuseaction,VariableString);
		} else	{
			LoadPages(fuseaction,VariableString);
		}
	}
}


function CreateFormFieldObj(MyForm, MyFunction, DivName){
	var f=MyForm;
	var SkipFunction = false;

	if (DivName == null)
	{
		DivName = 'MainContainer';
	}

	SkipFunction = (sFunction == '');
	//Validate that the form as been properly filled
	var sFunction = MyFunction+"(f);";

	if (SkipFunction || eval(sFunction)){
		//Generate an array of objects to loop through
		var FieldArray = new Array();
		for(i=0;i<f.length;i++) {
			e = f.elements[i];

			if((e.type != "button")) {

				if((e.type != "checkbox") && (e.type != "radio") && (e.type != "select-multiple")) {
					FieldArray[FieldArray.length] = e;
				} else {
					if (e.type != "select-multiple") {

						// Look for a obj who's already be passed to the loop
						chkObjExist = 0;
						for(j=0;j<FieldArray.length;j++){
							if(FieldArray[j].name == e.name){
								chkObjExist=j;
							}
						}

						// if the object doesnt't exist. Create it
						if(!chkObjExist) {
							idx = FieldArray.length;
							FieldArray[idx] = new oCR(e);
							FieldArray[idx].myOptions[FieldArray[idx].myOptions.length] = new oCROption(e.checked, e.value);
						} else { // Insert option value
							FieldArray[chkObjExist].myOptions[FieldArray[chkObjExist].myOptions.length] = new oCROption(e.checked, e.value);
						}
					} else {
						idx = FieldArray.length;
						FieldArray[idx] = new oCR(e);

						// Loop to insert every value on the current select multiple
						for(j=0;j<e.length;j++) {
							FieldArray[idx].myOptions[FieldArray[idx].myOptions.length] = new oCROption(e[j].selected, e[j].value);
						}
					}
				}
			}
		}

		//Generate an string with the field name and values to be passed the submission fuseaction
		var sFromValue = "";
		for(i=0;i<FieldArray.length;i++) {
			if ((FieldArray[i].type != "checkbox") && (FieldArray[i].type != "radio") && (FieldArray[i].type != "select-multiple") ) {
				sFromValue += "&"+FieldArray[i].name+"="+encodeURIComponent(FieldArray[i].value);
			} else {
				var FieldValue = new Array();
				for(j=0;j<FieldArray[i].myOptions.length;j++) {
					if (FieldArray[i].myOptions[j].checked) {
						FieldValue.push(FieldArray[i].myOptions[j].value);
					}
				}
				sFromValue += "&"+FieldArray[i].name+"="+encodeURIComponent(FieldValue);
			}
		}

		//LoadPages(f.fuseaction.value, sFromValue);
		LoadDiv(DivName,f.fuseaction.value, sFromValue);
	}
}

// Make a generic validation.
function GenericValidation(MyForm) {
	var f=MyForm;

	for(i=0;i<f.length;i++) {
		e = f.elements[i];
		e.alt = trim(e.alt);

		var bVal = e.getAttribute("valrequired");
		var cFormat = e.getAttribute("format");

		// Set the compare function ex : First Field : email, Compare Field : email_compare
		if (e.name.search(/_compare/) != -1){
			c = e.name.split("_");
			document.getElementById(c[0]).compare = e;
		}

		if ((bVal != null) && (bVal.toLowerCase() == "true")){
			if ((e.type == "select-one") && (e.value == 0)){
				e.alt = trim(e.title);
				e.min = 1;
			} else if ((e.type == "text") && ((cFormat != null) && (cFormat.toLowerCase() == "email"))){
				e.emailFormat = true;
			} else if((e.type == "text") && ((cFormat != null) && (cFormat.toLowerCase() == "numeric"))){
				e.numeric = true;
			} else if (e.type == "textarea"){
				e.alt = trim(e.title);
			} else if ((e.type == "hidden") && (e.name.search(/FileUploaded/) == 0)){
				e.optional = false;
				e.alt = trim(e.title);
			}
		} else {
			e.optional=true;
		}
	}

	if (clickcheck == 0) {
	  return verify(f,true,false);
	} else {
	  return false;
	}
}

// Disable Enter Key
function handleEnter(field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;
		i = (i + 1) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	}
	else
	return true;
}

function showCoords(evt){
  alert(
    "clientX value: " + evt.clientX + "\n" +
    "clientY value: " + evt.clientY + "\n"
  );
}

getElementByComplexID = function(ID){
	if (ID.indexOf("parent.") > -1)
	{
		var sRealID = ID.substring(7);
		return parent.document.getElementById(sRealID);
	} else {
		return document.getElementById(ID);
	}
}

EvalScript = function(sString){
	if (sString != null)
	{
		var regEx = /<script\b[^>]*>([\s\S]*?)<\/script>/ig;

		while ( (scripts=regEx.exec(trim(sString))) != null) {
			eval(scripts[1]);
		}
	}
}

function loadValues(GetInfoURL,GetLoadingDiv,GetOutputDiv){
	Obj = new Ajax();
	Obj.loadValues(GetInfoURL,GetLoadingDiv,GetOutputDiv);
}


function showPopWin(paramUrl, winWidth, winHeight, Resize, WindowTitle) {
	if (Resize == null){
		Resize=false;
	}

	Dialog.info({url: 'index.cfm', options: {evalScripts:true,method: 'get', parameters:paramUrl}},{className: 'alphacube', title:WindowTitle, width:winWidth, height:winHeight, destroyOnClose: true, draggable:true, recenterAuto:false, resizable:Resize, closable:true});
}

function ShowTabItem(NameGroup,ItemID, Fuseaction, FuseParams){
	Tab = document.getElementById(NameGroup + '_'+ItemID);
	TabLength = (document.getElementById(NameGroup + '_Length').value);

	for(i=1; i <= TabLength; i++){
		document.getElementById(NameGroup + '_' + i).className = 'TabOff';
	}

	Tab.className = 'TabOn';

	LoadDiv('DivTab' + NameGroup ,Fuseaction,FuseParams);
}

function TypeSubType(FieldID, SubFieldID, ComponentValue, ComponentID, SubComponentID, Obj){
	FieldEle = document.getElementById(FieldID);
	SubFieldEle = document.getElementById(SubFieldID);
	
	if (FieldEle.value != 0) {
		SubFieldEle.disabled = false;
		if(Obj){
			LoadDiv('ActionDiv','IndividualMod.SetProperties','&Object='+Obj+'&SetFunction='+FieldEle.id+'&Value='+FieldEle.value);
		}
		LoadDiv(SubFieldEle.id+'_Container','IndividualMod.SetSubTypeValue','&TagID='+SubFieldEle.id+'&ComponentValue='+ComponentValue+'&ComponentID='+ComponentID+'&SubComponentID='+SubComponentID+'&Object='+Obj);
	} else {
		SubFieldEle.disabled = true;
		SubFieldEle.selectedIndex = 0;
		if(Obj){
			LoadDiv('ActionDiv','IndividualMod.SetProperties','&Object='+Obj+'&SetFunction='+FieldEle.id+'&Value='+FieldEle.value);
		}
	}
}

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

