//
// Scripts
//



//   POPUP OPENING FUNCTIONS
function getFrameWidth(){
	var frameWidth = 0;
	if (self.innerWidth){
		frameWidth = self.innerWidth;
	}else if (document.documentElement && document.documentElement.clientWidth){
		frameWidth = document.documentElement.clientWidth;
	}else if (document.body){
		frameWidth = document.body.clientWidth;
	}
	return frameWidth;
}
function getFrameHeight(){
	var frameHeight = 0;
	if (self.innerWidth){
		frameHeight = self.innerHeight;
	}else if (document.documentElement && document.documentElement.clientWidth){
		frameHeight = document.documentElement.clientHeight;
	}else if (document.body){
		frameHeight = document.body.clientHeight;
	}
	return frameHeight;
}

function centerPopup(width,height){
	var frameWidth = getFrameWidth();
	var frameHeight = getFrameHeight();
	var leftF = Math.round( (frameWidth - width) / 2 ) ; 
	var topF  = Math.round( (frameHeight - height) / 2 ) ;
	var coordinates = new Array(leftF,topF);

	return coordinates;
}

function openPopup(url,width,height){
	var xy = new Array();
	xy = centerPopup(width,height);
	var leftD = xy[0];
	var topD = xy[1];
	//popUpWin = open(url, 'popUpWin','toolbar=0,location=0,scrollbars=1,resizable=0,width='+width+',height='+height +',screenX='+ (leftD+screenX)+',screenY='+ (topD+screenY));
	popUpWin = open(url, 'popUpWin','toolbar=0,location=0,scrollbars=1,resizable=0,width='+width+',height='+height +',left='+ leftD +',top='+ topD );
	popUpWin.focus();
}
// end popup opening functions




// For rollover effects -- on & off
function switchOn(imgName){
   if (document.images){
      imgOn=eval(imgName + "_on.src");
      document[imgName].src= imgOn;
    }
}

function switchOff(imgName){
   if (document.images){
      imgOff=eval(imgName + "_off.src");
      document[imgName].src= imgOff;
    }
}




function replaceAll( str, from, to ) {
	var idx = str.indexOf( from );
	while ( idx > -1 ) {
		str = str.replace( from, to );
		idx = str.indexOf( from );
	}
	return str;
}


function showHide(sub){
	//alert('showhiding: ' + sub);
	d = document.getElementById(sub);
	if(d.style.display == 'none'){
		d.style.display = 'block';
	}else{
		d.style.display = 'none';
	}         
}


function setHomepage(url){
	this.style.behavior='url(#default#homepage)';
	this.setHomePage(url);
}


function addBookmark(url,title){

	if( window.sidebar && window.sidebar.addPanel ) {
	    //Gecko (Netscape 6 etc.) - add to Sidebar
	    window.sidebar.addPanel( title,url,'target="_content"' );
	} else if( window.external && ( navigator.platform == 'Win32' ||
	      ( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) ) ) {
	    //IE Win32 or iCab - checking for AddFavorite produces errors in
	    //IE for no good reason, so I use a platform and browser detect.
	    //adds the current page as a favourite; if this is unwanted,
	    //simply write the desired page in here instead of 'location.href'
	    window.external.AddFavorite( url,title);
	} else if( window.opera && window.print ) {
	    //Opera 6+ - add as sidebar panel to Hotlist
	    //return true;
	} else if( document.layers ) {
	    //NS4 & Escape - tell them how to add a bookmark quickly (adds current page, not target page)
	    window.alert( 'Please click OK then press Ctrl+D to create a bookmark' );
	} else {
	    //other browsers - tell them to add a bookmark (adds current page, not target page)
	    window.alert( 'Please use your browser\'s bookmarking facility to create a bookmark' );
	}
	//return false;
}



  
// What's this? :-S
function jumpMenu(targ,selObj,restore){ //v3.0

  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


// This is for the inform a friend page (usually a popup with a form). It's used to 'automatically' refresh the contents
// of the message the visitor sends to his/her friend.
function updatePreview(){
	et = document.getElementById('eText');
	sn = document.getElementById('sName');
	rn = document.getElementById('rName');
	rm = document.getElementById('rMail');
	bt = document.getElementById('bText');
	rp = document.getElementById('recPreview');
	pl = document.getElementById('page');

	// Bother only if at least one field has something
	if ( (et.value.length > 0) || (sn.value.length > 0) || (rn.value.length > 0)  ) {
		tmp = bt.value.replace(/{RNAME}/,rn.value);
		tmp1 = tmp.replace(/{SNAME}/,sn.value);
		tmp1 = tmp1.replace(/{RMAIL}/,rm.value);		
		tmp1 = tmp1.replace(/{PROD}/,pl.value);	
		tmp1 = replaceAll(tmp1,'\n','<br>');
		tmp2 = replaceAll(et.value,'\n','<br>');
		rp.innerHTML = tmp1 + "<br><br>" + tmp2;
	}
}

// This is for the inform a friend page (usually a popup with a form). It's used to 'automatically' refresh the contents
// of the message the visitor sends to his/her friend.
function copyText(){
	bt = document.getElementById('bText');
	rp = document.getElementById('recPreview');
	bt.value = rp.innerHTML;

}


// -------------------

function showAdminSectionOptions(newURI){
	// Part 1: Get an object, regardless  of browser...
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
    } catch (e)  {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
    }
	
	// Part 2: Do the async stuff
	xmlHttp.onreadystatechange=function() 
	{
		var optionsDiv = document.getElementById('optionsDiv');
		
		if(xmlHttp.readyState==4) 
		{
			optionsDiv.style.opacity= "1";
			//wait.style.display = "none";
			//hideFieldsDiv.innerHTML = "1"+xmlHttp.responseText + "2<br>";
			optionsDiv.innerHTML = xmlHttp.responseText ;
		}else{
			//wait.style.display = "block";
			optionsDiv.style.opacity= ".60";
			optionsDiv.innerHTML = "<html><body><div align=\"center\"><br><img src=\"../images/040.gif\" ></div></body></html>";
		}	
	}
 
	xmlHttp.open("GET",newURI,true);
	xmlHttp.send(null);

} 

