// JavaScript Document
var bwr = navigator.appName;
var ver = parseInt(navigator.appVersion, 10);
//alert (bwr + ver);
NS4 = ( bwr == "Netscape" && ver == 4) ? 1 : 0;
NS6 = ( bwr == "Netscape" && ver == 6) ? 1 : 0;
IE4 = ( bwr == "Microsoft Internet Explorer" && ver == 4) ? 1 : 0;
IE5 = ( bwr == "Microsoft Internet Explorer" && ver == 5) ? 1 : 0;
IE6 = ( bwr == "Microsoft Internet Explorer" && ver == 6) ? 1 : 0;
isMac = (navigator.appVersion.indexOf("Mac") != -1) ? 1 : 0;
isDynamic = (NS4 || (IE4 && !isMac) || NS6 || IE5) ? 1 : 0;



function getMouseclickX(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
		+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
		+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
	return posx;
}

function getMouseclickY(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
		+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
		+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information

	//saving it to global variable
	pageY=posy;

	return posy;
}

function getElementPositionY(elem)
{
	var element = document.getElementById(elem);
	var y = element.offsetTop;
	return y;
}




function setTransparency (element, level)
{
	var elem = document.getElementById(element);
	var NS4 = (navigator.appName.indexOf("Netscape")>=0 && !document.getElementById)? true : false;
	var IE4 = (document.all && !document.getElementById)? true : false;
	var IE5 = (document.getElementById && document.all)? true : false;
	var NS6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
	var W3C = (document.getElementById)? true : false;
	ieop=level; //change
	if(IE4 || IE5) elem.style.filter="alpha(opacity="+ieop+")";
	if(NS6) elem.style.MozOpacity=ieop/100;
}

function entityDecode(s) {
	var e = document.createElement("div");
	e.innerHTML = s;
	return e.firstChild.nodeValue;
}
function formData2QueryString(docForm) {
	var submitContent = '';
	var formElem;
	var lastElemName = '';
	for (i = 0; i < docForm.elements.length; i++) {
		formElem = docForm.elements[i];
		if(formElem.disabled) continue;
		switch (formElem.type) {

			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'submit':
			case 'select-one':
			submitContent += formElem.name + '=' + escape(formElem.value) + '&'
			break;
			// Radio buttons
			case 'radio':
			if (formElem.checked) {
				submitContent += formElem.name + '=' + escape(formElem.value) + '&'
			}
			break;
			// Checkboxes
			case 'checkbox':
			if (formElem.checked) {
				// Continuing multiple, same-name checkboxes
				if (formElem.name == lastElemName) {
					// Strip of end ampersand if there is one
					if (submitContent.lastIndexOf('&') == submitContent.length-1) {
						submitContent = submitContent.substr(0, submitContent.length - 1);
					}
					// Append value as comma-delimited string
					submitContent += ',' + escape(formElem.value);
				}
				else {
					submitContent += formElem.name + '=' + escape(formElem.value);
				}
				submitContent += '&';
				lastElemName = formElem.name;
			}
			break;
		}
	}

	// Remove trailing separator

	submitContent = submitContent.substr(0, submitContent.length - 1);

	return submitContent;

}

var responseText;
var pageY=0;

function xmlhttpGet(strURL, strSubmit, strResultFunc,id, refreshNeeded) {
	var xmlHttpReq = false;
	// IE
	if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	// Mozilla/Safari
	else if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
		xmlHttpReq.overrideMimeType('text/xml');
	}
	xmlHttpReq.open('POST', strURL, true);
	xmlHttpReq.setRequestHeader('Content-Type',
	'application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {
			//performing our things
			responseText=xmlHttpReq.responseText;
			eval(strResultFunc+"(responseText,'"+id+"','"+refreshNeeded+"')")
		}

	}
	xmlHttpReq.send(strSubmit);
}
function processLogin(frm){
	submit=formData2QueryString(frm);
	xmlhttpGet("login.ajax.php", submit, "callbackWrapper",'login-section',1);
	return false;
}

