// JavaScript Document




/*PRODUCT FUNCTIONS*/
function getUrlHash(){
	url = window.location.href.split("#");
	debug('getting URL Hash - result ='+url);
	if(!url[1])return false;
	else {
		hash = url[1].split("-");
		ajaxLocation = hash[0];
		ret=new Array();
		ret[0] = url[1];
		ret[1] = hash[0];
		return ret;
	}
}



function loadProductPage()
{
	dhtmlHistory.initialize();	
	dhtmlHistory.addListener(showProducts);
	url_info = getUrlHash();
	debug("loadProductPage - URL Info -"+url_info);
	debug('calling showProducts');
	if(dhtmlHistory.isFirstLoad())
		showProducts(url_info[0],"");
	debug('back from show products and in loadProductPage');
	debug('end loadProductPage');	
	
}

function showProducts(newLocation, callback)
{
	url_info = getUrlHash();
	debug('in showProducts');
	debug('showProducts-url_info ='+url_info);
	if(url_info[1]=="roller")
	{
		debug('showProducts url_info == roller');
		if(!roller_state)
		{
			debug('no roller_state, showProductRoller');
			showProductRoller();
		}
		debug('back from showProductRoller - Showing roller Section '+newLocation);
		window.productGroupRoller.showSection( newLocation.slice(12));
	}
	else if(url_info[1]=='product')
	{
		debug('showProducts - url_info = product, hiding product roller');
		
		product_id = newLocation.slice(16);
		debug('loading product details');
		if(roller_state);
		{
			debug('productRoller visible - hidding');
			hideProductRoller();		
		}
		loadProductDetails(product_id);
	}
	else{
		debug("ShowProducts - no url info - creating product roller");
		showProductRoller();
	}
	return false;
}

function productDetailsClick(product_id)
{
	debug('clicked to product_details id='+product_id);
	debug('adding url click');
	dhtmlHistory.add("product-details-"+product_id, "loadProductDetails");
	//if we have a productDetailsClick then the user clicked the link from a shown product roller,
	//we should hide the roller, get the product details, and show the details.
	//hide the roller
	debug('loading product details');
	loadProductDetails(product_id);
}

function hideProductRoller()
{	
	debug('in hideProductRoller - hiding product roller');
	roller_div = document.getElementById('product_roller_block');
	roller_div.style.display='none';
	roller_state=0;
}
function showProductRoller()
{
	debug('in ShowProductRoller');
	if(details_state)
	{	
		debug('details_state ='+details_state+' so hidingProductDetails');
		hideProductDetails();
	}
	if(!roller_state)
	{	debug('setting the roller_div to display:block');
		roller_div = document.getElementById('product_roller_block');
		roller_div.style.display='block';
		roller_state=1;
	}
	if(!window.productGroupRoller)
	{
		debug('window.productGroupRoller not found, now loading product roller');
		loadProductGroupRoller();
		debug('done');
	}
	
}
function hideProductDetails()
{	
	debug('in HideProductDetails');
	details = document.getElementById('product_details_block');
	details.style.display='none';
	details_state=0;
}

function loadProductDetails(product_id)
{ 
	debug('in loadProductDetails');
	cp.set_response_type('text');
	cp.call('ajax/product_details.php', 'get_product_details', showProductDetails, product_id);
	debug('ajax call done');
}




function showProductDetails(result)
{
	debug('in showProductDetails');
	details = document.getElementById('product_details_block');
	debug('roller_state ='+roller_state);
	if(!details_state)
	{
		debug('details are hidden, setting to block');
		details.style.display='inline';	//<-- this is needed to keep the div from doubbling in height in mozilla... odd.
		details.style.display='block';
		details.style.height="310px";
		details_state=1;
	}
	if(roller_state)hideProductRoller();
	details.innerHTML=result; 
}

var large_1 = "";
var large_2 = "";
var small_1 = "";
var small_2 = "";
var cur_img = "large_1";

