var keepMenu = 0;
var timer1;

function checkLocalLogin(isLoggedIn) {
	if (isLoggedIn || verify_pass('loginForm', 'username', 'passwd')) {
		document.getElementById('loginForm').submit();
	} else {
		alert("Error on form, contact the hotladies dev center.");
	}
}

function verify_pass(form_id, uname, passwd) {
	var form = document.getElementById(form_id);
	var user = document.getElementById(uname);
	var pass = document.getElementById(passwd);
	if (form && user && pass) {
		if (pass.value.length == 32 ) {
			return true;
		} else if (form && user && pass ) {
			pass.value = get_pass(user.value, pass.value);
			//alert(pass.value);
			return true;
		}
	}
	return false;
}

function delete_thumb(fname){
//	alert(fname);
	if (fname && confirm("Are you sure you want to delete this thumb?")) {
		var el = document.getElementById("action_ctrl");
		var ffile = document.getElementById("fname");
		var thumbForm = document.getElementById("thumbForm");
		
		if (el && ffile && thumbForm) {
			el.value = "delete";
			ffile.value = fname;
			thumbForm.submit();
			return true;
		} 
	} 
	return false;
}

function delete_item(id, formId, msg) {
	if (!msg) msg = "Do you want to delete this item (id = " + id + ")?";
	input_box=confirm(msg);
	if (input_box==true) {
		var el = document.getElementById(formId);
		if (!el) alert("Warning! Wrong form ID!");
		else {
			if (el.action_ctrl) el.action_ctrl.value = "delete";
			if (el.primary) el.primary.value = id;
			el.submit();
		}
	}
}

function edit_item(id, formId) {
	var el = document.getElementById(formId);
	if (el) {
		if (el.action_ctrl) el.action_ctrl.value = "edit";
		if (el.primary) el.primary.value = id;
		el.submit();
	}
}

function add_item(formId) {
	var el = document.getElementById(formId);
	if (el) {
		if (el.action_ctrl) el.action_ctrl.value = "add";
		el.submit();
	}
}
function view_model(model_name, formId) {
	var el = document.getElementById(formId);
	if (el) {
		if (el.action_ctrl) el.action_ctrl.value = "view";
		if (el.model) el.model.value = model_name;
		el.submit();
	}
}

function verify_submit(obj_name){
	var el = document.getElementById(obj_name);
	if (!el) {
		return false;
	} else {
		switch (el.type) {
			case "checkbox":
				if (!el.checked) alert(el.alt);
				return el.checked;
				break;
			case "text":
				return true;
				break;
		}
		return false;
	}
}

function check_username(obj_name, form_id, activate_obj){
	
	var el = document.getElementById(obj_name);
	if (!el) {
		return false;
	} else {
		if (el.type != "text" || !el.value) {
			alert(el.alt);
			return false;
		} else {
			
			try {
				if (activate_obj) activate_obj.checked = true;
			} catch (err) {
			} 
			if (form_id) {
				el = document.getElementById(form_id);
				if (el.action_ctrl) el.action_ctrl.value = "check_username";
				el.submit();
			} 
		}
	}
}

function postBBCountry(){
	var form = document.getElementById('blackBookForm');
	if (form) {
		form.submit();
	}
}

function deleteAdminProfile(id){
	var form = document.getElementById('adminSearchForm');
	var selector2 = document.getElementById('selector2');
	if (form && selector2) {
		selector2.value = id;
		form.action_ctrl = "/addminno7931done/delete_profile";
		form.submit();
	}
}

function undeleteAdminProfile(id){
	var form = document.getElementById('adminSearchForm');
	var selector2 = document.getElementById('selector2');
	if (form && selector2) {
		selector2.value = id;
		form.action_ctrl = "/addminno7931done/undelete_profile";
		form.submit();
	}
}

function selectAdminProfile(select, id){
	var form = document.getElementById('adminSearchForm');
	var selector2 = document.getElementById('selector2');
	var form2 = document.getElementById('form_navigationbar');
	if (form) {
		if (select) selector2.value = id;
		else selector2.value = -id;
		if (form2) {
			form.start_page.value = form2.start.value;
		}
		form.submit();
	}
}

