var searchFlag = 0;

var serverUrl = 'http://www.mumzone.com.au/';

var isSection = 0;

function setFontStyle(obj, isSetFlag){

	if(obj.value != "" && obj.value != "Enter name to see meaning"){

		if(isSetFlag) obj.select();

		obj.style.color = '#000000';

		return false;

	}

	if(isSetFlag){

		obj.style.color = '#000000';

		obj.value = "";

	}else{

		obj.style.color = '#A79393';

		obj.value = "Enter name to see meaning";

	}

}



function searchNames(e){

	if(e.keyCode == 13){

		simpleSearch();

	}

}



$(document).ready(function() {

	jQuery('#searchResultTbl :checkbox').checkbox({cls: 'jquery-safari-checkbox'});

});


function moveRightItem(obj){

	if(obj.options[obj.selectedIndex].value) { 

		var tmp1,tmp2, tmpObj; 

		tmp1 = obj.options[obj.selectedIndex].text; 

		tmp2= obj.options[obj.selectedIndex].value;					

		tmpObj = document.getElementById("selectedOrigin");

		createOtionElement(tmp1, tmp2, tmpObj);

	}

}


function moveLeftItem(obj){

	obj.options[obj.selectedIndex] = null;

}


function selectOriginItem(){

	var obj = document.getElementById("selectOrigin");

	if(obj.selectedIndex < 0){

		alert("Please select name origin.");

		return false;

	}

	moveRightItem(obj);

}

function deselectOriginItem(){

	var obj = document.getElementById("selectedOrigin");

	if(obj.length == 0) return false;

	if(obj.selectedIndex < 0){

		alert("Please select name origin.");

		return false;

	}

	moveLeftItem(obj);

}



function createOtionElement(objText, objValue, parentObj){

	var opt = new Option("option");

	opt.text = objText;

	opt.value = objValue;

	if(!checkObjValue(parentObj, objValue)){

		return false;

	}

	parentObj.options[parentObj.length] = opt;

}



function fn_get(obj){

	var s= "";

	for(obj1 in obj){

		s += obj1 + "<br>\n";

	}

	return s;

}	



function checkObjValue(obj, objValue){

	for(i = 0; i < obj.length; i++){

		if(obj[i].value == objValue){

			return false;

		}

	}

	return true;

}



function simpleSearch(){

	if($("#babyNameTxt").val() == "Enter name to see meaning"){

		return false;

	}else{

		searchFlag = 0;

		$("#searchStr").val($("#babyNameTxt").val());

		$("#currentPage").val($("#nowPages").val());

		$("#processFlag").val("simpleSearch");

		document.getElementById("dataForm").action = serverUrl + "baby-names/" + $("#searchStr").val();

		document.getElementById("dataForm").submit();

	}

}

function addFavoritesItem(priKey, section){

	isSection = section;

	if(priKey == ""){		

		return;

	}

	var favoritesStr = priKey;	

	if(!isCheck){

		$(document).ready(function(){

			centerPopup(0);

			loadPopup();

			$("#userMail").focus();

		});

		return;

	}

	if(confirm("Are you sure to add the selected names to favourite?")){

		$("#searchStr").val(favoritesStr);

		$("#processFlag").val("addFavorite");

			$.ajax({

		        type: 'post'

		        , async: true

		        , url: serverUrl + 'baby-names/common/common.ctrl.php'

		       	, data: $("#dataForm").serialize() 

		        , success: function(data) {

//						alert(data);

					alert("Saved successfully.");

//					document.getElementById("dataForm").action = "../favorite_list.php";

//					document.getElementById("dataForm").submit();

					window.open(serverUrl + "baby-names/favorite_list.php","_self");

		          }

		        , error: function(data, status, err) {				        	

		            alert('Failed connection to server');

		          }

		        , complete: function() { 

		        	

		          }

		    });			

//			document.getElementById("dataForm").action = '../login.php';

//			document.getElementById("dataForm").submit();		

	}

}

function addFavorites(section){

	isSection = section;

	var favoritesStr = "";

	if(!isCheck){

		$(document).ready(function(){

			centerPopup(0);

			loadPopup();

			$("#userMail").focus();

		});

		return false;

	}

	$('#searchResultTbl input').each(function(){

		var el = $(this);

		if(this.checked){

			if(favoritesStr == ""){

				favoritesStr = this.value;

			}else{

				favoritesStr += "_@@@_" + this.value;

			}

		}	

	});

	if(favoritesStr == ""){

		alert("Please select baby names");

		return false;

	}else{

		if(confirm("Are you sure to add the selected names to favourite?")){

			$("#searchStr").val(favoritesStr);

			$("#processFlag").val("addFavorite");			

			$.ajax({

		        type: 'post'

		        , async: true

		        , url: serverUrl + 'baby-names/common/common.ctrl.php'

		       	, data: $("#dataForm").serialize() 

		        , success: function(data) {

//						alert(data);

					alert("Saved successfully.");

					window.open(serverUrl + "baby-names/favorite_list.php","_self");

		          }

		        , error: function(data, status, err) {				        	

		            alert('Failed connection to server');

		          }

		        , complete: function() { 

		        	

		          }

		    });	

		}

//		alert(favoritesStr);

	}

}



