// Супир пупир комментарий

var main_div 		= null;
var aDimensions 	= new Array();
var bGobalDebugMode = 1;

function ShadowCLS(){
	this.fOpacity 			= 0.8;
	this.fCurrentOpacity 	= 0;
	this.Style 				= null;
	this.bProcessMode		= 0;
	this.iClickCounter		= 0;
	this.move_shadow = function(){
		if(document.getElementById("shadow_div") && document.getElementById("shadow_div").style.display == "block"){
			var oShadowStyle = document.getElementById("shadow_div").style;
			oShadowStyle.left = document.documentElement.scrollLeft + 'px';
		}
	}
	this.getPageSize = function(){
	    var xScroll, yScroll;
	    if (window.innerHeight && window.scrollMaxY) {
	        xScroll = window.innerWidth + window.scrollMaxX;
	        yScroll = window.innerHeight + window.scrollMaxY;
	    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
	        xScroll = document.body.scrollWidth;
	        yScroll = document.body.scrollHeight;
	    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	        xScroll = document.body.offsetWidth;
	        yScroll = document.body.offsetHeight;
	    }
	    var windowWidth, windowHeight;
	    if (self.innerHeight) { // all except Explorer
	        if(document.documentElement.clientWidth){
	            windowWidth = document.documentElement.clientWidth;
	        } else {
	            windowWidth = self.innerWidth;
	        }
	        windowHeight = self.innerHeight;
	    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	        windowWidth = document.documentElement.clientWidth;
	        windowHeight = document.documentElement.clientHeight;
	    } else if (document.body) { // other Explorers
	        windowWidth = document.body.clientWidth;
	        windowHeight = document.body.clientHeight;
	    }
	    // for small pages with total height less then height of the viewport
	    if(yScroll < windowHeight){
	        pageHeight = windowHeight;
	    } else {
	        pageHeight = yScroll;
	    }
	    // for small pages with total width less then width of the viewport
	    if(xScroll < windowWidth){
	        pageWidth = xScroll;
	    } else {
	        pageWidth = windowWidth;
	    }
	    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	    return arrayPageSize;
	}
	this.openShadowLayer = function (){
        if(this.Style == null){
        	if(document.getElementById("shadow_div")){
				this.Style 	= document.getElementById("shadow_div").style;
				if(document.getElementById("shadow_div").filters){
					this.fOpDisplayStap = this.fOpacity;
					this.fOpHideStap	= this.fOpacity;
				}else{
					this.fOpDisplayStap = this.fOpacity/10;
					this.fOpHideStap	= this.fOpacity/5;
				}
			}
		}
		if(this.Style){
			this.bProcessMode 		= 1;
			var aScreenDimensions 	= this.getPageSize();
			this.Style.left 		= document.documentElement.scrollLeft + 'px';
			this.Style.backgroundColor 	= "#000000";
		 	if(document.getElementById("shadow_div").filters){
		 		document.getElementById("shadow_div").filters.alpha.opacity = 0;
		 	}else{
		 		this.Style.opacity		= 0;
			}
			this.Style.display = "block";
	        if(aScreenDimensions[1] > 500){
	        	this.Style.height 	= aScreenDimensions[1] + 'px';
	        }else{
	        	this.Style.height 	= '500px';
	        }
	        changeShadowOpacity();
		}
	}
	this.closeShadowLayer = function(){
		if(this.Style){
			this.bProcessMode = 0;
			changeShadowOpacity();
		}
	}
	this.click = function(){
		if(this.iClickCounter >= 5 || main_div == null){
			this.iClickCounter = 0;
			this.closeShadowLayer();
		}else{
			this.iClickCounter++;
		}
	}
}