function selectAdminBlackBookProfile(select, id){
	var selector3 = document.getElementById('selector3');
	if (selector3) {
		selector3.value = 1;
	}
	selectAdminProfile(select, id);
}

function approveProfile(select, id){
	var form = document.getElementById('approveForm');
	var selector2 = document.getElementById('selector2');
	var reason_id = document.getElementById('reason_' + id);
	var reason = document.getElementById('reason');
	var form2 = document.getElementById('form_navigationbar');
	var approveElem = document.getElementById('reason_id');
	var approveElem1 = document.getElementById('chb_pic_'+id);
	var approveElem2 = document.getElementById('chb_about_'+id);
	
	if (approveElem && approveElem1 && approveElem2) {
		approveElem.value = parseInt(approveElem1.value) + 2*parseInt(approveElem2.value); 
	}
	approveElem = document.getElementById('approveAboutMe');
	approveElem1 = document.getElementById('approveAboutMatch');
	approveElem2 = document.getElementById('about_me');
	var approveElem3 = document.getElementById('about_match');
	
	if (approveElem && approveElem1 && approveElem2 && approveElem3) {
		approveElem.value=approveElem2.value;
		approveElem1.value=approveElem3.value;
		
	}
	
	if (form) {

		if (select) {
			if (select >0 ) {
				selector2.value = id;
			} else {
				if (confirm("Are you sure you want to delete this account?")){
					selector2.value = "delete"+id;
				} else {
					return;
				}
			}
			
		} else {
			selector2.value = -id;

		}
		if (reason && reason_id){
			reason.value = reason_id.value;
		}
		if (form2) {
			form.start_page.value = form2.start.value;
		}
		form.submit();
	}
}

function denyPicture(id){
	approvePicture(id, false);
}

function deletePicture(id){
	approvePicture(id, -1);
}

function approvePicture(id, approve){
	
	var form = document.getElementById('approvePicForm');
	
	var picID = document.getElementById('pictureID');
	var approveElem = document.getElementById('approveInput');
	
	var approveLocal = approve;
	if (approveLocal == null) approveLocal = 1;
	if (form){
		if (picID){
			picID.value = id;
			if (approveElem) {
				approveElem.value = approveLocal;
				form.submit();
			}
		}
		
	}
}

function validateSignup1(param){
	if (param){
		var display;
		if (param == 1) display = "none";
		else display = "block";
		
		var el = document.getElementById("overlay");
		el.style.display = display;
		el = document.getElementById("overlay_window2");
		el.style.display = display;
		var el = document.getElementById("signupForm");
		if (el){
			for(i=0; i<el.elements.length; i++){
				try {
					if (el.elements.item(i).tagName == "INPUT"){
						el.elements.item(i).focus();
						break;
					} 
				} catch (e){
					
				}
			}
		}
		
	} else {
		if (!document.getElementById("agree").checked){
			alert("Please read and agree with our terms and conditions, first.");
			return false;
		} else {
			return true;
		}
	}
}
var alertTimerId = 0;
var lastElement = null;

function validateSignup2(param1, param2, do_submit){
	var el = document.getElementById(param1);
	if (el) el.style.display = "none";
	el = document.getElementById(param2);
	if (el) {
		el.style.display = "block";
		lastElement = el;
	} else {
		lastElement = null;
	}
	if (do_submit){
		validateSignupInterval();
		//alertTimerId = setInterval(validateSignupInterval, 4000);
	}
}

function validateSignupInterval(){
	var el = document.getElementById("signupForm");
	if (alertTimerId){
		clearInterval(alertTimerId);
	}
	if (el) el.submit();
	else {
		if (lastElement) {
			lastElement.style.display = "none";
		}
	}
	
}

