//Forms functions: Form components section
function GetSectionAttrs(id_sec, dest, dest1, dest2){
	last=document.getElementById('sec_'+id_section);
	cur=document.getElementById('sec_'+id_sec);
	last.className='inactive_section';
	cur.className='active_section';
	id_section=id_sec;
	var del_button=document.getElementById('del_button');
	
	if(s_const[id_section]!=null){
		del_button.disabled=true;
	}
	else{
		del_button.disabled=false;
	}
	var field1=null, field2=null;
	field1=document.getElementById(dest1);
	if(dest2) field2=document.getElementById(dest2);
	field1.innerHTML='';
	if(field2) field2.innerHTML='';
	
	AjaxQuery("ajax_sections.php?sel=get_section_attrs&id_section=" + id_sec, dest);
}

function DelSecAttibute(id_section, id_attr){
	var params=new Array(4);
	params[0]=id_section;
	params[1]='sec_attrs_div';
	params[2]='cur_section_div';
	params[3]='attr_props_div';
	
	AjaxQuery("ajax_sections.php?sel=del_section_attr&id_attr="+id_attr, null, GetSectionAttrs, params);
}

function LoadAtributeForm(id_sec, id_attr, dest, dest2){
	field2=document.getElementById(dest2);
	if(field2) field2.innerHTML="";
	var act=(id_attr!=null)?'edit':'new';
	AjaxQuery("ajax_sections.php?sel=get_attr_form&act="+act+"&id_sec="+id_sec+"&id_attr="+id_attr, dest);
}

function LoadAttrPropsForm(control_type, id_attr, dest){
	query="ajax_sections.php?sel=get_attr_props_form&control_type="+control_type;
	if(id_attr!=null) query+="&id_attr="+id_attr;
	AjaxQuery(query, dest);
}

//Forms functions: Catalog manager section
function LoadFormManager(id_cat, dest_div, id_section){
	if(id_section==null) id_section=0;
	AjaxQuery("ajax_forms.php?sel=get_manager&id_section=" + id_section+"&id_form="+id_cat, dest_div);
}

function LoadSectionAttrs(id_sec, id_form, form_div, sec_div, dest_div){
	//id_section must be global, uses to keep current section id
	if (id_sec == '-1') {
            document.getElementById('form_div').style.display = 'none';
            document.getElementById('sec_dest').style.display = 'none';
            return;
        }
        document.getElementById('form_div').style.display = 'block';
        document.getElementById('sec_dest').style.display = 'block';
	id_section = document.getElementById('id_section_1').value;
	if (id_sec == null) {
	
		id_sec = id_section;
	}
	id_form = document.getElementById('id_form_1').value;
	//last=document.getElementById('sec_'+id_section);
	//cur=document.getElementById('sec_'+id_sec);
	//last.className='inactive_section';
	//cur.className='active_section';
	id_section=id_sec;

	var after_load=function(form_div, sec_div){
		sections=[form_div, sec_div];
		Sortable.destroy(form_div);
		Sortable.destroy(sec_div);
		Sortable.create(form_div,{tag:'div', onUpdate: updateForm, dropOnEmpty: true, containment: sections, only:'lineitem', constraint:false});
		Sortable.create(sec_div,{tag:'div', dropOnEmpty: true, containment: sections, only:'lineitem', constraint:false});
		
		//Force constant attributes to constraint
		ForceConstraint();
	}
	var params=Array(1);
	params[0]=form_div;
	params[1]=sec_div;
	AjaxQuery("ajax_forms.php?sel=get_section_attrs&id_section=" + id_sec+"&id_form="+id_form, dest_div, after_load, params);
}

//Attribute edit form
function CheckValues(){
	var form=document.attr_props;
	var len=form.control_type.length;
	var c_type='';
	for(var i=0; i<len;i++){
		if(form.control_type[i].checked){
			c_type=form.control_type[i].value;
			break;
		}
	}
	switch(c_type){
		case 'date':
					if(form.min_age.value>form.max_age.value){
						var temp=form.max_age.value;
						form.max_age.value=form.min_age.value;
						form.min_age.value=temp;
					}
					if(form.min_age.value==form.max_age.value){
						alert(err_age_invalid);
						return false;
					}
					break;
	}

	if(form.field_name.value==''){
		alert(err_field_name);
		form.field_name.focus();
		return false;
	}
	return true;
}

//Attribute properties form
function addElement(){
	var form=document.attr_props;
	
	if(form.new_value.value=='') return;
	temp_id++;
	form.values.options[form.values.length]=new Option(form.new_value.value, temp_id);
	form.s_data.value=serializeArr(form.values.options);
}

function delElement(){
	var form=document.attr_props;
	form.values.options[form.values.selectedIndex]=null;
	form.s_data.value=serializeArr(form.values.options);
}