function changeShadowOpacity(){
	if(oShadow.bProcessMode){
		if(oShadow.fOpacity > oShadow.fCurrentOpacity){
			oShadow.fCurrentOpacity 	+= oShadow.fOpDisplayStap;
			setTimeout("changeShadowOpacity(1);",10);
		}else{
			oShadow.fCurrentOpacity		= oShadow.fOpacity;
		}
	}else{
		if(oShadow.fCurrentOpacity > 0){
			oShadow.fCurrentOpacity 	-= oShadow.fOpHideStap;
			setTimeout("changeShadowOpacity(0);",10);
		}else{
			oShadow.fCurrentOpacity	= 0;
			oShadow.Style.display 	= 'none';
		}
	}
 	if(document.getElementById("shadow_div").filters){
 		document.getElementById("shadow_div").filters.alpha.opacity = oShadow.fCurrentOpacity*100;
 	}else{
		oShadow.Style.opacity 		= oShadow.fCurrentOpacity;
	}
	//oShadowStyle.MozOpacity 		= fCurrentOpacity;
	//oShadowStyle.KhtmlOpacity 	= fCurrentOpacity;
}

function closeGrayDiv(){
	oShadow.closeShadowLayer();
}

function closePebuzzLayers(sMainLayer,sInternalLayer){
	//closeGrayDiv();
	if(main_div = document.getElementById(sMainLayer)){
		main_div.style.top	= 0;
		main_div.style.left	= 0;
		main_div.style.display="none";
		if(msg_div = document.getElementById(sInternalLayer)){
			msg_div.style.display="none";
		}//else{
			//alert('There is no "' + sInternalLayer + '" object in this document.');
		//}
	}//else{
		//alert('There is no "' + sMainLayer + '" object in this document.');
	//}
	main_div = null;
}

function moveDiv2center(oDiv){
	oShadow.openShadowLayer();
	var aScreenDimensions = oShadow.getPageSize();
	oDiv.style.top 	= Math.round(document.documentElement.scrollTop + aScreenDimensions[3]/2 - (oDiv.style.height.replace('px','')/2)) + "px";
	oDiv.style.left = Math.round(document.documentElement.scrollLeft + aScreenDimensions[2]/2 - (oDiv.style.width.replace('px','')/2)) + "px";
}

function moveDiv2centerNew(oDiv){
	oShadow.openShadowLayer();
	var oContentDiv = document.getElementById('content_div');
	var aScreenDimensions = oShadow.getPageSize();
	oDiv.style.top	 = Math.round(document.documentElement.scrollTop + aScreenDimensions[3]/2 - (oContentDiv.style.height.replace('px','')/2)) + "px";
	oDiv.style.left = Math.round(document.documentElement.scrollLeft + aScreenDimensions[2]/2 - (oContentDiv.style.width.replace('px','')/2)) + "px";
}

function sendRequest(sAction,aParameters){
	showAjaxLoader();
	var oDataTransfer = new DataTransferCLS();
	oDataTransfer.AddData('action',sAction);
	//setTimeout("showSFcontent();",2000);
	if(aParameters){
		for(key in aParameters){
			if(typeof(aParameters[key]) == 'object'){
				oDataTransfer.AddInput(aParameters[key]);
			}else{
				oDataTransfer.AddData(key,aParameters[key]);
			}
		}
	}
	oDataTransfer.SendData(location.href);
}

function showAjaxLoader(){
	main_div = document.getElementById("al_main_div");
	msg_div = document.getElementById("al_area_div");
	moveDiv2center(main_div);
	main_div.style.display="block";
	msg_div.style.display="block";
}

function closeAjaxLoader(){
	closePebuzzLayers("al_main_div","al_area_div");
}

