var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1 ? true : false;
var Safari = navigator.appName.indexOf("Safari") != -1 ? true : false;

// --- 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 payment(params){
	openWin('/predir.cfm?' + params, 535, 660,100,50,'yes','no');
}
function addObj(params){
	document.location = '/my/upload/';
}
function addNote(params){
	openWin('/addnote.cfm?' + params, 390);
}


function resortGal(id){
	openWin('/my/albums/resort.cfm?gid='+id, 500, 600);
}


function preview(params){
	var arrParams = params.split('?');
	var url='/preview.cfm?';
	if(typeof isFlashOk != "undefined"){
		if(isFlashOk) url='/preview_flash.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 showTerms(){ openWin('/terms.cfm', 500); }
function openHelp(url){ openWin(url, 600, 550, 50, 10); }

function getMyGalEditURL(id){return '/my/albums/galedit.cfm?gid='+id;}
function getMyGalNewURL(){return '/my/albums/galedit.cfm';}
function getMyImgEditURL(id){return '/my/albums/imgedit.cfm?oid='+id;}


// --- Working with cookies ---
function getDomain(){
	return document.location.hostname.split('.').slice(-2).join('.');
}

function setCookie(name, value, expires){
	var domain=getDomain();
    document.cookie= name.toUpperCase() + "=" + escape(value) +
        ((expires) ? "; path=/; expires=" + expires.toGMTString() : "; path=/" ) +
        ((domain) ? "; domain=" + domain : "");
}


function getCookie(name){
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) { begin = dc.indexOf(prefix); if (begin != 0) return null; }
    else  begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
} 

function delCookie(name){
	var domain=getDomain();
	var name1 = name.toLowerCase();
	var name2 = name.toUpperCase();
	var addStr = "=; path=/; expires=Thu, 01-Jan-1990 00:00:01 GMT;domain="+domain;
    if (getCookie(name) != null) document.cookie = name + addStr;
    if (getCookie(name1) != null) document.cookie = name1 + addStr;
    if (getCookie(name2) != null) document.cookie = name2 + addStr;
}

function clearCookies() {
	var dc = document.cookie;
	var tempString = "";
	var tempChar;
	var count = 0;
	var cookieLength = dc.length;
	var cDel = new Array();
	while (count < cookieLength) {
	
		tempChar = dc.charAt(count);
		if (tempChar == '=') {
			cDel[cDel.length] = tempString;
			tempString = "";
		}
		if (tempChar == ';')tempString = "";
		if (tempChar != '=' && tempChar != ';' && tempChar != " ")
			tempString += tempChar;
		count += 1;
	}
	for(i=0; i < cDel.length; i++) delCookie(cDel[i]);
}

function setTkn(tkn){ delCookie('token'); setCookie('token', tkn); }
function delTkn(){ clearCookies(); /* delCookie('token'); */ }

function pushMovie(frm){
	var movie = InternetExplorer ? mypixart : document.mypixart;
	movie.GotoFrame(0);
	movie.GotoFrame(frm);
}


function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


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 getLabelForId(id) {
 var label, labels = document.getElementsByTagName('label');
 for (var i = 0; (label = labels[i]); i++) {
   if (label.htmlFor == id) {
     return label;
   }
 }
 return null;
}

function markCtrlAsError(id){
	var lbl = getLabelForId(id);
	if(lbl != null) lbl.className = 'error';
}

function markCtrlAsOK(id){
	var lbl = getLabelForId(id);
	if(lbl != null) lbl.className = 'label';
}

function checkRequired(id,getLabel) {
 if (!getLabel) getLabel=0;
 var error = 0;
 var formfield = document.getElementById(id);
 if (formfield.value.replace(/ /gi,"").length == 0)
 	error = 1; 	

 if (getLabel){
	 var label = getLabelForId(id);
	 if (error) {
		   label.className = 'error';
		   return false;
		   } 
	 else  {
		label.className = 'label';
		return true;
		}
	 }
	
	if (error) {
		return false;
		}

 return true;
}

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;
}

function checkLatOnly (txt){
	for (var i=0; i < txt.length; i++){
	    if(txt.charCodeAt(i) > 127) return false;
	}
   return true;
}
/******* functions for toolTips ******/
// 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 };
}


// 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, width: obj.clientWidth, height: obj.clientHeight  };
}


//reference to last showed toolTip
currentTip = '';
mouseOnImg = '';
tipTimeOut = 500;
scaleX = 20;
scaleY = 20;

//just for usability
function setEvent(obj){
	mouseOnImg = obj;
}

function hideOldTips(tip){
	if (currentTip != tip && document.getElementById(currentTip)) {
		document.getElementById(currentTip).style.display='none';
		clearTimeout(tipShow);
		clearTimeout(tipHide);
		currentTip = '';
	}
}


function showTip(obj,tip){
	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;	
	}
	
	if (!obj || !tip)
		return false;

	setEvent(obj);
	hideOldTips(tip);	//hide old Tip
	tipShow=setTimeout('showTipMain("'+obj+'","'+tip+'");',tipTimeOut)	;// show Tip after tipTimeOut ms
}
function showTipMain(obj,tip) {
	if (mouseOnImg == obj ) {	
	

		img=document.getElementById(obj);
		toolTip=document.getElementById(tip);
		objXY = getXY(img);//get X and Y

		toolTip.style.top=objXY.y+(objXY.height-scaleY)+'px';
		toolTip.style.left=objXY.x+(objXY.width-scaleX)+'px';
		toolTip.style.display='block';	
		toolTip.style.visibility='hidden';	
		/***** !!!NOTE!!!! this code MUST be after - toolTip.style.display='block'; ***/
		tipWidth = toolTip.clientWidth;
		tipHeight = toolTip.clientHeight;
		/*****/		
		scrollXY = getScrollXY();
		brwWH = browserSize();

		tipXY=getXY(toolTip);//get actual tip position
		//tooltip уходит за границу вниз
		if ((tipXY.y-scrollXY.y+tipHeight) > brwWH.h) 
					toolTip.style.top=objXY.y-(tipHeight-scaleY)+'px';					
	
		tipXY=getXY(toolTip);//get actual tip position
			//tooltip уходит за границу справа
			if ((tipXY.x-scrollXY.x+tipWidth) > brwWH.w)	
					toolTip.style.left=objXY.x-(tipWidth-scaleX)+'px';		
			
		tipXY=getXY(toolTip);//get actual tip position
			//tooltip уходит за границу сверху
			if ((tipXY.y-scrollXY.y) < 0)
					toolTip.style.top=objXY.y+(objXY.height-scaleY)+'px';		

		tipXY=getXY(toolTip);//get actual tip position
			//tooltip уходит за границу справа
			if ((tipXY.x-scrollXY.x) < 0)
					toolTip.style.top=objXY.x+(objXY.width-scaleX)+'px';		
		
		
		toolTip.style.visibility='visible';			
		hideOldTips(tip);
		currentTip = tip;
	}

		
}
function hideTip(tip){
	setEvent('');	// hide Tip after tipTimeOut ms
	tipHide=setTimeout('hideTipMain("'+tip+'");',tipTimeOut);
}

function hideTipMain(tip){
	if (mouseOnImg==''){
		document.getElementById(tip).style.display='none';	
	}	
}



/******* functions for toolTips end ******/




/* 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();}
}
