/*
* get info about fields for specific layout //admin/add_list.php
*/
var maxwidth = new Array();
function getref(key, value, _L_FieldID, synasyn, selected){
	
		action ='getRef';
		refnext='';
		xmlOptions = '?action='+action;
		if(key!='')   		xmlOptions += '&key='+key;
		if(value!='') 		xmlOptions += '&value='+value;
		if(_L_FieldID!='')  xmlOptions += '&_L_FieldID='+_L_FieldID;
		
		xmlhttp.open("GET", url+xmlOptions, synasyn, null,null);
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				//alert(xmlhttp.responseText);
				layout   = xmlhttp.responseXML.documentElement;
				layoutid = layout.getAttributeNode('id').value;
				refkeys = layout.getAttributeNode('refkeys').value;
				clearOtherReferences(refkeys);
				key	= layout.getAttributeNode('key').value;
				clearSelect(layoutid);
				addOption(layoutid, '', 'choose one:', 0);
				for(i=0; i < layout.childNodes.length; i++){
					name = layout.getElementsByTagName('name')[i].firstChild.data;
					id = layout.getElementsByTagName('id')[i].firstChild.data;
					addOption(layoutid, id, name, i+1, selected); // add new option to select
				}
			}
		}
		xmlhttp.send(null);
}

/*
* get info about fields for specific layout //admin/add_list.php
*/

function getXMLData(layout, _R_Id, _R_Name, thisID, referenceSelectID, action, selected){ 
	xmlOptions = '?ID='+thisID;
	if(_R_Id!='')   xmlOptions += '&_R_Id='+_R_Id;
	if(_R_Name!='') xmlOptions += '&_R_Name='+_R_Name;
	if(layout!='')  xmlOptions += '&layout='+layout;
	if(action!='')  xmlOptions += '&action='+action;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
   		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ // check status
			//alert(xmlhttp.responseText);
			clearSelect(referenceSelectID); // clear select
			addOption(referenceSelectID, '', 'choose one:', 0);
			record   = xmlhttp.responseXML.documentElement;
			for(i=0; i < record.childNodes.length; i++){
				name = record.getElementsByTagName('name')[i].firstChild.data;
				if(action=='') id = record.getElementsByTagName('id')[i].firstChild.data;
				else 		 id = name;
				addOption(referenceSelectID, id, name, i+1, selected); //add new option to select
			}
		}
 	}
    xmlhttp.send(null);
}

function delete_table(param,id1,id2){
	text = "Are you sure you want to remove selected file ?";
	if(confirm(text)){
		xmlhttp.open("GET", url+'/?'+param);
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ // check status
				//alert(xmlhttp.responseText);
				document.getElementById(id1).innerHTML='';
				document.getElementById(id2).innerHTML='';
			}
		}
		xmlhttp.send(null);
	}
}

/* change access show_access.php */
function changeAccess(id, key, _R_PageID,_R_GroupID,flag){
	var value=0;
	if(document.getElementById(id).checked) value=1;
	if(flag==1)       action = 'action=changeTaskAccess';
	else if(flag==2)  action = 'action=changeGrantAccess';
	else              action = 'action=changeAccess';
	xmlOptions = '?_R_GroupID='+_R_GroupID+'&_R_PageID='+_R_PageID+'&value='+value+'&key='+key+'&'+action;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
		//alert(xmlhttp.responseText);
   		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){}
 	}
    xmlhttp.send(null);
}

function genericChangeAccess(id,links,value3, divID){
	
	var value=value3+"=0";
	
	action = 'action=genericChangeAccess';
	if(document.getElementById(id).checked) value=value3+"=1";
	xmlOptions = '?'+links+'&'+value+'&'+action;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
   		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			
		}
 	}
    xmlhttp.send(null);
}

function changePublicAccess(links, flag, id){
	if(flag=='rem'){
		newflag = 'add';
		textold = 'private';
		text = 'public';
	}
	else{ 
		newflag = 'rem';
		text = 'private';
		textold = 'public';
	}
	if(window.confirm("Are you sure you want to change "+text+" access on "+textold+" access ?")){
		action = 'action=changePublicAccess';
		xmlOptions = '?'+links+'&'+action+'&flag='+flag;
		xmlhttp.open("GET", url+xmlOptions);
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				id.parentNode.innerHTML = '<a href="javascript:void(0)" onClick=\'changePublicAccess("'+links+'","'+newflag+'",this)\'>set&nbsp;'+text+'</a>&nbsp;';
			}
		}
		xmlhttp.send(null);
	}
}