function processRating(frm){
	submit=formData2QueryString(frm);
	xmlhttpGet("wish.rate.ajax.php", submit, "callbackWrapper",'login-section',1);
	return false;
}

function processFullfill(frm){
	submit=formData2QueryString(frm);
	xmlhttpGet("wish.fullfill.ajax.php", submit, "callbackWrapper",'login-section',1);
	return false;
}

function callbackWrapper(responseText,obj,refreshNeeded){
	//checking response for the presence of error tags/markups
	if(responseText.indexOf('<span class="error">')!=-1){
		//OMG this is a error
		showErrorPopup(responseText);
	}
	else{
		if(responseText.indexOf('<span class="info">')!=-1){
			//OMG this is a error
			responseText=responseText.replace('<span class="info">','');
			responseText=responseText.replace('</span>','');

			var args= new Array();

			args=responseText.split('|');
			showInfoPopup(args[0],args[1],args[2],args[3]);
		}
		else{
			cell=document.getElementById(obj);
			cell.innerHTML=responseText;
			if(refreshNeeded){
				history.go(0);
			}
		}
	}

	//document.write(responseText);
}

function showBackgroundGray(){
	var documentWidth = document.body.offsetWidth;
	var documentHeight = document.getElementById('footer').offsetHeight + document.getElementById('footer').offsetTop;
	//document.write(documentWidth + ', ' + documentHeight);
	var bgGray=document.getElementById('background-gray');
	if(!bgGray){
		var bgGray=document.createElement('div');
		bgGray.id='background-gray';
	}
	else{
		bgGray.style.display='block';
	}
	bgGray.style.width = documentWidth + 'px';
	if (documentHeight>document.body.offsetHeight)
	{
		bgGray.style.height = documentHeight + 'px';
	}
	else
	{
		bgGray.style.height = document.body.offsetHeight + 'px';
	}
	document.body.appendChild(bgGray);
}


function showErrorPopup(errMsg,url){
	showBackgroundGray();
	var onClick=function(){}

	if(!url){
		var url='javascript:void(null)';
		var onClick = function(){document.getElementById('popup-window').style.display='none'; document.getElementById('background-gray').style.display='none';}
	}

	var div=document.getElementById('popup-window');
	if(div){
		div.parentNode.removeChild(div);
	}


	var div=document.createElement('div');
	var a=document.createElement('a');
	var p=document.createElement('p');
	var h2=document.createElement('h2');

	//preparing div element
	div.id='popup-window';

	h2.innerHTML=document.getElementById('errorPopupTitle').value;
	div.appendChild(h2);

	p.innerHTML=errMsg;
	div.appendChild(p);

	a.href=url;
	a.onclick=onClick;
	a.className='popup-btn-orange';
	a.innerHTML='<big>'+document.getElementById('errorPopupButton').value+'</big>';
	div.appendChild(a);

	document.body.appendChild(div);
}



function showInfoPopup(title, body, button, url){
	showBackgroundGray();
	var div=document.getElementById('popup-window');
	if(div){
		div.parentNode.removeChild(div);
	}
	var div=document.createElement('div');
	var a=document.createElement('a');
	var p=document.createElement('p');
	var h2=document.createElement('h2');

	//preparing div element
	div.id='popup-window';

	if(pageY) div.style.top=pageY+'px';

	h2.innerHTML=title;
	div.appendChild(h2);

	p.innerHTML=body;
	div.appendChild(p);

	a.href=url;
	a.className='popup-btn-green';
	a.innerHTML='<big>'+button+'</big>';
	div.appendChild(a);

	document.body.appendChild(div);
}


/*** registration *******************************************************/
function showArrowTipsReg (action, id){
	switch (action){
		case 'show':
		document.getElementById('reg-tip-' + id).className = 'tips-active';
		document.getElementById('reg-arrow-' + id).className = 'arrow-active';
		break;
		case 'hide':
		document.getElementById('reg-tip-' + id).className = 'tips';
		document.getElementById('reg-arrow-' + id).className = 'arrow';
		break;
	}
}




