// JavaScript Document
var xmlDoc = false;
var comparison_items = 0;

function trim(x){
	trimmed = x.replace('/^\s+|\s+$/g', '');
	return trimmed;
}

function q_nav(){
	nav_itemno = document.getElementById('quick_nav').value;
	window.location = "http://dev.crownprod.com/?p=viewitem&itemno="+nav_itemno;
}

function echeck(str) {
	recipients = str.split(";");
	for(var x=0; x < recipients.length; x++){
		var at="@";
		var dot=".";
		var lat=recipients[x].indexOf(at)
		var lstr=recipients[x].length
		var ldot=recipients[x].indexOf(dot)
		if (recipients[x].indexOf(at)==-1){
		   return false
		}
	
		if (recipients[x].indexOf(at)==-1 || recipients[x].indexOf(at)==0 || recipients[x].indexOf(at)==lstr){
		   return false
		}
	
		if (recipients[x].indexOf(dot)==-1 || recipients[x].indexOf(dot)==0 || recipients[x].indexOf(dot)==lstr){
			return false
		}
	
		 if (recipients[x].indexOf(at,(lat+1))!=-1){
			return false
		 }
	
		 if (recipients[x].substring(lat-1,lat)==dot || recipients[x].substring(lat+1,lat+2)==dot){
			return false
		 }
	
		 if (recipients[x].indexOf(dot,(lat+2))==-1){
			return false
		 }
		
		 if (recipients[x].indexOf(" ")!=-1){
			return false
		 }	 
	 
	}	 
	return true					
}

function toggle_menu_tabs(id){
	_menu = document.getElementById('main_menu_tabs');
	_menu_content = document.getElementById('banner_menu_wrapper');
	_tabs = _menu.getElementsByTagName('li');
	for(i=0; i < _tabs.length;i++){
		_tabs[i].className = 'MainMenuTab';
		document.getElementById('menu_content_'+i).style.display='none';
		if(_tabs[i].id == 'menu_tab_'+id){
			document.getElementById('menu_content_'+i).style.display='inline';
		}
	}	
	document.getElementById('menu_tab_'+id).className = 'MainMenuTab banner_'+id+' white_16 bold';
	document.getElementById('footer').className = 'banner_'+id;
	_menu_content.className = 'banner_'+id;
	
}


function toggle_sfilters(){
	filters = document.getElementById('sfilters');
	filters_top = document.getElementById('sfilters_top');
	filters_ctr = document.getElementById('sfilters_ctr');
	filters_btm = document.getElementById('sfilters_btm');
	if(filters.style.display=='block'){
		filters.style.display = 'none';
		filters_top.style.backgroundImage = '';
		filters_ctr.style.backgroundImage = '';
		filters_btm.style.backgroundImage = '';
		filters_btm.style.height='0px';
	} else {
		filters.style.display='block';
		filters_top.style.backgroundImage = 'url(images/layout/search_tag_cloud_top.gif)';
		filters_ctr.style.backgroundImage = 'url(images/layout/search_tag_cloud_ctr.gif)';
		filters_btm.style.backgroundImage = 'url(images/layout/search_tag_cloud_btm.gif)';
		filters_btm.style.height='28px';
	}	
}

function filter_search(){
	document.getElementById('search_ajax_target').innerHTML = '<center><div id="search_status"><img src="'+site_url+'/images/ajax/loading_bar.gif" style="margin-top:20px;"><br><span style="color:#666;">Filtering search results. Please wait.</span></div></center>';
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		xmlDoc.onreadystatechange = processsearch;
	}
	else {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onload = processsearch;
	}
	
	if(!xmlDoc){
		  document.search_filter_frm.submit();
		  return false;
	}
	
	sf_search = document.getElementById('sfilter_search');
	sf_type = document.getElementById('sfilter_type');
	sf_cat = document.getElementById('sfilter_cat');
	sf_special = document.getElementById('sfilter_special');
	sf_color = document.getElementById('sfilter_color');
	sf_count = document.getElementById('sfilter_count');
	sf_sort = document.getElementById('sfilter_sort');
	sf_minprice = document.getElementById('sfilter_minprice');
	sf_maxprice = document.getElementById('sfilter_maxprice');
	
	
	stamp = new Date();
	stamp = stamp.getTime();
	request_url = site_url+'/includes/search.php?ajax=Y&type='+sf_type.value+'&count='+sf_count.value+'&sort='+sf_sort.value+'&cat='+sf_cat.value+'&special='+sf_special.value+'&color='+sf_color.value+'&minprice='+sf_minprice.value+'&maxprice='+sf_maxprice.value+'&stamp='+stamp+'&search='+sf_search.value;
	//alert(request_url);
	xmlDoc.open( "GET", request_url, true );
	xmlDoc.send( null );	
}	