function deleteFavorites(){

	var favoritesStr = "";

	$('#searchResultTbl input').each(function(){

		var el = $(this);

		if(this.checked){

			if(favoritesStr == ""){

				favoritesStr = this.value;

			}else{

				favoritesStr += "_@@@_" + this.value;

			}

		}	

	});

	if(favoritesStr == ""){

		alert("Please select baby names");

		return false;

	}else{

		if(confirm("Are you sure to delete the selected names from favourite?")){

			$("#searchStr").val(favoritesStr);

			$("#processFlag").val("deleteFavorite");

			$.ajax({

		        type: 'post'

		        , async: true

		        , url: serverUrl + 'baby-names/common/common.ctrl.php'

		       	, data: $("#dataForm").serialize() 

		        , success: function(data) {

					

					alert("Deleted successfully.");

					location.reload();

		          }

		        , error: function(data, status, err) {				        	

		            alert('Failed connection to server');

		          }

		        , complete: function() { 

		        	

		          }

		    });

		}

	}

}


function detailSearch(){

	var checkGirl = document.getElementById("checkGirl");

	var checkBoys = document.getElementById("checkBoys");

	var selSearchType = document.getElementById("selSearchType");

	var selectedOrigin = document.getElementById("selectedOrigin");

	if(selSearchType.value == "Search by first letter of the name" && selectedOrigin.length < 1 && !checkGirl.checked && !checkBoys.checked){

		alert("You need to make a selection.");

		return false;

	}else{

		searchFlag = 1;

		if(selSearchType.value == "Search by first letter of the name"){

			var tmpStr = "";

		}else{

			var tmpStr = selSearchType.value;

		}

		var selStr = "";

		var count = 0;

		for(i = 0; i < selectedOrigin.length; i++){

			if(selStr == ""){

				selStr = selectedOrigin[i].value;

			}else{

				selStr += "_COL_" + selectedOrigin[i].value;

			}

			count++;

		}

		

		tmpStr += "_ROW_" + selStr;

		if(checkGirl.checked){

			girlStr = "gender='female'";

		}else{

			girlStr = "";

		}

		

		if(checkBoys.checked){

			boyStr = "gender='male'";

		}else{

			boyStr = "";

		}

		

		tmpStr += "_ROW_" + girlStr;

		tmpStr += "_ROW_" + boyStr;

		$("#searchStr").val(tmpStr);

		$("#sortStr").val("");

		$("#currentPage").val($("#nowPages").val());

		$("#processFlag").val("detailSearch");

		

		serachUrl = "";

		if(count < 2){

			tmpStr = selStr.toLowerCase();

			tmpStr = tmpStr.replace(" ", "");

			tmpStr = tmpStr.replace(",", "");

			serachUrl = tmpStr + "-baby-names";

		}else{

			serachUrl = "baby-names-results";

		}

		

		document.getElementById("dataForm").action = serverUrl + "baby-names/" + serachUrl;

		document.getElementById("dataForm").submit();

	}

}

function setFontStyle1(obj, isSetFlag){

	if(obj.value != "" && obj.value != "Search by first letter of the name"){

		obj.style.color = '#000000';

		if(isSetFlag) obj.select();

		return false;

	}

	if(isSetFlag){

		obj.style.color = '#000000';

		obj.value = "";

	}else{

		obj.style.color = '#A79393';

		obj.value = "Search by first letter of the name";

	}

}



this.tooltip = function(){	

	/* CONFIG */		

		xOffset = 10;

		yOffset = 20;		

		// these 2 variable determine popup's distance from the cursor

		// you might want to adjust to get the right result		

	/* END CONFIG */		

	$("td.tooltip").hover(function(e){	

		if(this.title == "")return;

		this.t = this.title;

		this.title = "";					

		if((this.t).length < 50){

			width = (this.t).length + 100;

		}else if((this.t).length > 100){

			width = this.width;

		}else{

			width = this.width;

		}

		$("body").append("<p id='tooltip' style='width:" + width + "'>"+ this.t +"</p>");

		$("#tooltip")

			.css("top",(e.pageY + xOffset) + "px")

			.css("left",(e.pageX + yOffset) + "px")

			.fadeIn("fast");		

    },

	function(){

		this.title = this.t;		

		$("#tooltip").remove();

    });	

	$("td.tooltip").mousemove(function(e){

		$("#tooltip")

			.css("top",(e.pageY + xOffset) + "px")

			.css("left",(e.pageX + yOffset) + "px");

	});			

};







// starting the script on page load

$(document).ready(function(){

	tooltip();

    selectTr();

//    centerPopup(0);

//	loadPopup();

    $("#popupContactClose").click(function(){

		disablePopup();

	});

	$("#backgroundPopup").click(function(){

		disablePopup();

	});

	$(document).keypress(function(e){

		if(e.keyCode==27 && popupStatus==1){

			disablePopup();

		}

	});

});



function pageMoves(pageNum, getFlag){

	$("#nowPages").val(pageNum);

	if(getFlag == "simple"){

		simpleSearch();

	}else{

		detailSearch();

	}	

}



this.selectTr = (function(){

	$("tr.selectTr").hover(function(e){	

		this.style.backgroundColor = "#CCFFCC";

    });	

	$("tr.selectTr").mouseout(function(e){

		this.style.backgroundColor = "white";

	});			

});



function detailView(id, name){

	$("#priKey").val(id);

	name = name.replace(" ","-");

	$("#id").val(name);	

	document.getElementById("dataForm").action = serverUrl + "baby-names/meaning/" + $("#id").val();

	document.getElementById("dataForm").submit();

}



function logOut(){

	$("#processFlag").val("logOut");

	$.ajax({

        type: 'post'

        , async: true

        , url: serverUrl + 'baby-names/common/common.ctrl.php'

       	, data: $("#dataForm").serialize() 

        , success: function(data) {			

			window.open(serverUrl + 'baby-names.php','_self')

          }

        , error: function(data, status, err) {				        	

            alert('Failed connection to server');

          }

        , complete: function() { 

        	

          }

    });

}