/*** popup login *******************************************************/
function showLoginPopup (title, body, button,cancel, url, e){
	if (!e) var e = window.event;

	showBackgroundGray();
	var div = document.getElementById('popup-login');
	if(div){
		div.parentNode.removeChild(div);
	}
	var div = document.createElement('div');
	var a = document.createElement('a');
	var p = document.createElement('p');
	var h2 = document.createElement('h2');
	var form=document.createElement('form');
	var fieldset=document.createElement('fieldset');

	var labelUsername = document.createElement('label');
	var labelPassword = document.createElement('label');
	var inputUsername = document.createElement('input');
	var inputPassword = document.createElement('input');

	var aCancel = document.createElement('a');

	//preparing div element
	div.id = 'popup-login';
	div.style.top=getMouseclickY(e)-60+'px';



	h2.innerHTML = title;
	div.appendChild(h2);

	p.innerHTML = body;
	div.appendChild(p);

	labelUsername.innerHTML = 'Username';
	fieldset.appendChild(labelUsername);
	inputUsername.className = 'input';
	inputUsername.id='userLogin';
	inputUsername.name='userLogin';

	fieldset.appendChild(inputUsername);
	labelPassword.innerHTML = 'Password';
	fieldset.appendChild(labelPassword);
	inputPassword.className = 'input';
	inputPassword.type='password';
	inputPassword.id='userPassword';
	inputPassword.name='userPassword';
	fieldset.appendChild(inputPassword);

	aCancel.href = 'javascript: hideLoginPopup()';
	aCancel.className = 'cancel';
	aCancel.innerHTML = cancel;
	fieldset.appendChild(aCancel);

	a.href = 'javascript:void(null)';
	a.onclick=function(){processLogin(this.parentNode.parentNode);hideLoginPopup()}
	a.className = 'popup-btn-green';
	a.innerHTML = '<big>' + button + '</big>';
	fieldset.appendChild(a);

	form.appendChild(fieldset);
	div.appendChild(form);

	document.body.appendChild(div);
}


function hideLoginPopup(){
	var div = document.getElementById('popup-login');
	if (div){
		div.parentNode.removeChild(div);
	}
	div = document.getElementById('background-gray');
	if (div){
		div.parentNode.removeChild(div);
	}
}



/*** popup rating *******************************************************/