function validateSignupSubmit(txt){
	//alert(document.getElementById('uploadedfile').value);
	var el = document.getElementById(txt);
	if (el && (el.value.length < 20)) {
		alert("Please tell us a few more words before continue...");
		return;
	}
	el = document.getElementById('uploadedfile'); 
	if (el && (el.value == '')) {
		validateSignup2('step3', 'step7', true);	
		return false;
	} else {
		validateSignupInterval();
		return true;
	}
}

function validateLocalForm(){
	var item = document.getElementById("about_match");

	if (!item || (item.value=="")) {
		alert("Please say a few words about your match.");
		return false;
	}
	var item2 = document.getElementById('Q[69]["none"]');
	if (!item2) return false;

	ret = false;
	if (item2.checked) {
		document.getElementById('Q[69]["russian"]').checked = false;
		item = document.getElementById('Q[69]["eastern"]').checked = false;
		item = document.getElementById('Q[69]["latin"]').checked = false;
		item = document.getElementById('Q[69]["asian"]').checked = false;
		ret = true; 
	} else {
		item = document.getElementById('Q[69]["russian"]');
		if (item.checked) { 
			ret = true; 
		} else {
			item = document.getElementById('Q[69]["eastern"]');
			if (item.checked) { 
				ret = true; 
			} else {
				item = document.getElementById('Q[69]["latin"]');
				if (item.checked) { 
					ret = true; 
				} else {
					item = document.getElementById('Q[69]["asian"]');
					if (item.checked) { 
						ret = true; 
					}
				}
			}
		}
	}

	if (!ret) {
		alert("You must choose at least a region to receive emails from, or choose \"ALL\"");
	}
	return ret;	 
}

function validateLocalCheckboxes(selectAll){
	var item = document.getElementById('Q[69]["none"]');
	var item1 = document.getElementById('Q[69]["russian"]');
	var item2 = document.getElementById('Q[69]["eastern"]');
	var item3 = document.getElementById('Q[69]["latin"]');
	var item4 = document.getElementById('Q[69]["asian"]');
	if (!item || !item2 || !item3 || !item4 || !item1 ){
		return false;
	}
	if (selectAll){
		if (item.checked) {
			item1.checked = false;
			item2.checked = false;
			item3.checked = false;
			item4.checked = false;
		}
	} else {
		item.checked = false;
	}
	return ret;
}

function popupTerms(){
	popupTermsItem("terms");
}

function popupPrivacy(){
	popupTermsItem("privacy");
}
function popupRefund(){
	popupTermsItem("refund");
}
function popupTermsItem(item){
	var myWindow = window.open("/terms/"+item+"?show", item+"Window", "width=1040, height=300, scrollbars=1");
	myWindow.moveTo(30, 150);
}

function validateLeftSignup(){
	if (!document.getElementById("agree2").checked){
		alert("Please read and agree with our terms and conditions, first.");
		return false;
	}
	return true;
}	

function ungreyObject(parent){
	var el = document.getElementById(parent);
	if (el && el.style.color != "black") {
		el.value = "";
		el.style.color = "black";
	}
}