function setProductImages(l1,l2,s1,s2)
{
	large_1 = new Image();
	large_1.src = "pictures/product_images/"+l1;
	large_2 = new Image();
	large_2.src = "pictures/product_images/"+l2;
	small_1 = s1;
	small_2 = s2;
}

function imgSwap(sm, thumb)
{			
	if(cur_img == "large_1")
	{
		
		thumb.src = large_2.src;
		//thumb.width = large_2.width;
		thumb.height = large_2.height;
		
		sm.src="pictures/product_images/"+small_1;			
		cur_img = "large_2";		
	}
	else if(cur_img == "large_2")
	{
		thumb.src = large_1.src;
		//thumb.width = large_1.width;
		thumb.height = large_1.height;
		sm.src="pictures/product_images/"+small_2;			
		cur_img = "large_1";		
	}
}


/*END PRODUCT FUNCTIONS*/


/*ADDRESS BOOK FUNCTIONS*/

//Preforms various functions to add ajax to the address book.

function getAddressObjects()
{
	addbook = new Array();
	addbook['first_name']= document.getElementById('first_name');
	addbook['last_name']= document.getElementById('last_name');
	addbook['address']= document.getElementById('address');
	addbook['address2']= document.getElementById('address2');
	addbook['city']= document.getElementById('city');
	addbook['state']= document.getElementById('state');
	addbook['zip']= document.getElementById('zip');
	addbook['phone']= document.getElementById('phone');
	return addbook;
}
//don't think this function is used
function getAddressErrorObjects()    
{ 
	addbook = new Array();
	addbook['first_name_error']= document.getElementById('first_name_error');
	addbook['last_name_error']= document.getElementById('last_name_error');
	addbook['address_error']= document.getElementById('address_error');
	addbook['address_error2']= document.getElementById('address_error2');
	addbook['city_error']= document.getElementById('city_error');
	addbook['state_error']= document.getElementById('state_error');
	addbook['zip_error']= document.getElementById('zip_error');
	addbook['phone_error']= document.getElementById('phone_error');
	return addbook;
}

function ajaxAddressBook()
{
	addbook=getAddressObjects();
	button = document.getElementById('address_book_submit');
	
	//sets the ajax request for adding an address.
	button.onclick=sendAddressRequest;
	
	for(fieldName in addbook)
	{
		addbook[fieldName].onkeyup=showAddBookSubmit;	
	}
	
	tabsblock = document.getElementById("tabs");
	booklinks = tabsblock.getElementsByTagName("a");
	for(i=0;i<booklinks.length;i++)
	{
		
		if(booklinks[i].id !='new_address_link' && booklinks[i].id != 'overflowLink')
		booklinks[i].onclick=addBookSwitchTabs;
	}
	document.getElementById('new_address_link').onclick=showNewAddressForm;
}

function showAddBookSubmit()
{	
	if(address_book_submit_shown == false)
	{
		button = document.getElementById('address_book_submit');
		label = document.getElementById('address_book_submit_label');
		button.style.display='block';
		label.style.display='block';
		address_book_submit_shown=true;
	}
	
}	

function hideAddBookSubmit()
{	
	if(address_book_submit_shown == true)
	{
		button = document.getElementById('address_book_submit');
		label = document.getElementById('address_book_submit_label');
		button.src="images/b_r_save.gif";
		button.style.display='none';
		label.style.display='none';
		address_book_submit_shown=false;
	}
	
}	

function sendAddressRequest()
{
	addressBookClearErrors();
	
	showAddBookSubmit();
	//Set the loading graphic for the button
	button = document.getElementById('address_book_submit');
	button.src='images/loading.gif';
	addbook = getAddressObjects();

	xml = "<address>";
	for(fieldName in addbook)
	{
		if(addbook[fieldName].id)
			xml+= "<field name=\""+fieldName+"\" id=\""+addbook[fieldName].id+"\" display=\""+addbook[fieldName].title+"\">"+addbook[fieldName].value+"</field>";
	}
	xml +="</address>";
//	cp.set_debug(true);
	cp.set_response_type('xml');
	cp.call('ajax/new_address.php', 'validateAddress', sendAddressRequest_result, xml);
	
	//set the souce for the button back to orrional and hide
	hideAddBookSubmit();
	return false;
}