function showRatingPopup(id,title1,heading1,login,title2,heading2,heading3,time,button,cancel,e){
	if (!e) var e = window.event;

	id=unescape(id);
	title1=unescape(title1);
	heading1=unescape(heading1);
	login=unescape(login);
	title2=unescape(title2);
	heading2=unescape(heading2);
	heading3=unescape(heading3);
	time=unescape(time);
	button=unescape(button);
	cancel=unescape(cancel);

	showBackgroundGray();
	var div = document.getElementById('popup-rating');
	if(div){
		div.parentNode.removeChild(div);
	}

	//preparing everything
	var div = document.createElement('div');
	var a = document.createElement('a');
	var p = document.createElement('p');
	var h2 = document.createElement('h2');
	var h3 = document.createElement('h2');
	var form=document.createElement('form');
	var input=document.createElement('input');
	var textarea=document.createElement('textarea');
	var table=document.createElement('table');
	var fieldset=document.createElement('fieldset');
	var hidden=document.createElement('input');
	hidden.className='hidden';


	//starting our dirty business
	wrapper=div.cloneNode(false);
	wrapper.id='popup-rating';

	//wrapper.style.left=e.pageX+'px';
	wrapper.style.top=getMouseclickY(e)+'px';


	h2.innerHTML=title1;
	wrapper.appendChild(h2);
	p1=p.cloneNode(false);
	p1.innerHTML=heading1.replace('%s','<a href="profiles/'+login+'">'+login+'</a>');
	wrapper.appendChild(p1);

	divGreen=div.cloneNode(false);
	divGreen.id='green-section';
	h3.innerHTML=title2;
	p2=p.cloneNode(false);
	p2.innerHTML=heading2;
	divGreen.appendChild(h3);
	divGreen.appendChild(p2);
	wrapper.appendChild(divGreen);

	divBottom=div.cloneNode(false);
	divBottom.id='bottom-section';
	p.innerHTML=heading3.replace('%s',time);
	divBottom.appendChild(p);

	th=table.insertRow(0);

	for(i=1;i<11;i++){
		cell=th.insertCell(i-1);
		cell.innerHTML=i.toString();
	}

	input.name='commentRating';
	input.type='radio';

	tr=table.insertRow(1);

	for(i=1;i<11;i++){
		cell=tr.insertCell(i-1);
		//radio=input.cloneNode(false);
		//radio.value=i.toString();
		//radio.onclick=function(){this.checked=true;}
		cell.innerHTML='<input type="radio" name="commentRating" value="'+i.toString()+'" />';
		//cell.appendChild(radio);
	}

	fieldset.appendChild(table);

	textarea.name='commentBody';
	fieldset.appendChild(textarea);

	//adding some hidden fields
	wishID=hidden.cloneNode(false);
	wishID.name='wishID';
	wishID.value=id.toString();
	fieldset.appendChild(wishID);




	a.href = 'javascript:void(null)';
	a.onclick=function(){processRating(this.parentNode.parentNode);hideRatingPopup();}
	a.className = 'popup-btn-yellow';
	a.innerHTML = '<big>' + button + '</big>';
	fieldset.appendChild(a);

	aCancel = document.createElement('a');
	aCancel.href = 'javascript: hideRatingPopup()';
	aCancel.className = 'cancel';
	aCancel.innerHTML = cancel;
	fieldset.appendChild(aCancel);

	form.appendChild(fieldset);
	divBottom.appendChild(form);

	wrapper.appendChild(divBottom);


	document.body.appendChild(wrapper);
}

function hideRatingPopup(){
	var div = document.getElementById('popup-rating');
	if (div){
		div.parentNode.removeChild(div);
	}
	div = document.getElementById('background-gray');
	if (div){
		div.parentNode.removeChild(div);
	}
}


/*** popup fullfill *******************************************************/


function showFullfillPopup(id,title1,heading1,title2,heading2,heading3,login,button,cancel,e){
	if (!e) var e = window.event;

	showBackgroundGray();
	var div = document.getElementById('popup-fullfill');
	if(div){
		div.parentNode.removeChild(div);
	}


	id=unescape(id);
	title1=unescape(title1);
	heading1=unescape(heading1);
	title2=unescape(title2);
	heading2=unescape(heading2);
	heading3=unescape(heading3);
	login=unescape(login);
	button=unescape(button);
	cancel=unescape(cancel);

	//preparing everything
	var div = document.createElement('div');
	var a = document.createElement('a');
	var p = document.createElement('p');
	var h2 = document.createElement('h2');
	var h3 = document.createElement('h2');
	var form=document.createElement('form');
	var input=document.createElement('input');
	var fieldset=document.createElement('fieldset');
	var hidden=document.createElement('input');
	hidden.className='hidden';

	//starting our dirty business
	wrapper=div.cloneNode(false);
	wrapper.id='popup-fullfill';

	//wrapper.style.left=e.pageX+'px';
	wrapper.style.top=getMouseclickY(e)+'px';

	h2.innerHTML=title1;
	wrapper.appendChild(h2);
	p1=p.cloneNode(false);
	p1.innerHTML=heading1.replace('%s','<a href="profiles/'+login+'">'+login+'</a>');
	wrapper.appendChild(p1);

	divGreen=div.cloneNode(false);
	divGreen.id='green-section';
	h3.innerHTML=title2;
	p2=p.cloneNode(false);
	p2.innerHTML=heading2;
	divGreen.appendChild(h3);
	divGreen.appendChild(p2);
	wrapper.appendChild(divGreen);

	divBottom=div.cloneNode(false);
	divBottom.id='bottom-section';
	p.innerHTML=heading3.replace('%s','<a href="profiles/'+login+'">'+login+'</a>');
	divBottom.appendChild(p);


	//adding some hidden fields
	wishID=hidden.cloneNode(false);
	wishID.name='wishID';
	wishID.value=id.toString();
	fieldset.appendChild(wishID);




	a.href = 'javascript:void(null)';
	a.onclick=function(){processFullfill(this.parentNode.parentNode);hideFullfillPopup();}
	a.className = 'popup-btn-violet';
	a.innerHTML = '<big>' + button + '</big>';
	fieldset.appendChild(a);

	aCancel = document.createElement('a');
	aCancel.href = 'javascript: hideFullfillPopup()';
	aCancel.className = 'cancel';
	aCancel.innerHTML = cancel;
	fieldset.appendChild(aCancel);

	form.appendChild(fieldset);
	divBottom.appendChild(form);

	wrapper.appendChild(divBottom);


	document.body.appendChild(wrapper);
}