function checkAllHorizontal(id,links,value3, divID){

	var value="value="+value3;
	accesscheck = new Array('EAccessView','EAccessInsert','EAccessUpdate','EAccessDelete','DAccessView','DAccessInsert','DAccessUpdate','DAccessDelete','SAccessView','SAccessInsert','SAccessUpdate','SAccessDelete');
	action = 'action=checkAllHorizontal';
	if(value3==1){ 
		newvalue = 0; 
		check=true;
	}
	else{ 
		newvalue = 1; 
		check = false;
	}
	
	for(i=0; i < accesscheck.length; i++){
		document.getElementById(accesscheck[i]+"_"+id).checked  = check;
	}
	document.getElementById(divID).innerHTML='<a href="javascript:void(0)" onClick=checkAllHorizontal("'+id+'","'+links+'","'+newvalue+'","'+divID+'")>all</a>&raquo;';

	xmlOptions = '?'+links+'&'+value+'&'+action;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
   		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			//alert(xmlhttp.responseText);
		}
 	}
    xmlhttp.send(null);
	
}

function checkAllVertical(no, id, value, divID){
	if(value) newvalue=0;
	else newvalue=1;
	document.getElementById(divID).innerHTML='<a href="javascript:void(0)" onClick=checkAllVertical("'+no+'","'+id+'","'+newvalue+'","'+divID+'")>all</a>&raquo;';
	for(i=0; i < no; i++){
		document.getElementById(id+"_"+i).checked  = 1;
	}
	
}



function clearOtherReferences(refkeys){
	if(refkeys!=''){
		ids = refkeys.split(" ");
		for(i=0; i<ids.length-1; i++){
			clearSelect(ids[i]);
			addOption(ids[i], '', 'choose one:', 0);
		}
	}
}


function checkvalue(element, _L_PageID, _L_LayoutID){
	if(element.value=='select'){
		links = "t=6&sw=1&_L_PageID="+_L_PageID+"&_L_LayoutID="+_L_LayoutID;
		open_window('./?'+links,400,400);
	}
}

function clearSelect(selectID){
	while(document.getElementById(selectID).length){
		document.getElementById(selectID).remove(0);
	}
}

function addOption(selectID, optionValue, optionName, index, selected){
	document.getElementById(selectID).options[index] = new Option(optionName);
	document.getElementById(selectID).options[index].value = optionValue;
	if(selected == optionValue) document.getElementById(selectID).options[index].selected = true;

}

function zoomIN(id, sizesID, param, image, currwidth, currheight){
	tab = new Array();
	
	width = document.getElementById(id).width +=15;
	tab = getResize(currwidth, currheight, width);
	xmlhttp.open("GET", url+'/?'+param+"&width="+width);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ // check status
			document.getElementById(sizesID).innerHTML="<FONT SIZE=1>"+tab[0]+" x "+tab[1]+" pixels</FONT>";
			document.getElementById(id).src = "create_thumb.php/?i="+image+'&max='+width;
		}
	}
	xmlhttp.send(null);
}

function zoomOUT(id, sizesID, param, image, currwidth, currheight){
	tab = new Array();
	width = document.getElementById(id).width -=15;
	tab = getResize(currwidth, currheight, width);
	xmlhttp.open("GET", url+'/?'+param+"&width="+width);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ // check status
			document.getElementById(sizesID).innerHTML="<FONT SIZE=1>"+tab[0]+" x "+tab[1]+" pixels</FONT>";
			document.getElementById(id).src = "create_thumb.php/?i="+image+'&max='+width;
		}
	}
	xmlhttp.send(null);
}

function getResize(currwidth, currheight, maxpixelsize){
	var newwidth=0;
	var newheight=0;
	if(currwidth > currheight && currwidth > maxpixelsize){
		newwidth = maxpixelsize;
		newheight = parseInt((maxpixelsize / currwidth) * currheight);
	} 
	else if(currwidth < currheight && currheight > maxpixelsize){
		newheight = maxpixelsize;
		newwidth = parseInt((maxpixelsize / currheight) * currwidth);
	} 
	else if(currwidth == currheight && currwidth > maxpixelsize){
		newwidth  = maxpixelsize;
		newheight = maxpixelsize;
	}
	
	tab = new Array();
	tab[0] = newwidth;
	tab[1] = newheight;
	return tab;
}


