Event.observe(window,'load',pageLoaded,false);

var btns = new Array();
function pageLoaded(e)
{
	$('emailAddress').value ='Enter your email address';
	$('keyword').value='Keyword or Item #';
	//Event.observe('navigation','click',menuClickEvent,false);
	//Event.stop(e);
	/*var liList = $('navigation').getElementsByTagName('li');
	liList = $A(liList);
	liList.each(function(el){
	Event.observe(el,'mouseover',function(evt){$('navSub').removeClassName('gray'); Event.stop(evt) });
	});
	
	 btns = document.getElementsByClassName("btn");
	for (var i = 0; i < btns.length; i++)
	{
	btns[i],'mouseover',function(evt){Event.element(evt).addClassName('btnOver'); Event.stop(evt) });
	}
	*/
	
	Event.observe('emailAddress','focus',inputEvent,false);
	Event.observe('emailAddress','blur',inputEvent,false);
	Event.observe('keyword','focus',inputEvent,false);
	Event.observe('keyword','blur',inputEvent,false);
		
}

function inputEvent(e)
{
	var el = Event.element(e);
	el.removeClassName('txtError');
	el.addClassName('txt');
	initElement(el);
	
}

function initElement(el){
	if(el.id =='emailAddress')
			initMessage(el,'Enter your email address');
	else if(el.id =='keyword')
		initMessage(el, 'Keyword or Item #');
}


function initMessage(el,mess){
	if(el.value == mess)
		el.value = '';
	else if(el.value == '')
		el.value = mess;
}

function menuClickEvent(e) {
	$('navSub').addClassName('redBg');	
}


function setActiveMenu(id,className)
{
	$(id).addClassName(className);
	if(id=="outlet")	
		$('navSub').addClassName('redBg');
	else
		$('navSub').addClassName('lightGrayBg');
				
}

function openPopupImage(evt){
	var link = Event.element(evt);

	alert (link.tagName);
	return false;
}


//attributes section
function Attribute(prdKey, atrId,atrDsc,atrLink,atrLinkDsc, atrDepUpn,atrDepMod,options,dependents ){
	this.prdKey = prdKey;
	this.atrId = atrId;
	this.atrDsc = atrDsc;
	this.atrLink = atrLink;
	this.atrLinkDsc = atrLinkDsc;
	this.atrDepUpn = atrDepUpn;
	this.atrDepMod = atrDepMod;
	this.options = options;
	this.dependents = dependents;
	
}

function MyOption(prdKey, atrId,atrValId,atrValDsc, appItmNum, appItmNumVal,incOrdNot){
	this.prdKey = prdKey;
	this.atrId = atrId;
	this.atrValId = atrValId;
	this.atrValDsc = atrValDsc;
	this.appItmNum=appItmNum;
	this.appItmNumVal = appItmNumVal;
	this.incOrdNot = incOrdNot;
}


function DependentOption(prdKey, depAtrId, depAtrValId,ctlAtrId,ctlAtrValId)
{
	this.prdKey=prdKey;
	this.depAtrId=depAtrId;
	this.depAtrValId = depAtrValId;
	this.ctlAtrId = ctlAtrId;
	this.ctlAtrValId = ctlAtrValId;
}

new MyOption();
new Attribute();
new DependentOption();

function getAttributesOptions(attributes,attrName)
{
	var attr = getAttribute(attributes, attrName);
	if(attr != null)		  
		return attr.options;
	return null;
	
	
}

function getAttribute(attributes, attrName)
{
	attrName = attrName.toUpperCase();
	for(var i = 0; i<attributes.length; i++)
	{	
		var attr = attributes[i];
		//alert(attr.atrDsc);
		if(attr!= null && attr.atrDsc.toUpperCase() == attrName){
			  
			return attr;
		}
	}
	return null;
}