function hideFullfillPopup(){
	var div = document.getElementById('popup-fullfill');
	if (div){
		div.parentNode.removeChild(div);
	}
	div = document.getElementById('background-gray');
	if (div){
		div.parentNode.removeChild(div);
	}
}




/*** universal popup ***/
function showUniPopup(title,content,button,buttonOnClick,cancel,e){
	//if (!e) var e = window.event;
	content=unescape(content);

	showBackgroundGray();
	var div = document.getElementById('uniPopUp');
	if(div){
		div.parentNode.removeChild(div);
	}


	title=unescape(title);
	content=unescape(content);
	button=unescape(button);
	buttonOnClick=unescape(buttonOnClick);
	cancel=unescape(cancel);

	//preparing everything
	var div = document.createElement('div');
	var a = document.createElement('a');
	var p = document.createElement('p');
	var h2 = document.createElement('h2');
	var h3 = document.createElement('h2');
	var form=document.createElement('form');
	var input=document.createElement('input');
	var fieldset=document.createElement('fieldset');
	var hidden=document.createElement('input');
	hidden.className='hidden';

	//starting our dirty business
	wrapper=div.cloneNode(false);
	wrapper.id='uniPopUp';

	wrapperTop=div.cloneNode(false);
	wrapperTop.className='top';
	wrapper.appendChild(wrapperTop);

	//wrapper.style.left=e.pageX+'px';
	if(e) wrapper.style.top=getMouseclickY(e)+'px';

	h2.innerHTML=title;
	wrapper.appendChild(h2);

	divContent=div.cloneNode(false);
	divContent.id='uniPopUpContent';
	divContent.innerHTML=content;
	wrapper.appendChild(divContent);

	a.href = 'javascript:void(null);';
	a.onclick=function(){eval(buttonOnClick+'(this)');}
	a.className = 'popup-btn';
	a.innerHTML = '<big>' + button + '</big>';
	wrapper.appendChild(a);

	aCancel = document.createElement('a');
	aCancel.href = 'javascript: hideUniPopup()';
	aCancel.className = 'cancel';
	aCancel.innerHTML = cancel;
	wrapper.appendChild(aCancel);

	wrapperBottom=div.cloneNode(false);
	wrapperBottom.className='bottom';
	wrapper.appendChild(wrapperBottom);

	document.body.appendChild(wrapper);
}



function hideUniPopup(){
	var div = document.getElementById('uniPopUp');
	if (div){
		div.parentNode.removeChild(div);
	}
	div = document.getElementById('background-gray');
	if (div){
		div.parentNode.removeChild(div);
	}
}




/*********************************** questionaire event handler ****************/
function processQuestionaireSubmission(frm){
	submit=formData2QueryString(frm);
	xmlhttpGet("questionaire.ajax.php", submit, "callbackWrapper",'login-section',1);
	return false;
}

