allcols=new Array();
allrows=new Array();
colsrows=new Array();

function obj_size(inobj){
	var res=0;
	for(var i in inobj) res++;
	return(res);
}

function get_selected_col(inarr, obj_id){
	var res=0;
	for(var i in inarr){
		if(obj_size(inarr)==1){
			res=i;
		}else{
			el=document.getElementById(obj_id + i);
			if(el.className.substr(el.className.length-2,2)=="ed") res=i;
		}
	}
	return(res);
}

function set_rows_displ(inarr, rows_to_displ, obj_id){
	for(var i in inarr){
		var el=document.getElementById(obj_id + i);
		if(el){
			var ex=false;
			for(var n=0; n<rows_to_displ.length; n++) if(rows_to_displ[n]==inarr[i]) ex=true;
			if(ex){
				if(el.style.display=="none") el.style.display="block";
			}else{
				if(el.style.display!="none") el.style.display="none";
			}
		}
	}
}

function set_cols(col_id, pd_col_id){
	var res=new Array();
	var res_main=new Array();
	if(!col_id) col_id=get_selected_col(allcols[iid],"col_selector_");
	for(var i in allrows[iid]){
		if(col_id){
			if(colsrows[iid][i][col_id]) res_main[res_main.length]=allrows[iid][i];
		}else{
			res_main[res_main.length]=allrows[iid][i];
		}
	}
	if(pd_id){
		if(pd_col_id==0) pd_col_id=get_selected_col(allcols[pd_id],"col_pd_selector_");
		if(pd_col_id>0){
			var res_pd=new Array();
			for(var i in allrows[pd_id]){
				if(colsrows[pd_id][i][pd_col_id]) res_pd[res_pd.length]=allrows[pd_id][i];
			}
			for(var n=0; n<res_main.length; n++){
				ex=false;
				for(var nn=0; nn<res_pd.length; nn++){
					if(res_pd[nn]==res_main[n]){
						res[res.length]=res_main[n];
					}
				}
			}
		}else{
			res=res_main;
		}
	}else{
		res=res_main;
	}
	set_rows_displ(allrows[iid],res,"row_selector_");
}

function out_cost(par){
/*	par=Math.floor(par)+Math.round((par-Math.floor(par))*100)/100;
//	par=par.toFixed(2);
	t=Math.round(100*(par-Math.floor(par)));
	if(t==0) return(par+'.00');
	if((t%10)==0) return(par+'0');*/
	oc_res=par.toFixed(2)
	if(par>=1000){
		r1=oc_res.substring(oc_res.length-6);
		r2=oc_res.substring(0,oc_res.length-6);
		oc_res=r2+','+r1;
	}
	return(oc_res);
}

function createXmlRequestObject()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();

	}
	catch(e)
	{
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
										'MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP');
		for( var i=0; i < XmlHttpVersions.length && !xmlHttp; i ++ )
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	if ( !xmlHttp )
		alert('Error creation XMLHttpRequest');
	else
		return xmlHttp;
}

function cart_redraw(){
	$.get("/static/ajax/cart_redraw.php", function(data){
		res=data.split('|');
		$("#cart_title_closed").html(res[0]);
		$("#cart_title_opened").html(res[1]);
		$("#cart_contents_opened").html(res[2]);
	});
}

function EnsureNumeric()
{
     var k = window.event.keyCode;
     if (( k < 48 || k > 57) && k != 8 && k != 13 && k != 44) {window.event.returnValue = false; }
}

function CheckPhoneNumber(ob){
     f=ob.form;
     n=ob.name;
     v=ob.value;
     nextfield=0;
     currfield=parseInt(n.substr(n.length-1,1));
     fn=n.substr(0,n.length-1);
     if(currfield<3) nextfield=currfield+1;
     if(nextfield){
          if(v.length>=3){
               f[fn+nextfield].focus();
          }
     }
}

function set_field_state(val, dst){
	c=dst.options.length-1;
	currsel=null;
	for(i=c;i>0;i--){
		if(dst.options[i].selected) currsel=dst.options[i].value;
		dst.remove(i);
	}
	toset=new Array();
	if(val=='USA'){
		for(i in states_ALL){
			if(!states_CA[i]){
				 toset[i]=states_ALL[i];
			}
		}
	}
	if(val=='CA'){
		for(i in states_CA){
			toset[i]=states_ALL[i];
		}
	}
	n=1;
	for(i in toset){
		ds=false;
		if(i==currsel) ds=true;
		no = new Option(toset[i], i, false, ds);
		dst.options[n]=no;
		n++;
	}
}

function cart_delete(cid,i){
	ajaxhide('#cartp'+i);
	$.get("/static/ajax/cart_redraw.php?cart_action=remove&cid="+cid, function(data){
		res=data.split('|');
		$("#cart_title_closed").html(res[0]);
		$("#cart_title_opened").html(res[1]);
		$("#cart_contents_opened").html(res[2]);
		});
}
function cart_freeitem_delete(fid,i){
	ajaxhide('#cartp'+i);
	$.get("/static/ajax/cart_redraw.php?cart_action=remove_free&fid="+fid, function(data){
		res=data.split('|');
		$("#cart_title_closed").html(res[0]);
		$("#cart_title_opened").html(res[1]);
		$("#cart_contents_opened").html(res[2]);
		});
}

function swim_popup(site_id,addr,title){
	if(site_id==0){
		tb_show(title, addr);
	}else{
		$.prettyPhoto.open(addr+"&iframe=true",title,"");
	}
}

function swim_alert(message){
	alert(message);
	$.ajax({URL: '/static/ajax/post_error.php', data: {'message':message, 'ucart_page_name':ucart_page_name}});
}