function DataTransferCLS(){
		this.names		 = new Array();
		this.values	 = new Array();
		this.cnt		 = 0;
		this.inputs	 = new Array();
		this.iCnt		 = 0;
	this.AddData	 = function(sName,sValue){
				this.names[this.cnt] = sName;
				this.values[this.cnt] = sValue;
				this.cnt++;
		};
	this.AddInput	 = function(oInput){
				this.inputs[this.iCnt++] = oInput;
		};
	this.SendData	 = function(sPath){
				var oTrasferForm = document.createElement('FORM');
				oTrasferForm.target = 'service_frm';
				oTrasferForm.method = 'post';
				oTrasferForm.enctype="multipart/form-data";
				oTrasferForm.encoding="multipart/form-data";
				oTrasferForm.action = sPath;
				for(var item = 0; item < this.cnt;item++){
						var oInput = document.createElement('INPUT');
						oInput.type = 'hidden';
						oInput.name = this.names[item];
						oInput.value = this.values[item];
						oTrasferForm.appendChild(oInput);
				}
				for(var i = 0; i < this.iCnt;i++){
						oTrasferForm.appendChild(this.inputs[i]);
				}
				document.getElementById('output_tranfer_div').appendChild(oTrasferForm);
				oTrasferForm.submit();
				document.getElementById('output_tranfer_div').removeChild(oTrasferForm);
				document.getElementById('output_tranfer_div').innerHTML = '';
		}
}

function showReview(field_id,action,field_name,field_value,id,type_field_id,parent_id,text_new_value){
	main_div = document.getElementById("review_main_div");
	msg_div = document.getElementById("review_msg_div");
	document.getElementById("ajax_loader").style.display = 'block';
	document.getElementById("content_field_edit").innerHTML = '';
	JsHttpRequest.query(
	    '/index.php', // backend
	    {
	        // pass a text value
			'id': id,
			'field_id': field_id,
			'parent_id': parent_id,
			'type_field_id': type_field_id,
			'field_value': field_value,
	        'action': action,
			'get_parameters': 1,
			'ajax': 1,
			'test':document.getElementById("field_id")
	    },
	    // Function is called when an answer arrives.
	    function(result, errors) {
	        // Write the answer.
	        showDebugMessages(errors);
	        document.getElementById("content_field_edit").innerHTML = result["html_block"].replace(/<br>/g,"\r\n");
	        document.getElementById("title").innerHTML = result["title"];
			document.getElementById("ajax_loader").style.display = 'none';
			document.getElementById("content_field_edit").style.display = 'block';
	    },
	    true  // do not disable caching
	);
	document.getElementById("field_id").value = field_id;
	document.getElementById("parent_id").value = parent_id;
	document.getElementById("type_field_id").value = type_field_id;
	document.getElementById("id").value = id;
	document.getElementById("action").value = action;
	moveDiv2center(main_div);
	main_div.style.display	= "block";
	msg_div.style.display	= "block";
   	document.getElementById("new_review_buttons").style.display = 'block';
}

function closeReview(main_div,msg_div){
	if (document.getElementById("field_value"))
	{
		document.getElementById("field_value").value = "";
	}
	closePebuzzLayers(main_div,msg_div);
}