function sendAddressRequest_result(result)
{
	valid = result.getElementsByTagName('valid').item(0).firstChild.data;
	if(valid==1)
	{

	  addbook = getAddressObjects();   // Successfully added to db, so clear new address tab
	  for(fieldName in addbook)
	  {
	  	addbook[fieldName].value='';
	  }

		addressId = result.getElementsByTagName('address_id').item(0).firstChild.data;
//		alert('valid address');
		//update the addressbook tabs, set the tab as active, and switch to that tab
		addressId = document.getElementById('ship_to');
		addressId.value = addressId;
		
		//Add the tab
		addNewTab(result);
	}
	else
	{
//		alert('INVALID address');
		//pass result to the form errors handler	
		addressBookError(result);
	}
	return false;
}

function addNewTab(result)
{
	//get the id
	address = document.getElementById('ship_to');
	address.value = result.getElementsByTagName('address_id').item(0).firstChild.data;
	addressId = address.value;
	
	//get the name to display
	fname = result.getElementsByTagName('first_name');
	lname = result.getElementsByTagName('last_name');
	name = fname.item(0).firstChild.data +" "+ lname.item(0).firstChild.data;
//	alert(name);
	//do we put this in the tabs, or in the overflow container?
	tabRow = document.getElementById('tabs');
	tabEL = tabRow.getElementsByTagName('li');
	
	//tabs are structured to have 4 tabs + the overflow container.
	if(tabEL[4])
	{	//so if we have an element 5 then we know the overflow is active and we should post there.
//		alert(tabs[4]);	
		overflow = document.getElementById('tabOverflowContainer');
		overflow.innerHTML +='<a href="#" id="address_'+addressId+'" title="Click to send flowers to '+name+'">'+name+'</a>';
	}
	else
	{	//if we get here, were not done yet, we need to see if we have 4 tabs already, and if we do we need to create the overflow container
		if(tabEL.length ==4)
		{	//create overflow container
			newTab='<li id="tabOverflow" class="tabOverflow" onMouseOver="showOverflow()" onMouseOut="hideOverflow()"><img class="form_tab_right" src="images/add_form_right.png" style="display:none;" /><a href="#" id="overflowLink" title="Click to see other contacts" onClick ="return false;">More...</a><div id="tabOverflowContainer"><a href="#" id="address_'+addressId+'" title="Click to send flowers to '+name+'">'+name+'</a></div>';
		}
		else
		{	//ok, NOW we can add a normal tab
			newTab='<li class="name_tab"><img class="form_tab_right" src="images/add_form_right.png" style="position:absolute; float:right;top:-1px;right:-1px; display:none;" /><a href="#" id="address_'+addressId+'" title="Click to send flowers to '+name+'">'+name+'</a></li>';			
		}
		tabRow.innerHTML+=newTab;
		
	}
	//now that our tab is added, we can switch to it.
	ajaxAddressBook();
	addBookSwitchTabs('address_'+addressId);
}

function addressBookError(result)
{
	addressBookClearErrors();
	resErrors = result.getElementsByTagName("error");
	for(i=0;i<resErrors.length;i++)
	{
		errorDiv = document.getElementById(resErrors.item(i).getAttribute('field')+"_error");
		errorDiv.style.display="block";
		errorDiv.innerHTML=resErrors.item(i).firstChild.data;
	}
}

function addressBookClearErrors()
{
	addbook = document.getElementById("address_form");
	fields = addbook.getElementsByTagName("div");
	for(i=0;i<fields.length;i++)
	{
		if(fields[i].getAttribute('class')=='error_field')
			fields[i].innerHTML="";
	}
}

