var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1 ? true : false;
var Safari = navigator.appName.indexOf("Safari") != -1 ? true : false;

// Object sizes and positions
function browserSize(){
  var x,y;
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  return {w: myWidth, h: myHeight};
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement &&
      ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return {x: scrOfX, y:scrOfY };
}

// --- Window opening library
function openWin(scriptURL, height, width, left, top, status, menubar){
	if(width == null) width = 550;
	if(left == null) left = 160;
	if(top == null) top = 160;
	if(status == null) status = "no";
	if(menubar == null) menubar = "no";
	var wnd = window.open(scriptURL, 'pa_win', 'resizable=yes,scrollbars=yes,menubar='+menubar+',status='+status+',height='+height+',width='+width+',left='+left+',top='+top);
	wnd.focus();
}

function preview(params){
	var arrParams = params.split('?');
	//var url = '/svc/pp.cfm?';
	var url='/preview.cfm?';
	var token = getCookie('token');
	var height = 700;
	if(token==null) token='';
	if(arrParams.length > 1) url = url + arrParams[1];
	else url = url + params;
	url = url + '&token='+token;
	//if(screen.height > 850){
	//	height = 800; url = url + "&fm=1";
	//}
	
   var winwidth = screen.availWidth;
   var winheight = screen.availHeight;
	

if (document.all) {
	 window.open(url,"",'left=0,top=0,width='+winwidth+',height='+winheight+',scrollbars=auto,fullscreen=yes');

} else {
  openWin(url, winheight, winwidth, 0, 0, 'no', 'no') 
   }
	//openWin(url, height, 700, 50, 0);
}
	
function findObjById(id){
	var x, d=document;
	if(d.getElementById) x=d.getElementById(id);
	else if(d.all) x=d.all[id];
	return x;
}
function intToStr(i){
	return ''+i;
}


function checknumber(value){
  if (value.length == 0) return true;
  var number_format = ".0123456789";
  var check_char;
  for (var i = 0; i < value.length; i++)  {
    check_char = number_format.indexOf(value.charAt(i));
	if(check_char < 0) return false;
  }
  return true;
}

function checkPhone(value){
  if (value.length < 3) return true;
  var number_format = "0123456789";
  var num_count = 0;
  for (var i = 0; i < value.length; i++)  {
    if(number_format.indexOf(value.charAt(i)) >= 0)	num_count++;
  }
  if(num_count < 5) return false;
  return true;
}

function checkEmail2(value){
	var email = /^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return email.test(value);
}

function checkEmail(id){
	var email = /^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	value = document.getElementById(id).value;
	if (!email.test(value)) {
          return false;
   }	
   return true;
}	

function cssClass (obj,classname){
	document.getElementById(obj).className=classname;
}

/******* functions for toolTips ******/


// this function return object with object's absolute coordinate
function getXY(obj){
	var x,y,t=obj;
		x=0;y=0;
	do {
		x+=t.offsetLeft;
		y+=t.offsetTop;
		t=t.offsetParent;
	}
	while(t!=null);
	
	return {x: x, y: y};
}


//reference to last showed toolTip
currentTip = '';
mouseOnImg = '';
prefixTip = 'tip_';
tipTimeOut = 500;
scaleX = 30;
scaleY = 30;
frameW = 6;        // 3+3 : бордер 2 раза
frameH = 22;       // 3+3+14+1+1 : бордер 2 раза, иконка, vspace 2 раза

// set tooltip parameters
function setToolTip(tiprefix, titimeout, scx, scy, frw, frh){
	prefixTip = tiprefix;
	tipTimeOut = titimeout;
	scaleX = scx;
	scaleY = scy;
	frameW = frw;
	frameH = frh;
}

//just for usability
function setEvent(obj){
	mouseOnImg = obj;
}