function emailMe(id){
	var a_el = document.getElementById("a_"+id); //anchor with name
	var s_el = document.getElementById("s_"+id); //span with age and location info
	var p_el = document.getElementById("p_"+id); //photo
	var s_ab = document.getElementById("about_"+id); //about
	
	var email_a = document.getElementById("email_a");
	var email_a2 = document.getElementById("email_a2");
	var email_s = document.getElementById("email_s");
	var email_p = document.getElementById("email_p");
	var email_about = document.getElementById("email_about");
	var composeTA = document.getElementById("composeTA");
	var idx = document.getElementById("idx");
	var subject = document.getElementById("bodyComposeInput");
	var amInFullProfile = false;
	if (a_el && s_el && email_a && email_s && email_p && s_ab && email_about && composeTA && subject){
		idx.value = id;
		var userName = a_el.innerHTML;
		if (userName.indexOf("<b>") == 0) {
			userName = userName.substr(3, userName.length-7);
			
		}
		subject.value = "Hi " + userName;
		if (!p_el) {
			p_el = document.getElementById("pic_cell_"+id+"_1"); //viewprofile
			amInFullProfile = true;
			email_a2.href = location.href;
		} else {
			email_a2.href = p_el.parentNode.href;
		}
		
		email_a.innerHTML = " \""+a_el.innerHTML + "\" #" + id;
		if (s_el) {
			if (s_el.innerHTML.indexOf("<li>")> 0 && email_s.tagName == "LI"){
				var paren = email_s.parentNode;
				paren.innerHTML = s_el.innerHTML;
				paren.id = "email_s";
			} else {
				email_s.innerHTML = s_el.innerHTML;
			}
		} else {
			email_s.innerHTML = "";
		}
		email_about.innerHTML = s_ab.innerHTML;
		if (p_el){
			email_p.src = p_el.src;
		} else {
			email_p.src = "/imgs/p-fied/nopict.jpg";
		}
		if (composeTA.style.color != "black"){
			composeTA.value = "Type here your message to \"" + userName + "\"";
		}
		p_el = document.getElementById("email_a_fullprofile");
		if (p_el) p_el.href = email_a2.href;
		if (amInFullProfile){
			email_a2.href = "javascript:validateSignup1(1);";
			p_el.href = "javascript:validateSignup1(1);";
		}
		validateSignup1(2);
	}
}

function action(id){
	var item1 = document.getElementById("action_dispatch");
	if (item1) {
		item1.value = id;
	}
	item1 = document.getElementById("sendEmailForm");
	if (item1){
		item1.submit();
	}
}

function sendEmail(){
	var answer = confirm("You will be charged with 1 credit for sending this email, do you accept?");
	if (answer) {
		action("send");
	}
}

function saveEmail(){
	action("save");
}

function cancelEmail(){
	action("cancel");
}


function sendEmailAction(message, index){
	var el = document.getElementById("submitBtn");
	if (el.value =="Reply"){
		el.value = "Send";
		el = document.getElementById("bodyComposeInput");
		el.value = "Re: " + el.value;
		el = document.getElementById("composeTA");
		el.value = "\n\n=================================\n"+el.value;
		el.selectionStart = 0;
		el.selectionEnd = 0;
		el.focus();
		return false;
	} else {
		var c = confirm(message);
		if (c) {
			el = document.getElementById("idx");
			if (el) el.value = index;
			return validateSignup1();
		} else return false;
	}
}

function openSearchMenu(id){
	document.getElementById("advSearch").style.display = "block"; 
	openOverlay(2, "advSearch");
}

function showMenu(val){
	keepMenu = val;
	document.getElementById('searchMenu').style.display='block';	
}

function menuTimeout(){
	clearTimeout(timer1);
	if (!keepMenu) document.getElementById('searchMenu').style.display='none';
}

function removeMenu(val){
	clearTimeout(timer1);
	if (!keepMenu || val){
		document.getElementById('searchMenu').style.display='none';
	} else {
		keepMenu = 0;
		timer1 = setTimeout("menuTimeout()", 1000);
	}
}

function openOverlay(param, id, form){
	if (param){
		var display;
		if (param == 1) display = "none";
		else display = "block";
		
		var el = document.getElementById("overlay");
		el.style.display = display;
		if (id){
			el = document.getElementById(id);
		} else {
			el = document.getElementById("overlay_window2");
		}
		el.style.display = display;
		var el = document.getElementById(form);
		if (el){
			for(i=0; i<el.elements.length; i++){
				try {
					if (el.elements.item(i).tagName == "INPUT"){
						el.elements.item(i).focus();
						break;
					} 
				} catch (e){
					
				}
			}
		}
		
	} 
}

function hasAnyPicApproved(id){
	return eval("cntPicApproved_"+id) > 0;
}