function setSelectOptions(options, field)
{
	var fieldDesc = field;
	
	if(field.substring(0,2)=='b_')
	fieldDesc = field.substring(2);
	
	if(options==null || $(field) == null  )
		return;
		
	// 05/06/2008 ksa/rwp
	// Livevalidation 1.3 includes support for disable and enable (see livevalidation.js)
	// Following if/else added to support attribute dependency where selection of certain 
	//  controlling attribute values would exclude all dependent attribute values.
	if(options=="") {
		$(field).disable();
		$(field).hide();
		var f = new LiveValidation($(field),{onlyOnSubmit: true});
		f.disable(); 
		return;
	}
	else {
		$(field).enable();
		$(field).show();
		var f = new LiveValidation($(field),{onlyOnSubmit: true});
		f.enable();
		f.add(Validate.Presence,{failureMessage:"Select "+element});
	}
		
	$(field).innerHTML="";
	$(field).options.add(new Option("--- "+fieldDesc+" ---",''));
	for(var i = 0; i < options.length; i++ )
	{
	var opt = options[i];
	
	$(field).options.add(new Option(opt.atrValDsc,opt.atrValId));
	
	}	
}

/**
 * not used
 */
function initAttributes()
{
var colors = getAttributesOptions(attributes,'color');
var sizes = getAttributesOptions (attributes,'size');
var strands = getAttributesOptions(attributes,'strand');
setSelectOptions(colors,'color');
setSelectOptions(sizes,'size');
setSelectOptions(strands,'strand');
}

/**
 * Finds the options for the targetAttrName select button
 * based on the value of the cnotrolling select crtAttrId
 */
function getAttributesOptionsFor(targetAttrName,crtAttrId)
{
	//alert("controling "+crtAttrId+": " +$F(crtAttrId));
	if($F(crtAttrId)=='')
		return;
	var driver = parseInt($F(crtAttrId));
	//alert('hi');
	var attr = getAttribute(attributes,targetAttrName);
	//alert(driver + ", "+attr.atrDepMod);
	//alert(attr.atrDsc);
	//alert("ctlId "+driver+", attr depen mod "+attr.atrDepMod+ ", attr depen upon "+attr.atrDepUpn);
	var atrDepMod = attr.atrDepMod;
	if(atrDepMod ==null || atrDepMod =="")
		return;
	
	
	var options = attr.options;
	var dependents = attr.dependents;
	var newOptions = new Array();
	for (var i =0; i < options.length; i++)
	{
		var opt = options[i]
		
		
		var found = isFound(opt,dependents,driver );
		
		if(atrDepMod =='I' && found)
		{
			newOptions[newOptions.length] =opt;
			 
		}		
		else if(atrDepMod =='E' && !found)
		{	//alert("one excluded");
			newOptions[newOptions.length] =opt;
		}
		
			
			
			
	}
		
	setSelectOptions(newOptions,targetAttrName);
}

function getBonusAttributesOptionsFor( targetAttrName,crtAttrId)
{
	//alert("controling "+crtAttrId+": " +$F(crtAttrId));
	if($F(crtAttrId)=='')
		return;
	var driver = parseInt($F(crtAttrId));
	//alert('hi');
	var attr = getAttribute(bonAttributes,targetAttrName);
	//alert(driver + ", "+attr.atrDepMod);
	//alert(attr.atrDsc);
	//alert("ctlId "+driver+", attr depen mod "+attr.atrDepMod+ ", attr depen upon "+attr.atrDepUpn);
	var atrDepMod = attr.atrDepMod;
	if(atrDepMod ==null || atrDepMod =="")
		return;
	
	
	var options = attr.options;
	var dependents = attr.dependents;
	var newOptions = new Array();
	for (var i =0; i < options.length; i++)
	{
		var opt = options[i]
		
		
		var found = isFound(opt,dependents,driver );
		
		if(atrDepMod =='I' && found)
		{
			newOptions[newOptions.length] =opt;
			 
		}		
		else if(atrDepMod =='E' && !found)
		{	//alert("one excluded");
			newOptions[newOptions.length] =opt;
		}
		
			
			
			
	}
		
	
	setSelectOptions(newOptions,targetAttrName);
}

