/* Global Orange Plug Web Functions */

images_path = "/web/jsp/img/";
sessionIDPrefix = ";jsessionid=";

imagesMax = 7;
imagesCount = 0;
imageStore = new Array();

var chatPopupOpened = false;

function MenuOn(strid) {
   document[strid].src = images_path + strid + "2.gif";
}


function MenuOff(strid) {
   document[strid].src = images_path + strid + "1.gif";
}


function MenuPreload(strid) {
  if (imagesCount<imagesMax) {
    var index = imagesCount;
    imageStore[index] = new Image();
    imageStore[index].src = images_path + strid + "2.gif";
    imagesCount++;
  }
}


function nRand(maxval) {
	today=new Date();
	seed=today.getTime();
    seed = (seed*9301+49297) % 233280;
    seed = seed /(233280.0);
	return Math.ceil(seed*maxval);
}


function popUp(path, w, h, handler) {
	var t=(screen.height/2)-(h/2)-50;
	var l=(screen.width/2)-(w/2);
  if(!handler) handler="pop"+nRand(100);
	var windowprops = "location=no,scrollbars=no,menubars=no,toolbars=no,resizable=no, left=" + l + ",top=" + t + ",width=" + w + ",height=" + h;
	popwin = window.open(path,handler,windowprops);
	popwin.focus();
}


function popUpScroll(path, w, h, handler) {
	var t=(screen.height/2)-(h/2)-50;
	var l=(screen.width/2)-(w/2);
  if(!handler) handler="pop"+nRand(100);
  var windowprops = "location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=no, left=" + l + ",top=" + t + ",width=" + w + ",height=" + h;
	popwin = window.open(path,handler,windowprops);
	popwin.focus();
}


function refreshParent() {
       if (window.opener) window.opener.location.reload();
}


function confirmationMessage(cMessage, cPathOK, cPathCancel) {
	isConfirmed = window.confirm(cMessage);

	if (isConfirmed) {
		if (cPathOK == "") {
			return;
		}
		else {
			window.location.href = cPathOK;
			return;
		}
	}
	else {
		if (cPathCancel == "") {
			return;
		}
		else {
			window.location.href = cPathCancel;
			return;
		}
	}
}

function openImageWindow(imageID) { 
    popup_url = "/web/jsp/pop_image.jsp?imageID=" + imageID;
    var t=20;
    var l=20;
	var windowprops = "location=no,scrollbars=no,menubars=no,toolbars=no,resizable=yes, left=" + l + ",top=" + t + ",width=320,height=200";
	popwin = window.open(popup_url,"pop"+nRand(100),windowprops);
	popwin.focus();
}

/* If you want only to trim, use for the third parameter value false
*/
var lastValue = "";
function handleDefaultValue(obj, defaultValue, setToEmpty){
	
	obj.value = obj.value.trim();
	if (setToEmpty == true && defaultValue.trim() == obj.value) {
    obj.value = "";
  }
	return;
}

function popUpNameCompatibility(name1, name2){
	popUpScroll("/web/jsp/loveMachine/name_compat_result_pop.jsp?name1="+name1+ "&name2="+name2 , 300, 220);
}

function preloadImages(imagePathArray){

	for(i=0; i < imagePathArray.length; i++) {
    	var preload = new Image();
    	preload.src = imagePathArray[i];
	}
		
}

function popUpSendMessage(sessionID, userID){
	popUp("/web/jsp/sendMessage.jsp"+sessionIDPrefix+sessionID+"?userID="+userID, 425, 325);
}

function popUpDeleteMessage(sessionID,messageID,senderName){
	popUpScroll("/web/jsp/delMessage.jsp"+sessionIDPrefix+sessionID+"?sname="+senderName+"&msgID="+messageID, 425, 130);
}

function limitText(field, maxlen, message) {
	
	if (field.value.length > maxlen) {
		field.value = field.value.substring(0, maxlen);
		alert(message);

	}
}

function popLogin(){
	popUp('/web/jsp/popup_register.jsp',415,210,'popReg');
}

/*** CHAT JS FUNCTIONS ***/
function popUpChat(path, w, h, handle) {
	var t=(screen.height/2)-(h/2)-50;
	var l=(screen.width/2)-(w/2);
	var windowprops = "location=no,status=no,scrollbars=no,menubars=no,toolbars=no,resizable=no, left=" + l + ",top=" + t + ",width=" + w + ",height=" + h;

	popwin = window.open('','chatwin',windowprops);
	if (popwin.window.users == null) {
		popwin.window.location = path;
	}
	return popwin;
}

function openChat(baseURL,userID,userName,userPlatform) {
	var path = baseURL+'?recipientID='+userID;
	chatwin = popUpChat(path,450,415,'chatwin');
	
	if (chatwin.window.users) {
    //var strUserID = userID.toString();
		//chatwin.window._callFunction(strUserID,"addUserToChatWindow");
		chatwin.window.addUserToChatWindow(userID,userName,userPlatform);
		chatwin.window.focus();
	}
}
/*** END CHAT JS FUNCTIONS ***/



