/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*                                                                            *
*      COMMON JavaScript file.                                               *
*                                                                            *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */



function subWin(url, x, y, bln, blnScrBar) {
	var subWin = window.open(url,'','width=' + x + ',height=' + y + ',scrollbars=' + blnScrBar + ',top=100,left=100,resizable=' + bln + ',menubar=' + bln + ',toolbar=' + bln + ',status=' + bln + ',location=' + bln);
}

function winLink(url) {
	subWin(url, 600, 500, 1, 1);
}

function winTerms() {
	subWin('http://www.genebase.com/terms.php?is_special=1', 450, 410, 1, 1);
}

function winYDNAImportForm() {
	subWin('/my/ydna_import_form.php', 650, 650, 1, 1);
}

function winContact() {
	subWin('/lib/popup_contact.php?ref=' + encodeURI(window.location.href), 450, 410, 0);
}

function winPasswordReminder() {
	subWin('/lib/password_reminder.php', 450, 300, 0);
}

function winFeedback() {
	subWin('/lib/popup_feedback.php?ref=' + encodeURI(window.location.href), 450, 400, 0);
}

function winWhatIsDNA() {
	subWin('/what_is_dna.php', 650, 335, 0);
}

function winSPCaseStudy() {
	subWin('/sp_case_study.php', 650, 335, 0);
}

function winTestSteps() {
	subWin('/test_steps.php', 650, 335, 0);
}

function winPhotoSender(photoID) {
	subWin('/lib/photo_sender.php?id=' + photoID, 450, 400, 0);
}

function winRecommendToFriend() {
	subWin('/lib/recommend_to_friend.php?ref=' + encodeURI(window.location.href), 450, 400, 0);
}

function winReportAbuse(id) {
	subWin('/lib/report_abuse.php?id=' + id + '&ref=' + encodeURI(window.location.href), 560, 500, 0);
}

function sendMail(recipient) {
	subWin('/send_gb_mail.php?to=' + recipient, 470, 480, 0);
}

function addToBuddyList(user_id) {
	subWin('/lib/add_buddy.php?id=' + user_id,  250, 150, 0);
}

function cfmReset() {
	return confirm('Do you really want to discard all the changes you made?');
}

function cfmSubmit() {
	return confirm('This action is "final" and cannot be redone. Are you sure you want to proceed?');
}

function emptyIfValueIs(ele, elementValue) {
	//var ele = document.getElementById(elementID);
	if (ele.value == elementValue) {
		ele.value = '';
	}
}

function checkAll(nameEle, blnFlag) {
	var ele = document.getElementsByName(nameEle);
	for (i = 0; i < ele.length; i++) {
		if (!ele[i].disabled) {
			ele[i].checked = blnFlag;
		}
	}
}

function getSmiley(ele, smiley) {
	var frm = document.getElementById(ele);
	frm.value = frm.value + ' ' + smiley + ' ';
	frm.focus();
}

function getYCML(ele, tag, prm) {
	
	// Shortcuts to objects.
	var txa = document.getElementById(ele);
	var brw = navigator.appName.toLowerCase();
	
	// If Parameter is set, open tag should reflect that.
	var open_tag = tag;
	if (prm != '') {
		open_tag = tag + '=' + prm;
	}
	
	// Check if the user's browser is MS IE.
	if (brw.substr(0, 9) == 'microsoft') {
		
		// Yes. Get the selected text thing.
		var selectedText = document.selection.createRange().text;
		
		// Check if the selected text is not null.
		if (!selectedText) {
			txa.value += '[' + open_tag + ']Your_Text[/' + tag + ']';
		}
		else {
			// Check if the selected text is a textarea.
			if (document.selection.createRange().parentElement().tagName != 'TEXTAREA') {
				return false;
			}
			// Go enclose'em.
			document.selection.createRange().text = '[' + open_tag + ']' + selectedText + '[/' + tag + ']';
		}
		
	}
	else {
		// Not MS IE. Just append the tags.
		txa.value += '[' + open_tag + ']Your_Text[/' + tag + ']';
	}
	
	// Focus.
	txa.focus();
	
}

function getYCMLURL(ele) {
	var linkURL = prompt('Enter Link URL:', 'http://');
	var linkTitle = prompt('Enter Link Title:', linkURL);
	var txa = document.getElementById(ele);
	if (linkTitle == '') {
		linkTitle = linkURL;
	}
	txa.value += '[url=' + linkURL + ']' + linkTitle + '[/url]';
	txa.focus();
}

function getYCMLIMG(ele) {
	var photoPickerWin = subWin('/lib/photo_picker.php?element_id=' + ele, 610, 580, 0, 1);
}