function questionaireSubmit(caller){
	frm=caller.parentNode.getElementsByTagName('form')[0];
	processQuestionaireSubmission(frm);
	hideUniPopup();
}

/*********************************** abuse event handler ****************/

function processAbuseReport(frm){
	submit=formData2QueryString(frm);
	submit+='&callbackURL='+location.href;
	xmlhttpGet("wish.abuse.ajax.php", submit, "callbackWrapper",'login-section',1);
	return false;
}

function abuseReport(caller){
	frm=caller.parentNode.getElementsByTagName('form')[0];
	processAbuseReport(frm);
	hideUniPopup();
}



/*********************************** copy wish popup **********************/

function showCopyWishPopup(id,title,content,button1,button2,cancel,e){
	
	//getting the website_url of this site
	var website_url=document.getElementsByTagName('base')[0].href;
	
	//if (!e) var e = window.event;
	content=unescape(content);

	showBackgroundGray();
	var div = document.getElementById('uniPopUp');
	if(div){
		div.parentNode.removeChild(div);
	}


	title=unescape(title);
	content=unescape(content);
	button1=unescape(button1);
	button2=unescape(button2);
	cancel=unescape(cancel);

	//preparing everything
	var div = document.createElement('div');
	var a = document.createElement('a');
	var p = document.createElement('p');
	var h2 = document.createElement('h2');
	var h3 = document.createElement('h2');
	var form=document.createElement('form');
	var input=document.createElement('input');
	var fieldset=document.createElement('fieldset');
	var hidden=document.createElement('input');
	hidden.className='hidden';

	//starting our dirty business
	wrapper=div.cloneNode(false);
	wrapper.id='uniPopUp';
	wrapper.className='popup500';

	wrapperTop=div.cloneNode(false);
	wrapperTop.className='top';
	wrapper.appendChild(wrapperTop);

	//wrapper.style.left=e.pageX+'px';
	if(e) wrapper.style.top=getMouseclickY(e)+'px';

	h2.innerHTML=title;
	wrapper.appendChild(h2);

	divContent=div.cloneNode(false);
	divContent.id='uniPopUpContent';
	divContent.innerHTML=content;
	wrapper.appendChild(divContent);

	btn1=a.cloneNode(false);
	btn1.href = website_url+'my/copy/'+id+'/';
	btn1.className = 'popup-btn btn1';
	btn1.innerHTML = '<big>' + button1 + '</big>';
	wrapper.appendChild(btn1);

	
	btn2=a.cloneNode(false);
	btn2.href = website_url+'my/clone/'+id+'/';
	btn2.className = 'popup-btn btn2';
	btn2.innerHTML = '<big>' + button2 + '</big>';
	wrapper.appendChild(btn2);

	aCancel = document.createElement('a');
	aCancel.href = 'javascript: hideUniPopup()';
	aCancel.className = 'cancel';
	aCancel.innerHTML = cancel;
	wrapper.appendChild(aCancel);

	wrapperBottom=div.cloneNode(false);
	wrapperBottom.className='bottom';
	wrapper.appendChild(wrapperBottom);

	document.body.appendChild(wrapper);
	
	
}





/************************************ void ****************************************/
function moveFooterToBottom(){
	var footer = document.getElementById('footer');
	var bodyHeight = document.body.clientHeight;
	var fHeight = footer.offsetHeight;
	var rightTop = bodyHeight - fHeight;
	footer.style.position = 'absolute';
	footer.style.top = rightTop + 'px';
}




function placeholderMessage(input,msg,focus){
	if(focus){
		//on focus
		input.value=(input.value==msg)?'':input.value;
	}
	else{
		//on fade
		input.value=(input.value=='')?msg:input.value;

	}
}

function placeholderPassword(input,msg,focus){
	if(focus){
		//on focus
		input.value=(input.value==msg)?'':input.value;
		input.type='password';
	}
	else{
		//on fade
		if(input.value==''){
			input.value=msg;
			input.type='text';
		}

	}
}