/*** CHAT JS FUNCTIONS ***/
function popUpChat2(path, w, h, handle) {
    var t=20;
    var l=20;
	var windowprops = "location=no,status=no,scrollbars=no,menubars=no,toolbars=no,resizable=no, left=" + l + ",top=" + t + ",width=" + w + ",height=" + h;

	popwin = window.open('','chatwin',windowprops);
	if (popwin.window.users == null) {
		popwin.window.location = path;
	}
	return popwin;
}


function openChat2(baseURL,userID,userName,userPlatform) {
	var path = baseURL+'?recipientID='+userID;
	chatwin = popUpChat2(path,450,415,'chatwin');
	
	if (chatwin.window.users) {
    //var strUserID = userID.toString();
		//chatwin.window._callFunction(strUserID,"addUserToChatWindow");
		chatwin.window.addUserToChatWindow(userID,userName,userPlatform);
		chatwin.window.focus();
	}
}
/*** END CHAT JS FUNCTIONS ***/





var imagesToResize = new Array;

function resizeImage(pictureObj, size) {
	var imageToResizeObject = new Object();
	imageToResizeObject.pictureObj = document.getElementById(pictureObj);
	imageToResizeObject.size = size;
	
	imagesToResize[imagesToResize.length] = imageToResizeObject;
}


function resizeImageProcessAll() {
	for (i=0; i<imagesToResize.length; i++) {
		if (imagesToResize[i].pictureObj) {
			var h = imagesToResize[i].pictureObj.height;
			var w = imagesToResize[i].pictureObj.width;
			
			if (w > h) {
				imagesToResize[i].pictureObj.height = imagesToResize[i].size;
				imagesToResize[i].pictureObj.removeAttribute("width");
			}
			else {
				imagesToResize[i].pictureObj.width = imagesToResize[i].size;
				imagesToResize[i].pictureObj.removeAttribute("height");
			}
		}
	}
}


// DEPRECATED !!!!!
function resizeImg(ownerId,pictureObj,size) {	
	if (pictureObj) {
		var h = pictureObj.height;
		var w = pictureObj.width;

		if (w > h) {
			pictureObj.height = size;
			pictureObj.removeAttribute("width");
		}
		else {
			pictureObj.width = size;
			pictureObj.removeAttribute("height");
		}
	}
}


function resizeImgOnlyIfBigger(ownerId,pictureObj,size,isWidth) {	
	if (pictureObj) {
		var h = pictureObj.height;
		var w = pictureObj.width;
	
		if (!isWidth && h>size) {
			pictureObj.height = size;
			pictureObj.removeAttribute("width");
		}
		else {
			if(w>size) {
				pictureObj.width = size;
				pictureObj.removeAttribute("height");
			}
		}
	}
}


function resizeImgHint(ownerId,pictureObj,size,hint){
	var h = pictureObj.height;
	var w = pictureObj.width;

	var propertyname = "";
	var value = 0;
	if(w>h) {
		propertyname = "height";
	} else {
		propertyname = "width";
	}
	document.getElementById(ownerId).innerHTML = '<img src="'+pictureObj.src+'" '+propertyname+'="'+size+'" onmouseover="return overlib(\''+hint+'\');" onmouseout="return nd();"/>';
}




var showHideMap = new Array;

function showOrHide(showHideId) {
	var showHideObject = null;
	for (i=0; i<showHideMap.length; i++) {
		if (showHideMap[i].id == showHideId) {
			showHideObject = showHideMap[i];
		}
	}
	if (!showHideObject) {
		showHideObject = new Object();
		showHideObject.id = showHideId;
		showHideObject.isShown = false;
	}

	var plusMinusImage = document.getElementById(showHideId+'PlusMinus');
	
	if(!showHideObject.isShown) {
		showHideObject.isShown = true;
		$(document.getElementById(showHideId)).slideDown(500);
		if (plusMinusImage) {
			plusMinusImage.src = '/web/jsp/img/btn_minus.gif';
		}
	}
	else {
		showHideObject.isShown = false;
		$(document.getElementById(showHideId)).slideUp(500);
		if (plusMinusImage) {
			plusMinusImage.src = '/web/jsp/img/btn_plus.gif';
		}
	}
	
	showHideMap[showHideMap.length] = showHideObject;
}


function checkAllPlusMinus() {
	// JK: jQuery solution DOESNT work in IE :-)))))) what a surprise !
	//var images = $(".plusMinus > img");
	//var hiddenObject = $(document.getElementById(name));
	//var innerText = $("newsFeedList").html;

	// so let's use the old school stuff	
	var images = document.getElementsByName("plusMinus");
	
	for (i=0; i<images.length; i++) {
		var name = images[i].id.substring(0, images[i].id.length-9);
		var hiddenObject = document.getElementById(name);
		
		if (!hiddenObject || jQuery.trim(hiddenObject.innerHTML) == "") {
			images[i].style.display="none";
			images[i].parentNode.parentNode.parentNode.style.cursor="default";
		}
	}
}


function goToTopAndScrollLoginStrip(targetUrl) {
		window.scroll(0,0);
		$(window.document.getElementById('loginTopStrip')).slideDown(600);
		
		if (targetUrl) {
			window.document.getElementById('loginFormHeader').targetUrl.value = targetUrl;
		}
}