function processsearch(){
	if ( xmlDoc.readyState != 4 ) return ;	
	document.getElementById('search_ajax_target').innerHTML = xmlDoc.responseText;	
	js_redirect = document.getElementById('javascript_redirect');
	if(js_redirect.value != ''){
		window.location = js_redirect.value;
	}
}

function search_pages(){
	sf_count = document.getElementById('sfilter_count');
	sf_count_select = document.getElementById('num_results_select');
	sf_count.value= sf_count_select.value;
	filter_search();
}

function search_sortby(){
	sf_sort = document.getElementById('sfilter_sort');
	sf_sort_select = document.getElementById('sort_by');
	sf_sort.value= sf_sort_select.value;
	filter_search();
}

function toggle_compare(comp_itemno){
	icon = document.getElementById('compare_'+comp_itemno);	
	chkbox = document.getElementById('compare_'+comp_itemno+'_chkbox');	
	if(!chkbox.checked){
		icon.src = site_url+'/images/layout/compare_on.jpg';
		chkbox.checked = true;
		comparison_items++;
	}
	else{
		icon.src = site_url+'/images/layout/compare_off.jpg';
		chkbox.checked = false;
		comparison_items--;
	}	
}

function submit_comparison(){	
	if(comparison_items > 1){
		document.comparison_form.submit();
		return true;
	}
	else{
		alert("You must mark at least 2 products for a product comparison.");
		return false;
	}
}

function ajax_loadorder(ajax_ordno){
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		xmlDoc.onreadystatechange = process_loadorder;
	}
	else {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onload = process_loadorder;
	}	
	
	if(!xmlDoc){ //redirect if AJAX is not supported
		  window.location = site_url+'?p=vieworder&ordno='+ajax_ordno
		  return false;
	}
	else{
		document.getElementById('ajax_loadorder_target').innerHTML = '<center><div id="search_status"><img src="'+site_url+'/images/ajax/loading_green_bar.gif" style="margin-top:20px;"><br><span style="color:#289728;">Loading order information. Please wait.</span></div></center>';
		stamp = new Date();
		stamp = stamp.getTime();
		request_url = site_url+'/includes/vieworder.php?ajax=Y&ordno='+ajax_ordno+'&stamp='+stamp;
		//alert(request_url);
		xmlDoc.open( "GET", request_url, true );
		xmlDoc.send( null );		
	}
}

function process_loadorder(){
	if ( xmlDoc.readyState != 4 ) return ;	
	document.getElementById('ajax_loadorder_target').innerHTML = xmlDoc.responseText;		
}
function ajax_orderstatus(ordno){
	orderstatus = document.getElementById('ajax_orderstatus_target');
	orderstatus.innerHTML = '<span style="color:#0076CC; font-weight:bold;"><img src="'+site_url+'/images/ajax/blue_circle_sm.gif">&nbsp;Retrieving current order status. Please wait.</span>';
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		xmlDoc.onreadystatechange = process_orderstatus;
	}
	else {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onload = process_orderstatus;
	}	
	
	if(!xmlDoc){ //redirect if AJAX is not supported
		  orderstatus.innerHTML = '<span class="red_10 bold">Your browser does not support AJAX. The current status of this order could not be retrieved.</span>';
		  return false;
	}
	else{		
		stamp = new Date();
		stamp = stamp.getTime();
		request_url = site_url+'/ajax/orderstatus.php?ordno='+ordno+'&stamp='+stamp;
		//alert(request_url);
		xmlDoc.open( "GET", request_url, true );
		xmlDoc.send( null );		
	}
}

function process_orderstatus(){
	if ( xmlDoc.readyState != 4 ) return;	
	document.getElementById('ajax_orderstatus_target').innerHTML = xmlDoc.responseText;
}

function session_load_items(){	
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		xmlDoc = new XMLHttpRequest();
	}
	stamp = new Date();
	stamp = stamp.getTime();
	request_url = site_url+'/includes/functions/session_load_items.php?stamp='+stamp;
	xmlDoc.open( "GET", request_url, true );
	xmlDoc.send(null);
}

function popup_contact(itemno){
	popup = window.open(site_url+'includes/contact.php?id='+site_id+'&window=Y&itemno='+itemno,"contact_window","status=0,toolbar=0,resizeable=0,width=375,height=325"); 
	if(popup == null){
		alert('The contact form was blocked by a pop-up blocker in your browser. Please disable your pop-up blocker and try again.');	
	}
}