function isFound(opt,dependents,driver )
{
	//alert(dependents.length);
	var prdKey = opt.prdKey;
	var atrId = opt.atrId;
	var atrValId = opt.atrValId;
	for(var j = 0; j < dependents.length; j++)
		{
			var dep = dependents[j];
			if (dep.prdKey == prdKey && dep.depAtrId == atrId && dep.depAtrValId == atrValId && dep.ctlAtrValId == driver )
						
					return true;
				 
		}
		return false;
}

function ajaxCall(url, params,callback)
{
	new Ajax.Request( url, { method: 'post', parameters: params, onSuccess: callback });
}

function alertMessage(){
alert('Your requested has been submitted');
}

function check(el, mess)
{
	if($(el).value == '' || $(el).value == mess)
	{
		//alert ('Please Enter a value in the text field');
		$(el).addClassName('txtError');
		return false;
	}
	
	return true;
}

function resetCheckboxes()
{
	 $$('#clientRequestForm input.check').each(function (box){
	 											if (box.checked == false)
	 												box.value = "";
	 										//		alert(box.value);
	 											});
	return true;
}


function getAttributeById(atrs, id){
	if(!atrs || id) 
	return null;
	
	for(var i = 0; i < atrs.length; i++){
		var atr = atrs[i];
		if(atr.atrId ==id)
			return atr;
	}
	return null;
}


function getOptionByAtrValueId(opts, id){
	
	if(!opts || !id) 
	return null;
	
	for(var i = 0; i <opts.length; i++){
		var opt = opts[i];
		if(opt.atrValId ==id)
			return opt;
	}
	return null;
	
}


	    
function getTime(){
var date = new Date();
return  date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
}
function storeInSession(name,value,fn){
		var url = '/catalog/multiTasker.do';
		params={action:'storeInSession', name:name, value:value};
	    ajaxCall(url,params,fn)
		}
function setShoppingCartLink(link){
	//alert('here');
	var shop = $('shoppingCart');
	shop.href=link;
	shop = $('viewShoppingCart');
	shop.href=link;
	var currentUrl = window.location.href;
	//alert(currentUrl);
	if(currentUrl.indexOf("action=precedent") == -1)
			storeInSession("returnTo",currentUrl,function(){});
	//alert(shop.href);
	
	
}

function sendEmail(subject, message){
	//alert(subject);
	var url = '/catalog/clientRequest.do';
	params={requestType:'sendShoppingCartError', subject:subject, message:message};
	ajaxCall(url,params,function(transport){});
}

function getWindowSize(){
		var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	   var dim = {width:myWidth,height:myHeight};
	   return dim;
	}
	
	function show(el){
	var dim = getWindowSize();
    	var s_left = parseInt(dim.width/2);
     	var s_top = parseInt(dim.height/2 );
     	alert(s_left, s_top);
     	var element =  $(el);
     	element.setStyle({left: s_left, top:s_top} );
        element.show(); 
}

function hide(el){
	var element =  $(el);
     	
        element.hide(); 
}

function handleKeyEvent(e){
	var code;
	var targ;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) 
		targ = targ.parentNode;
		
}
//
//function findCtlAttributeIndex(attributes, depUpon){
//	var l = attributes.length;
//	for(var i = 0 ; i < l ; i++){
//		if(attributes[i].depUpn == depUpon)
//		return i;
//	}
//	
//	return -1;
//}

/**
 * retun the controlling attribute index
 * return -1 if not found 
 */
function findCtlAttributeIndex(attributes, depUpon){
	var l = attributes.length;
	for(var i = 0 ; i < l ; i++){
		if(attributes[i].atrId == depUpon)
		return i;
	}
	
	return -1;
}


// Finds the controlling attribute desc, which is used as the controlling 
// // attribute select  Dom Id.
function findCtlAttributeDesc(attributes, depUpon){
	var l = attributes.length;
	for(var i = 0 ; i < l ; i++){
		if(attributes[i].atrId == depUpon)
		return attributes[i].atrDsc;
	}
	
	return "";
}