function processReview() {
	if (document.getElementById("field_description"))//редактирование комментария к полю пользователя
	{
		var a =
			{
				// pass a text value
				'action': 		document.getElementById("action").value,
				'id': 		document.getElementById("id").value,
				'parent_id': 		document.getElementById("parent_id").value,
				'save_field_description': 	1,
				'field_description': 	document.getElementById("field_description").value,
				'ajax': 		1,
				'field_id': 	document.getElementById("field_id").value,
				'type_field_id': 	document.getElementById("type_field_id").value,
				'g': 	document.getElementById("action")
			};
	}
	else
	{
		//выбор значения для выбираемого поля
			if (document.getElementById("type_field_id").value == 1)
			{
				/*new_field_value = '';
				new_field_value = document.getElementById("new_field_name").value;
				if (new_field_value != ''){
					new_field_value = document.getElementById("new_field_name").value;
				}
				else
				{
					var radioGroupObj = document.getElementsByName("field_value");
					field_value = '';
					for (var i=0; i < radioGroupObj.length; i++)
					{
						if (radioGroupObj[i].checked)
						field_value = radioGroupObj[i].value;
					}
					if (field_value == ''){
						alert('Ошибка! Выберите, пожалуйста, значение поля!');
						return false;
					}
				}*/
				var radioGroupObj = document.getElementsByName("field_value");
				field_value = '';
				for (var i=0; i < radioGroupObj.length; i++)
				{
					if (radioGroupObj[i].checked)
					field_value = radioGroupObj[i].value;
				}
				if (field_value == ''){
					alert('Ошибка! Выберите, пожалуйста, значение поля!');
					return false;
				}
			}
			//ввод значения заполняемого поля
			else if(document.getElementById("type_field_id").value == 0)
			{
				field_value = document.getElementById("field_value").value;
				if (field_value == ''){
					alert('Ошибка! Заполните, пожалуйста, поле!');
					return false;
				}
			}
			var a =
				{
					// pass a text value
					'action': 		document.getElementById("action").value,
					'id': 		document.getElementById("id").value,
					'parent_id': 		document.getElementById("parent_id").value,
					//'new_field_value': 	new_field_value,
					'field_value': 	field_value,
					'ajax': 		1,
					'field_id': 	document.getElementById("field_id").value,
					'type_field_id': 	document.getElementById("type_field_id").value,
					'g': 	document.getElementById("action")
				};
	}
	JsHttpRequest.query(
	    '/index.php', // backend
	    a,
	    // Function is called when an answer arrives.
	    function(result, errors) {
			if (result['error'])
			{
				alert(result['error']);
			}
			else
			{
				if (result['fields'])
				{
					document.getElementById("fields").innerHTML = result['fields'];
				}
				closeReview("review_main_div","review_msg_div");
			}
	    },
	    true  // do not disable caching
	);
}

function showDebugMessages(errors){
	if(bGobalDebugMode && errors && document.getElementById("debug")){
		msg_div 	= document.getElementById("debug_msg_div");
       	msg_div.innerHTML 		= "<a herf=\"#\" onclick=\"document.getElementById(\"debug\").style.display=\"none\" ;return false;\">Close</a>";
       	msg_div.innerHTML 		+= errors;
		msg_div.style.display	= "block";
	}
}

var oShadow = new ShadowCLS;


function showSFcontent(){
 if(document.all){
        debug_var(document.frames['service_frm'].document.body.innerHTML,1);
    }else{
        debug_var(document.getElementById('service_frm').contentWindow.document.body.innerHTML,1);
    }
}

//Js debug tools
/*
debug_var(sel.duplicate(),2);
*/
function debug_var(mVar,iDebugLevel){
oShowArea     = document.createElement('DIV');
document.body.appendChild(oShowArea);
oShowArea.align
= "left";
oShowArea.style.width
= '640px';
oShowArea.style.height
= '480px';
oShowArea.style.position = 'absolute';
oShowArea.style.overflow = 'auto';
    oShowArea.style.left = '100px';
    oShowArea.style.top = '100px';
oShowArea.style.backgroundColor = '#EEEEEE';
    oShowArea.style.fontSize= '16px';
oShowArea.innerHTML = '<div align="center" onclick="document.body.removeChild(this.parentNode);" style="font-size:16px;background-color:#FFFF55;cursor:pointer">Close</div>';
oShowArea.innerHTML += print_r(mVar,iDebugLevel);
}

function print_r(mVar,iDebugLevel){
var sOut = '';
if(iDebugLevel){
iDebugLevel--;
switch(typeof(mVar)){
case 'object':
case 'array':
sOut += typeof(mVar) + "{<BLOCKQUOTE>";
for(key in mVar){
try {

sOut += "<br>" + "[" + key + "] = " + print_r(mVar[key],iDebugLevel);
}
catch (e) {

sOut += "<br>" + "[" + key + "] = " + e;
}
}
sOut += "</BLOCKQUOTE>}";
break;
case 'string':
sOut += mVar;
break;
case 'number':
sOut += mVar;
break;
case 'boolean':
sOut += "boolean (" + mVar + ")";
break;
case 'function':
if(0){
sOut += mVar;
}else{
sOut += '...handler source code...';
}
break;
default:
sOut += typeof(mVar);
}
}else{
sOut += "...";
}
return sOut;
}