function setTo(to_user) {
	// Reference to the form's To field.
	var frm = document.frmCompose.To;
	// Initialize the delimitter.
	var delim = ', ';
	// If there is no To entry at all OR to_user is empty, do not add the delimitter.
	if (!frm.value || !to_user) {
		delim = '';
	}
	// Append the user with/without the delimitter.
	frm.value += delim + to_user;
	
}

function validateComment(elementIDBody) {
	// The only field to validate is the comment body.
	var err = '';
	var maxChars = 2000;
	var txaCommentBody = document.getElementById(elementIDBody);
	if (txaCommentBody.value == '') {
		err += 'Comment Body is required.';
	}
	else if (txaCommentBody.value.length > maxChars) {
		err += 'Please keep Comment Body under ' + maxChars + ' characters.' + "\n";
	}
	
	if (err == '') {
		return true;
	}
	else {
		alert(err);
		return false;
	}
}

function setVisibility(eleName, cssValue) {
	document.getElementById(eleName).style.visibility = cssValue;
}

function setDisplay(eleName, cssValue) {
	document.getElementById(eleName).style.display = cssValue;
}

function colorPicker(pickFor, previewIn, imgEditing) {
	var x = 199;
	var y = 134;
	var bln = 0;
	winColorPicker = window.open('/lib/color_picker.php?pick_for=' + pickFor + '&preview_in=' + previewIn + '&img_editing=' + imgEditing, 'ColorPicker', 'width=' + x + ',height=' + y + ',scrollbars=' + bln + ',top=100,left=100,resizable=' + bln + ',menubar=' + bln + ',toolbar=' + bln + ',status=' + bln + ',location=' + bln);
	if (imgEditing != '') {
		document.getElementById(imgEditing).src = '/template/img/now_editing.gif';
	}
	winColorPicker.focus();
}


/*  */
function getWorksheetEntries() {
	var worksheetEntries = document.getElementsByName('worksheet_entries[]');
	var getParam = '';
	for (i = 0; i < worksheetEntries.length; i++) {
		if (worksheetEntries[i].checked) {
			getParam += '_' + worksheetEntries[i].value;
		}
	}
	return getParam.substring(1);
}