function addBookSwitchTabs(forcedId)
{
	useId = (this.id?this.id:forcedId);
	if(!useId)return;
//	alert(useId);
	//show loading
	loading = document.getElementById('address_loading');
	loading.style.display='block';

	cp.set_response_type('xml');
//	cp.set_debug(true);
	cp.call('ajax/new_address.php', 'getAddress', addBookSwitchTabs_result, useId);

	
	return false;	
}

function addBookSwitchTabs_result(result)
{
//	addbook = getAddressObjects();
//	for(fieldName in addbook)
//	{
//		//if we are switching tabs, then we dont have a state (we removed it)
//		if(fieldName !="state")
//		addbook[fieldName].value='';
//	}

	nodes = result.getElementsByTagName('info');
	if(nodes.item(0).childNodes.length>0)
	{
		//update nodes
		fields = nodes.item(0).childNodes;
		for(i=0;i<fields.length;i++)
		{
			val = fields.item(i).firstChild;
			formfield = fields.item(i).nodeName;
			EL = document.getElementById("disp_"+fields.item(i).nodeName);
			if(EL !=null)
				document.getElementById("disp_"+fields.item(i).nodeName).innerHTML=(val != null?val.data:"");
		}	
		
		display = document.getElementById("address_form_display");
//		display.style.zindex=1;
		display.style.display='block';
		hideNewAddressForm();
		
		//hide select
		if(!state_select_menu)
			state_select_menu= 	document.getElementById('state_li').innerHTML;
		document.getElementById('state_li').innerHTML='&nbsp;';
		//update the tabs
		
	}
	addressId = document.getElementById('ship_to');
	addressId.value = result.getElementsByTagName('id').item(0).firstChild.data;
        document.getElementById('edit_link').href = "address_book_edit.php?redir=products&id=" + result.getElementsByTagName('dbid').item(0).firstChild.data;  // Complete link for specific address

//	addNewTab(result);
	
	//swap tabs 
	swapTabStyles(addressId.value);
	
	//hide loading
	loading = document.getElementById('address_loading');
	loading.style.display='none';
}

function hideNewAddressForm()
{
	display = document.getElementById("address_form");
	display.style.zindex=0;
	display.style.display='none';
	
}

function showNewAddressForm()
{
	//clear our errors 
	addressBookClearErrors();

	//unset the ship_to
	document.getElementById('ship_to').value='';
	
	display = document.getElementById("address_form_display");
//	display.style.zindex=1;
	display.style.display='none';
	
	display = document.getElementById("address_form");
//	display.style.zindex=1;
	display.style.display='block';

//show select
	if(state_select_menu)
	document.getElementById('state_li').innerHTML=state_select_menu;
	
	swapTabStyles();
	//show form tab
	tab = document.getElementById('form_tab');
	
	capper = tab.getElementsByTagName('img');
	capper[0].style.display='block';
	
	tab.className='form_tab';
	
//	addbook = getAddressObjects();
//	for(fieldName in addbook)
//	{
//		addbook[fieldName].value='';
//	}
	return false;
}

function swapTabStyles(thisTab)
{
	
	tabnodes = document.getElementById('tabs').getElementsByTagName('li');
	maxLoop= (tabnodes.length>4?4:tabnodes.length);
	for(i=0;i<maxLoop;i++)
	{
		if(tabnodes[i].className !="tabOverflow");
		{
			tabnodes[i].className='name_tab';	
			capper = tabnodes.item(i).getElementsByTagName('img');
			capper[0].style.display='none';
		}
	}
	
	if(thisTab)
	{	
		showtab = document.getElementById('address_'+thisTab).parentNode;
		if(showtab.nodeName =="LI")
		{
			showtab.className='form_tab';

			capper = showtab.getElementsByTagName('img');
			capper.item(0).style.display='block';
		}
	}
	
}

function showOverflow()
{
	overflow=document.getElementById('tabOverflowContainer');
//	overflow.style.zindex=2;
	overflow.style.display='block';
}

function hideOverflow()
{
	document.getElementById('tabOverflowContainer').style.display='none';
}
/*END ADDRESS BOOK FUNCTIONS*/