function addGroup(_R_TaskID, _R_GroupID, divID){
	var temp = new Array();
	temp = _R_GroupID.split('sw=1');
	
	if(temp.length > 1){
		open_window('./?'+_R_GroupID,300,100);
	}
	else{
		if(_R_GroupID!=''){	
		
			action = 'action=addGroupToTask';
			xmlOptions = '?_R_GroupID='+_R_GroupID+'&_R_TaskID='+_R_TaskID+'&'+action+'&divID='+divID+'&next_flag=1';
			xmlhttp.open("GET", url+xmlOptions);
			xmlhttp.onreadystatechange = function() {
   				if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
					//alert(xmlhttp.responseText);
					document.getElementById(divID).innerHTML=xmlhttp.responseText;
				}
 			}
   			xmlhttp.send(null);
		}
	}
}

function deleteGroup(_R_TaskID, _R_GroupID, divID){
	action = 'action=deleteGroup';
	xmlOptions = '?_R_GroupID='+_R_GroupID+'&_R_TaskID='+_R_TaskID+'&'+action+'&divID='+divID+'&next_flag=1';
			xmlhttp.open("GET", url+xmlOptions);
			xmlhttp.onreadystatechange = function() {
   				if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
					//alert(xmlhttp.responseText);
					document.getElementById(divID).innerHTML=xmlhttp.responseText;
				}
 			}
   			xmlhttp.send(null);
}

function open_close(flag, next_flag, divID, _R_TaskID){
	action = 'action=open_close';
	xmlOptions = '?_R_TaskID='+_R_TaskID+'&flag='+flag+'&next_flag='+next_flag+'&'+action+'&divID='+divID;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
   	if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
		//alert(xmlhttp.responseText);
		document.getElementById(divID).innerHTML=xmlhttp.responseText;
	}
 }
   xmlhttp.send(null);
}

function getReferenceValues(divID, FieldName, Table, value){
	
	action = 'action=getReferenceValues';
	xmlOptions = '?FieldName='+FieldName+'&Table='+Table+'&'+action+'&divID='+divID+'&value='+value;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			//alert(xmlhttp.responseText);
			if(xmlhttp.responseText!='') document.getElementById(divID).innerHTML=xmlhttp.responseText;
		}
	}
    xmlhttp.send(null);

}

function setReferenceValues(divID, FieldName, Table, value){
	
	action = 'action=setReferenceValues';
	xmlOptions = '?FieldName='+FieldName+'&Table='+Table+'&'+action+'&divID='+divID+'&value='+value;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			//alert(xmlhttp.responseText);
			if(xmlhttp.responseText!='') document.getElementById(divID).innerHTML=xmlhttp.responseText;
		}
	}
    xmlhttp.send(null);

}

function setInterestIn(links, divID){
	
	if(divID.checked) value="value=1";
	else value="value=0";
	action = 'action=setInterestIn';
	xmlOptions = '?'+links+'&'+action+'&'+value;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			//alert(xmlhttp.responseText);
			//if(xmlhttp.responseText!='') document.getElementById(divID).innerHTML=xmlhttp.responseText;
		}
	}
    xmlhttp.send(null);

}
/*
	divID, actions
*/
function switch_window(divID, links){
	action = 'xmlAction=switch_window';
	xmlOptions = '?'+links+'&'+action+'&divID='+divID;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var text = xmlhttp.responseText;
			if(text) document.getElementById(divID).innerHTML=text;
		}
	}
    xmlhttp.send(null);
}

function callXMLProces(links,action,element){
	
	if(element){
		if(element.checked) links +="&value=1";
		else links +="&value=0";
	}

	action ="action="+action;
	xmlOptions = '?'+links+'&'+action;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			//alert(xmlhttp.responseText);
			//if(text) document.getElementById(divID).innerHTML=xmlhttp.responseText;
		}
	}
    xmlhttp.send(null);
}


function disconnected(str,group,staff){
	if(confirm("Are you sure to disconnect ?")){
		action = 'action=disconnected';
		xmlOptions = '?'+str+'&'+action;
		xmlhttp.open("GET", url+xmlOptions);
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				//alert(xmlhttp.responseText);
				location.reload();
			}
		}
		xmlhttp.send(null);
	}
}
function connected(str){
	if(confirm("Are you sure to connect ?")){
		action = 'action=connected';
		xmlOptions = '?'+str+'&'+action;
		xmlhttp.open("GET", url+xmlOptions);
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				//alert(xmlhttp.responseText);
				location.reload();
			}
		}
		xmlhttp.send(null);
	}
}

function changeColor(fieldName,color, id){
	realcolor=color.split('#');
	str="fieldName="+fieldName+"&color="+realcolor[1]+"&id="+id;
	action = 'action=changeColor';
	xmlOptions = '?'+str+'&'+action;
	xmlhttp.open("GET", url+xmlOptions);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			//alert(xmlhttp.responseText);
			//document.getElementById(divID).innerHTML='Email has been sent. Thank You.';
		}
	}
	xmlhttp.send(null);
	
}