function moveUp(){
	var form=document.attr_props;
	
	var index=form.values.selectedIndex;
	if(index==0) return false;
	
	var options=form.values.options;
	var temp_text=options[index-1].text;
	
	options[index-1].text=options[index].text;
	options[index].text=temp_text;
	options[index-1].selected=true;
	options[index].selected=false;
	form.s_data.value=serializeArr(form.values.options);
}

function moveDown(){
	var form=document.attr_props;
	
	var options=form.values.options;
	var index=form.values.selectedIndex;
	if(index==(options.length-1)) return false;
	var temp_text=options[index+1].text;
	
	options[index+1].text=options[index].text;
	options[index].text=temp_text;
	options[index+1].selected=true;
	options[index].selected=false;
	form.s_data.value=serializeArr(form.values.options);
}

function serializeArr(arr){
	var str='';
	var arr_len=arr.length;
	for(var i=0;i<arr_len;i++){
		str+=arr[i].value+'<!!>'+arr[i].text+'<!;>';
	}
	return str;
}

function DisableListboxSetup(type){
	var form=document.attr_props;
	
	if(type!='listbox'){
		form.vsize.disabled=true;
		form.is_mult.disabled=true;
	}
	else{
		form.vsize.disabled=false;
		form.is_mult.disabled=false;		
	}
}

function DisableListboxPanel(disable){
	var form=document.attr_props;
	
	form.add_btn.disabled=disable;
	form.del_btn.disabled=disable;
	form.mu_btn.disabled=disable;
	form.md_btn.disabled=disable;
	form.new_value.disabled=disable;
}

function GetDsValues(id_ds){
	
	//Block add/delete operations if Datasource is User Types
	if (id_ds==10 || id_ds==11) DisableListboxPanel(true);
		else DisableListboxPanel(false);
	
	var field=document.attr_props;
	var myFunc=function(){
		var form=document.attr_props;
		var arr_len=form.values.options.length;
		form.s_data.value=serializeArr(form.values.options);
		temp_id=0;
		for(var i=0; i<arr_len; i++){
			if(form.values.options[i].value>temp_id) temp_id=form.values.options[i].value;
		}
	}
	AjaxQuery('ajax_sections.php?sel=get_ds_values&id_ds='+id_ds, 'values_div', myFunc, null);
	return true;
}

function LoadPossibleFields(id_attr, ass_type, control_type, dest){
	AjaxQuery('ajax_sections.php?sel=get_ass_fields&id_attr='+id_attr+'&c_type='+control_type+'&ass_type='+ass_type, dest);
}

//Location functions
function AdoSelectRegion(id_attr, section, id_country, destination, destination2) {
	if (id_country != 0) {
		var query=site_path+"admin/ajax_loc.php?sec=" + section + "&sel=region&id_country=" + id_country+"&id_attr="+id_attr;
		AjaxQuery(query, destination);
	} else {
		document.getElementById(destination).innerHTML = "Country is not selected";
	}
	document.getElementById(destination2).innerHTML = "";
}

function AdoSelectCity(id_attr, section, id_region, destination) {
	if (id_region != 0) {
		var query=site_path+"admin/ajax_loc.php?sec=" + section + "&sel=city&id_region=" + id_region+"&id_attr="+id_attr;
		AjaxQuery(query, destination);
	} else {
		document.getElementById(destination).innerHTML = "Region is not selected";
	}
}

function ForceConstraint(){
	var object_arr=null;
	eval('object_arr=Sortable.sortables.form_div.draggables');
	var arr_len=object_arr.length;
	
	for(var i=0; i<arr_len;i++){
		var item_id=object_arr[i].element.id;
		if((/const_/).test(item_id)){
			eval('Sortable.sortables.form_div.draggables['+i+'].options.constraint=true;');
		}
	}
}

function updateForm(){
	var form_div=document.getElementById('form_div');
	var sec_div=document.getElementById('section_div');
    var id_form = document.getElementById('id_form_1').value;
	SwapAreaHeight(form_div);
	SwapAreaHeight(sec_div);

	var attrs_str=getSerArr('form_div');
	AjaxQuery('ajax_forms.php?sel=update_form&id_form='+id_form+'&attrs_str='+attrs_str);
}

function SwapAreaHeight(area_el){
	attr=area_el.getAttribute('style');
	if(area_el.innerHTML.length==0) {
		if(agent=='IE') {
			attr.height='25px';
		}
		area_el.setAttribute('style', 'position: relative; height: 25px;');
	}
	else area_el.setAttribute('style', 'position: relative;');
}

function getSerArr(src_id) {
		var section = document.getElementById(src_id);
		var ser_arr= '';

		ser_arr += Sortable.sequence(section) + '\n';
		return ser_arr;
}