function hideOldTips(obj){
    tip=prefixTip+obj;
	if (currentTip != tip && document.getElementById(currentTip)) {
		document.getElementById(currentTip).style.display='none';
		clearTimeout(tipShow);
		clearTimeout(tipHide);
		currentTip = '';
	}
}

function showTip(obj, pxw, pxh){
	if (document.getElementById('msgConfirm')) {
		if (document.getElementById('msgConfirm').style.display=='block')
		 return false;	
	}
	if (document.getElementById('msgAlert')) {
		if (document.getElementById('msgAlert').style.display=='block')
		 return false;	
	}

	setEvent(obj);
	hideOldTips(obj);	//hide old Tip
	
	tipShow=setTimeout('showTipMain("'+obj+'","'+pxw+'","'+pxh+'");',tipTimeOut); // show Tip after tipTimeOut ms

}
function showTipMain(obj, pxwidth, pxheight) {
	if (mouseOnImg == obj ) {	
		tip=prefixTip+obj;
		img=document.getElementById(obj);
		toolTip=document.getElementById(tip);

		objXY = getXY(img); //get X and Y
		scrollXY = getScrollXY();
		brwWH = browserSize();
		imgx  = objXY.x-scrollXY.x;
		imgy  = objXY.y-scrollXY.y;
/*		 alert('Координаты: x='+imgx+' y='+imgy+' screen='+brwWH.w+'|'+brwWH.h+ ' picture='+pxwidth+'|'+pxheight); 
*/
/*  вариант 1
		if(imgx > (brwWH.w+scaleX-pxwidth)) objXY.x=objXY.x-(pxwidth+scaleX);
		if(imgy > (brwWH.h+scaleY-pxheight)) objXY.y=objXY.y-(pxheight+scaleY);
		(imgx+scaleX+pxwidth) > brwWH.w
		(imgy+scaleY+pxheight) > brwWH.h
*/
/*  вариант 2
		if(imgx > (brwWH.w-pxwidth)) objXY.x=objXY.x-pxwidth;
		imgx  = objXY.x-scrollXY.x;
		if(imgx < 0) objXY.x=objXY.x-imgx+scaleX;
		if(imgy > (brwWH.h-pxheight)) objXY.y=objXY.y-pxheight;
		imgy  = objXY.y-scrollXY.y;
		if(imgy < 0) objXY.y=objXY.y-imgy+scaleY;
		alert(' picture='+pxwidth+'|'+pxheight); 		
		alert(' frame='+frameW+'|'+frameH); 		
*/
        pxwidth  = eval(pxwidth) + frameW;
		pxheight = eval(pxheight) + frameH;
		
		if((imgx+scaleX+pxwidth) > (brwWH.w-scaleX)) objXY.x=imgx+scaleX-pxwidth+scrollXY.x;
		
		if((imgy+scaleY+pxheight) > (brwWH.h-scaleY)) objXY.y=imgy+scaleY-pxheight+scrollXY.y;
		 fx  = objXY.x-scrollXY.x;
		 fy  = objXY.y-scrollXY.y;
/* alert('Координаты: x='+fx+' y='+fy); 		*/

		 if(fx < 0) objXY.x=objXY.x-fx; // (brwWH.w-pxwidth-scaleX)+scrollXY.x;
		 if(fy < 0) objXY.y=objXY.y-fy; //(brwWH.h-pxheight-scaleY)+scrollXY.y;
/* alert('Координаты: x='+objXY.x+' y='+objXY.y); 		*/

		
/*
alert('Координаты: x='+objXY.x+' y='+objXY.y+' screen='+brwWH.w+'|'+brwWH.h+ ' picture='+pxwidth+'|'+pxheight); 		
*/		
		toolTip.style.top=objXY.y+scaleY+'px';
		toolTip.style.left=objXY.x+scaleX+'px';
		toolTip.style.display='block';	
		hideOldTips(obj);
		currentTip = tip;
	}
}