function switchFeatured(type, ranking, interval, isPrev) {
	
	// IE.
	if (window.ActiveXObject) {
		var objHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	// Others.
	else if (document.implementation) {
		var objHttpReq = new XMLHttpRequest();
	}
	
	objHttpReq.open('get', '/lib/js_switchFeatured.php?type=' + type + '&ranking=' + ranking + '&interval=' + interval + '&is_prev=' + isPrev);
	objHttpReq.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
	objHttpReq.send(null);
	
	objHttpReq.onreadystatechange = function() {
		if (objHttpReq.readyState == 4) {
			PHPResult = objHttpReq.responseText.split('}+{');
			document.getElementById('currentRanking').value = PHPResult[0];
			
			for (i = 1; i <= interval; i++) {
				featuredInnerHTML = PHPResult[i].split('}-{');
				switch (type) {
					case 'fu':
					case 'fp':
						document.getElementById('featuredHTML' + i).innerHTML = '<div style="margin: 0 0 10px ! important; font-weight: bold ! important; ">' + featuredInnerHTML[2] + '</div><a href="' + featuredInnerHTML[1] + '"><img src="' + featuredInnerHTML[0] + '" alt="" style="float: right ! important; margin-left: 10px ! important; border: 1px solid #CCCCCC ! important;" /></a><a href="' + featuredInnerHTML[1] + '" style="color: #999999 ! important;">' + featuredInnerHTML[3] + '</a>';
						break;
					case 'fg':
						document.getElementById('featuredHTML' + i).innerHTML = '<a href="' + featuredInnerHTML[1] + '"><img src="' + featuredInnerHTML[0] + '" alt="" style="margin: 0px 10px 0px 0px; float: left; border: 1px solid #CCCCCC;" /></a><strong><a href="' + featuredInnerHTML[1] + '">' + featuredInnerHTML[2] + '</a></strong><br /><a href="' + featuredInnerHTML[1] + '">' + featuredInnerHTML[3] + '</a>';
						break;
				}
			}
			
		}
		return false;
	}
	
}


function switchFeaturedSet(ranking, interval, isPrev) {
	
	// IE.
	if (window.ActiveXObject) {
		var objHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	// Others.
	else if (document.implementation) {
		var objHttpReq = new XMLHttpRequest();
	}
	
	
	objHttpReq.open('get', '/lib/js_switchFeaturedSet.php?current_ranking=' + ranking + '&interval=' + interval + '&is_prev=' + isPrev);
	objHttpReq.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
	objHttpReq.send(null);
	
	objHttpReq.onreadystatechange = function() {
		if (objHttpReq.readyState == 4) {
			PHPResult = objHttpReq.responseText.split('}+{');
			featuredInnerHTML = PHPResult[0].split('}-{');
			document.getElementById('featuredUserHTML').innerHTML = '<a href="' + featuredInnerHTML[1] + '"><img src="' + featuredInnerHTML[0] + '" alt="" style="float: right; margin-left: 10px; border: 1px solid #CCCCCC;" /></a><a href="' + featuredInnerHTML[1] + '" style="color: #999999; "><strong>' + featuredInnerHTML[2] + '</strong><br />' + featuredInnerHTML[3] + '</a>';
			featuredInnerHTML = PHPResult[1].split('}-{');
			document.getElementById('featuredProjectHTML').innerHTML = '<a href="' + featuredInnerHTML[1] + '"><img src="' + featuredInnerHTML[0] + '" alt="" style="float: right; margin-left: 10px; border: 1px solid #CCCCCC;" /></a><a href="' + featuredInnerHTML[1] + '" style="color: #999999; "><strong>' + featuredInnerHTML[2] + '</strong><br />' + featuredInnerHTML[3] + '</a>';
			featuredInnerHTML = PHPResult[2].split('}-{');
			document.getElementById('featuredGroupHTML').innerHTML = '<a href="' + featuredInnerHTML[1] + '"><img src="' + featuredInnerHTML[0] + '" alt="" style="float: right; margin-left: 10px; border: 1px solid #CCCCCC;" /></a><a href="' + featuredInnerHTML[1] + '" style="color: #999999; "><strong>' + featuredInnerHTML[2] + '</strong><br />' + featuredInnerHTML[3] + '</a>';
			document.getElementById('currentRanking').value = PHPResult[3];
		}
		return false;
	}
	
}

function winShipBeforeChristas() {
	subWin('http://www.dnaancestryproject.com/shipBeforeChristmas.php', 700, 350, 0);
}



//*******Image Editor fucntions*****************************//
// Though "Dialog" looks like an object, it isn't really an object.  Instead
// it's just namespace for protecting global symbols.

function Dialog(url, action, init) {
	if (typeof init == "undefined") {
		init = window;	// pass this window object by default
	}
	Dialog._geckoOpenModal(url, action, init);
};

Dialog._parentEvent = function(ev) {
	setTimeout( function() { if (Dialog._modal && !Dialog._modal.closed) { Dialog._modal.focus() } }, 50);
	if (Dialog._modal && !Dialog._modal.closed) {
		Dialog._stopEvent(ev);
	}
};


// should be a function, the return handler of the currently opened dialog.
Dialog._return = null;

// constant, the currently opened dialog
Dialog._modal = null;

// the dialog will read it's args from this variable
Dialog._arguments = null;

Dialog._geckoOpenModal = function(url, action, init) {
	//var urlLink = "hadialog"+url.toString();
	var myURL = "hadialog"+url;
	var regObj = /\W/g;
	myURL = myURL.replace(regObj,'_');
	var dlg = window.open(url, myURL,
			      "toolbar=no,menubar=no,personalbar=no,width=10,height=10," +
			      "scrollbars=no,resizable=yes,modal=yes,dependable=yes");
	Dialog._modal = dlg;
	Dialog._arguments = init;

	// capture some window's events
	function capwin(w) {
		Dialog._addEvent(w, "click", Dialog._parentEvent);
		Dialog._addEvent(w, "mousedown", Dialog._parentEvent);
		Dialog._addEvent(w, "focus", Dialog._parentEvent);
	};
	// release the captured events
	function relwin(w) {
		Dialog._removeEvent(w, "click", Dialog._parentEvent);
		Dialog._removeEvent(w, "mousedown", Dialog._parentEvent);
		Dialog._removeEvent(w, "focus", Dialog._parentEvent);
	};
	capwin(window);
	// capture other frames
	for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));
	// make up a function to be called when the Dialog ends.
	Dialog._return = function (val) {
		if (val && action) {
			action(val);
		}
		relwin(window);
		// capture other frames
		for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
		Dialog._modal = null;
	};
};


// event handling

Dialog._addEvent = function(el, evname, func) {
	if (Dialog.is_ie) {
		el.attachEvent("on" + evname, func);
	} else {
		el.addEventListener(evname, func, true);
	}
};


Dialog._removeEvent = function(el, evname, func) {
	if (Dialog.is_ie) {
		el.detachEvent("on" + evname, func);
	} else {
		el.removeEventListener(evname, func, true);
	}
};


Dialog._stopEvent = function(ev) {
	if (Dialog.is_ie) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
};

Dialog.agt = navigator.userAgent.toLowerCase();
Dialog.is_ie	   = ((Dialog.agt.indexOf("msie") != -1) && (Dialog.agt.indexOf("opera") == -1));












function editImage(image) 
{
	var url = "my/imgeditor/imgeditor.php?img="+image;
	Dialog(url, function(param) 
	{
		if (!param) // user must have pressed Cancel
			return false;
		else
		{
			return true;
		}
	}, null);		
}



//*******End Image Editor fucntions*****************************//