function f_optionMove(s_from, s_to,form) {
	var e_from = document.forms[form].elements[s_from],
		e_to   = document.forms[form].elements[s_to];
		
	if (!e_from)
		return alert ("Error: selectbox with name '" + s_from + "' can't be found.");
	if (!e_to)
		return alert ("Error: selectbox with name '" + s_from + "' can't be found.");

	var n_moved = 0;
	for (var i = 0; i < e_from.options.length; i++) {
		if (e_from.options[i].selected) {
			e_to.options[e_to.options.length] = new Option(e_from.options[i].text, e_from.options[i].value);
			
			n_moved++;
		}
		else if (n_moved)
			e_from.options[i - n_moved] = new Option(e_from.options[i].text, e_from.options[i].value);
	}
	if (n_moved)
		e_from.options.length = e_from.options.length - n_moved;
	else
		alert("You haven't selected any options");
}


function f_selectAll (s_select,form) {
	var e_select = document.forms[form].elements[s_select];
	for (var i = 0; i < e_select.options.length; i++)
			e_select.options[i].selected = true;
}


function sortlist(list) {
var lb = document.getElementById(list);
arrTexts = new Array();
arrValues = new Array();

for(i=0; i<lb.length; i++)  {
  arrTexts[i] = lb.options[i].text;
  arrValues[i] = lb.options[i].value;
}

arrTexts.sort();

for(i=0; i<lb.length; i++)  {
  lb.options[i].text = arrTexts[i];
  lb.options[i].value = arrValues[i];
}
}


///User Login Magic!!!!
var targetHeight = 4;
var animRate = 0.3;
var interval = 20;
var animationTimer = 0;

function toggleLoginForm()
{
	if(animationTimer != 0) return;
	
	var loginForm = document.getElementById("loginForm");
	
	if(loginForm.className == "clientLoginBlock")
	{
		var offH = loginForm.offsetHeight;
		loginForm.className = "clientLoginBlockHidden";
		loginForm.style.height = offH+"px";
		targetHeight = 4;
	}
	else
	{
		//loginForm.className = "clientLoginBlock";
		targetHeight = loginForm.offsetHeight;
		targetHeight = 46;
		loginForm.style.height = 4+"px";
	}
	
	animationTimer = window.setTimeout("animateFrame()", interval);
}

function animateFrame()
{
	var loginForm = document.getElementById("loginForm");
	if(targetHeight < loginForm.offsetHeight)
	{
		var step = (loginForm.offsetHeight - targetHeight)*animRate;
		if(step < 1) step = 1;
		loginForm.style.height = (loginForm.offsetHeight - step) + "px";
		if(loginForm.offsetHeight < targetHeight)
			loginForm.style.height = targetHeight+"px";
			
		animationTimer = window.setTimeout("animateFrame()", interval);
	}
	else if(targetHeight > loginForm.offsetHeight)
	{
		var step = (targetHeight - loginForm.offsetHeight)*animRate;
		if(step < 1) step = 1;
		loginForm.style.height = (loginForm.offsetHeight + step) + "px";
		if(loginForm.offsetHeight > targetHeight)
			loginForm.style.height = targetHeight+"px";
			
		animationTimer = window.setTimeout("animateFrame()", interval);
	}
	else
	{
		if(loginForm.offsetHeight == 4)
			loginForm.className = "clientLoginBlockHidden"
		else
		{
			loginForm.className = "clientLoginBlock";
			document.getElementById("usernameBox").focus();
		}

		animationTimer = 0;			
		loginForm.style.height="";
	}
}





/*
//    OLD POPUP FUNCTIONS

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height) {  // This is not used anymore, it's commented out - keep for a while just in case

	//alert('You dont exist, I will open you now');
	//popUpWin = open(URLStr, 'popUpWin','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=yes,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	popUpWin = open(URLStr, 'popUpWin','toolbar=0,location=0,scrollbars=1,resizable=0,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	popUpWin.moveTo(100,100);

}


function hop(url, win, width, height, options){
	var leftPos = (screen.availWidth - width) / 2;
	var topPos = (screen.availHeight - height) / 2;
	options += 'toolbar=0,location=0,scrollbars=1,resizable=0,width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
	//return window.open(url, win, options);
	
	return window.open(url, win, options);
}
*/