function hideTip(obj){
    tip=prefixTip+obj;
	setEvent('');	// hide Tip after tipTimeOut ms
	tipHide=setTimeout('hideTipMain("'+tip+'");',tipTimeOut);
}

function hideTipNow(obj){
    tip=prefixTip+obj;
	setEvent('');	// hide Tip after tipTimeOut ms
	tipHide=setTimeout('hideTipMain("'+tip+'");',0);
}

function hideTipMain(tip){
	if (mouseOnImg==''){
		document.getElementById(tip).style.display='none';	
	}	
}



/******* functions for toolTips end ******/

/* just for safety


function imgToolTip(obj,tip,mode){

	setTimeout('imgToolTip2("'+obj+'","'+tip+'","'+mode+'");',1000)
}

function imgToolTip2(obj,tip,mode) {
	img=document.getElementById(obj);
	toolTip=document.getElementById(tip);

	//hide old toolTip
	if (currentTip){
		currentTip.style.display='none';
		currentTip='';
	}
	
	if (mode=='hide'){//hide tip
		toolTip.style.display='none';
		return true;
	}
	objXY = getXY(img);//get X and Y
	toolTip.style.top=objXY.y+40+'px';
	toolTip.style.left=objXY.x+20+'px';
	toolTip.style.display='block';
	currentTip=toolTip;


}

*/


/* custom alert function  */
alertCallBack = '';
function msgAlert (title, body, cbackFunction){

	if (!document.getElementById('msgAlert')) {
		if (title.length)
			alert(title+'. '+body);
		else
			alert (body);	
		return false;
	}
	
	
	alertCallBack = cbackFunction;
	if (!title) title='';
	document.getElementById('msgAlertTitle').innerHTML = title;
	document.getElementById('msgAlertText').innerHTML = body;
	document.getElementById('msgAlert').style.display='block';
	ctrlHider('select',0);
	if(!Safari) document.location='#';
	return false;
}

function alertOK(){
	document.getElementById('msgAlert').style.display='none';
	ctrlHider('select',1);
	if (alertCallBack != null)
		window.setTimeout(alertCallBack, 50);
}

confirmCallBack = '';
function msgConfirm (title, body, cbackFunction){


	if (!document.getElementById('msgConfirm')) {
		tmpStr = body;
		if (title.length)
			tmpStr=title+'. '+body;
		if (confirm(tmpStr) && cbackFunction != null)
			window.setTimeout(cbackFunction, 50);
		return false;
	}
	


	ctrlHider('select',0);
	confirmCallBack = cbackFunction;
	document.getElementById('msgConfirmTitle').innerHTML = title;
	document.getElementById('msgConfirmText').innerHTML = body;
	document.getElementById('msgConfirm').style.display='block';
	if(!Safari) document.location='#';
	return false;
}


function confYes(){
	document.getElementById('msgConfirm').style.display='none';	
	ctrlHider('select',1);
	if (confirmCallBack != null)
		window.setTimeout(confirmCallBack, 50);
}
function confNo(){document.getElementById('msgConfirm').style.display='none';	ctrlHider('select',1);}

// function hide or show all specific control types on page mode=1 - show, mode=0 - hide
hiddenCtrls = new Array();
function ctrlHider(ctrlType,mode){ 	
 if (!document.all) return true;
 var controls, controls = document.getElementsByTagName(ctrlType);
 for (i=0; i < controls.length; i=i+1) {

	if (!mode)	{
		if (controls[i].style.visibility == "hidden" || controls[i].style.display == "none" ){
		hiddenCtrls.push(i);			
		} else{	controls[i].style.visibility = "hidden";}
	}else{
		ableToShow = true;
		for (j in hiddenCtrls) {if (hiddenCtrls[j] == i){ ableToShow = false;break; } }
		if (ableToShow)
		controls[i].style.visibility = "visible";		
	} 
 }
 if (mode) {hiddenCtrls = new Array();}
}