function checkApprovalBtns(id){
	var el1 = document.getElementById("approve_"+id);
	var el2 = document.getElementById("deny_"+id);
	var el3 = document.getElementById("chb_aboutme_"+id+"_den");
	var el4 = document.getElementById("chb_aboutmatch_"+id+"_den");
	var el5 = document.getElementById("chb_deny_pictures_"+id);
	var el6 = document.getElementById("chb_app_pictures_"+id);
	var el;
	if (el5 && el5.checked){
		for(var i=1; i<=6; i++){
			el = document.getElementById("pic_den_"+id+"_"+i);
			if (el) el.checked = true;
		}
		
	} else if (el6 && el6.checked){
		for(var i=1; i<=6; i++){
			el = document.getElementById("pic_app_"+id+"_"+i);
			if (el) el.checked = true;
		}
	}
	if (!el5 || el3.checked || el4.checked || el5.checked || (!hasAnyPicApproved(id) && !el6.checked)){
		if (el1) el1.style.display = "none";
		el2.style.display = "inline-block";
	} else {
		if (el1) el1.style.display = "inline-block";
		el2.style.display = "none";
	}
}

function updatePicStates(radio_obj, client_id, pic_id){
	var obj1 = 'status_'+client_id+'_'+pic_id;
	
	if (((parseInt(window[obj1]) == 1) || (parseInt(window[obj1]) == 2)) && (radio_obj.value == 'approve')) { //was denied and we approve
		window["cntPicApproved_"+client_id] = parseInt(window["cntPicApproved_"+client_id])+1;
		window[obj1] = 3;
	}
	if ((parseInt(window[obj1]) >= 2)&& (radio_obj.value != 'approve')){ //was approve and now is not
		window["cntPicApproved_"+client_id] = parseInt(window["cntPicApproved_"+client_id])-1;
		window[obj1] = 1;
	}
	var el6 = document.getElementById("chb_app_pictures_"+client_id);
	var el5 = document.getElementById("chb_deny_pictures_"+client_id);
	if ((radio_obj.value != 'approve') && (el6.checked)){
		el6.checked = false;
	}
	if ((radio_obj.value != 'deny') && (el5.checked)){
		el5.checked = false;
	}
	var all_the_same = -1;
	var retvalue;
	for (var i=1; i<7; i++){
		el5 = document.getElementById("pic_app_"+client_id+"_"+i);
		if (!el5) continue;
		el6 = document.getElementById("pic_den_"+client_id+"_"+i);
		if (el5.checked) retvalue = 1;
		else if (el6.checked) retvalue = 0;
		else retvalue = -1;
		if (all_the_same == -1){ 
			if (retvalue != -1) {
				all_the_same = retvalue;
			}
		} else if (all_the_same != retvalue) {
			all_the_same = -1;
			break;
		}
	}
	if (all_the_same != -1){
		var prefix;
		if (all_the_same == 1) prefix ="app";
		else  prefix ="deny";
		
		el5 = document.getElementById("chb_"+prefix+"_pictures_"+client_id);
		el5.checked = true;
	}
	checkApprovalBtns(client_id);
}

function closeAdvSearch(){
	var el = document.getElementById("overlay");
	el.style.display = "none";
	el = document.getElementById("advSearch");
	el.style.display = "none";
}

function rotateImage(objID, degrees){
	var el = document.getElementById(objID);
	if (el){
		var idx = el.className.indexOf(" rotate");
		if (idx>=0) el.className = el.className.substring(0, idx) + ' rotate' + degrees;
		else {el.className = el.className + ' rotate' + degrees;}
/*		for (var i=0;i<100;i++){
			el = document.getElementById('highslide-wrapper-'+i);
			if (el){
				idx = el.className.indexOf(" rotate");
				if (idx>=0) { 
					el.className = el.className.substring(0, idx) + ' rotate' + degrees;
				} else {
					el.className = el.className + ' rotate' + degrees;
				}
				break;
			}
		}
*/	}
}

function openProfileMenu(idx){
	if (!document.getElementById("updatePhoto1")){
		document.location.href = "/profile?pos="+idx;
		return;
	}
	switch (idx){
	case 1:
		unhide("basic2");
		window.location.hash = "PERSONALINFO";
		break;
	case 2:
		unhide("basic1");
		window.location.hash = "PASSWORD";
		break;
	case 3:
		unhide("basic2");
		window.location.hash = "PHOTOS";
		break;
	}
}
