var isNavEnter, isIEEnter;
if (parseInt(navigator.appVersion)>=4) {
	if(navigator.appName == "Netscape") {
		isNavEnter = true;
	} else {
		isIEEnter = true;
	}
}
function checkEnter(evt, action) {
	var key;
	if (isNavEnter) {
		key = evt.which;
	} else {
		key = window.event.keyCode;
	}
	if (key==13) {
		eval(action);
	}
}


//top right expanding panel initialization
$(function() {
	// set opacity to nill on page load
	$("#selector div div").css("opacity","0");
	// on mouse over
	$("#selector div.hover").hover(function () {
		// animate opacity to full
		$(this).find('div')
			.stop().animate({opacity: .7}, "fast");
	},
	// on mouse out
	function () {
		// animate opacity to nill
		$(this).find('div')
			.stop().animate({opacity: 0}, "slow");
	});

	// Expand Panel
	$("#open").click(function(){
		$("#contentform").slideDown("slow");
	});

	// Collapse Panel
	$("#close").click(function(){
		$("#contentform").slideUp("fast");
	});

	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});
});

//top right login form functions
function username_focus() {
	if ($('#username').val()=='username') {
		$('#username').val('');
	}
}
function username_blur() {
	if ($('#username').val()=='') {
		$('#username').val('username');
	}
}
function mockpass_focus() {
	$('#mockpassword').hide();
	$('#password').show();
	$('#password').focus();
}
function password_blur() {
	if ($('#password').val()=='') {
		$('#mockpassword').show();
		$('#password').hide();
	}
}

/**
http://blog.stevenlevithan.com/archives/faster-trim-javascript
*/
function trim12(str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function do_login() {
	if ($('#username').val()=='username' || $('#username').val()=='') {
		alert('Please enter the username!');
		$('#username').focus();
		return false;
	}
	if ($('#password').val()=='') {
		alert('Please enter password!');
		mockpass_focus();
		return false;
	}
	$('#login_btn').hide();
	$('#login_waiter').show();
    $.ajax({
        type: "POST",
        url: APPURL+"ajax/a_login.php",
        data: "username="+$('#username').val()+"&password="+$('#password').val(),
        processData: false,//the automatic conversion of data to strings is prevented
        success: function(responseText){
        			var response=trim12(responseText);
                    if (response=='ok') {
                    	window.location.href=APPURL;
                    } else {
						alert(response);
						$('#login_waiter').hide();
						$('#login_btn').show();
                    }
        }
    });
}
function do_logout() {
    $.ajax({
        type: "POST",
        url: APPURL+"ajax/a_logout.php",
        processData: false,//the automatic conversion of data to strings is prevented
        success: function(responseText){
        			var response=trim12(responseText);
                    if (response=='ok') {
                    	window.location.href=APPURL;
                    } else {
						alert(response);
                    }
        }
    });
}

/**
adds a model to a client's wishlist
*/
function favorite(client,model,favorite) {
	if (client==0) {
		alert('You must login in to be able to perform this operation.');
		return false;
	}
	if (favorite=='yes') {
		alert('Model already added to wishlist.');
		return false;
	}
    $.ajax({
        type: "POST",
        url: APPURL+"ajax/a_favorite.php",
        data: "client="+client+"&model="+model,
        processData: false,//the automatic conversion of data to strings is prevented
        success: function(responseText) {
    				response=trim12(responseText);
    				if (response=='ok') {
    					alert('Model added to wishlist.');
    					return true;
    				}
    				if (response=='ok1') {
    					alert('Model already added to wishlist.');
    					return false;
    				}
    				alert(response);
    				return false;
	            }
    });
}

/**
removes a model from the favorites list
*/
function remove_favorite(client, model) {

    if (confirm('Are you sure you want to remove this model from your favorite list ?')) {

        if (client==0) {
            alert('You must login in to be able to perform this operation.');
            return false;
        }

        $.ajax({
            type: "POST",
            url: APPURL+"ajax/a_remove_favorite.php",
            data: "client="+client+"&model="+model,
            processData: false,//the automatic conversion of data to strings is prevented
            success: function(responseText) {
                        response=trim12(responseText);
                        if (response=='ok') {
                            alert('Model removed from wishlist.');
                            window.location.reload();
                            return true;
                        }
                        if (response=='ok1') {
                            alert('Model is not in your wishlist !');
                            return false;
                        }
                        return false;
                    }
        });
    
    }

}

/**
changes the items per page setting for various sections - some HARDCODING is used(section)
*/
function change_items_per_page(section) {
    $.ajax({
        type: "POST",
        url: APPURL+"ajax/a_change_ipp.php",
        data: "section="+section+"&ipp="+$('#items_per_page').val(),
        processData: false,//the automatic conversion of data to strings is prevented
        success: function(responseText) {
    				response=trim12(responseText);
    				if (response=='ok') {
    					window.location.reload();
    					return true;
    				}
    				alert(response);
    				return false;
	            }
    });
}

function please_login(){
	alert('You must authenticate yourself as a customer to use this feature.\n Please use the login button on top of the page to do this.');
}

function renew_captcha(imageid, imagesrc){
    $.ajax({
        type: "POST",
        url: APPURL+"ajax/a_captcha_image.php",
//        data: "client="+client+"&model="+model,
        processData: false,//the automatic conversion of data to strings is prevented
        success: function(responseText) {
    				response=trim12(responseText);
    				//reset entered captcha text
    				$('#captchainput').val('');
    				$('#'+imageid).attr('src',imagesrc+'?'+response)
    				return true;
	            }
    });
}


function search_gender_changed() {//some HARDCODE
	if($('#gender').val()==0 || $('#gender').val()==3) {//no gender or TS
		$('#male_limits').hide();
		$('#female_limits').hide();
		$('#all_limits').show();
		$('#shaved_li').hide();
		$('#cut_li').hide();
		$('#body_li').show();
        $('#body_man_li').hide();
        $('#body_man').val('');
        $('#dress_li').show();
		return true;
	}
	if($('#gender').val()==1) {//male
		$('#female_limits').hide();
		$('#all_limits').hide();
		$('#male_limits').show();
		$('#shaved_li').show();
		$('#cut_li').show();
        $('#body_li').hide();
		$('#body_man_li').show();
		$('#body').val('');
        $('#dress_li').hide();
        $('#dress').val('0');
		return true;
	}
	if($('#gender').val()==2) {//female
		$('#all_limits').hide();
		$('#male_limits').hide();
		$('#female_limits').show();
		$('#shaved_li').hide();
		$('#cut_li').hide();
		$('#body_li').show();
        $('#body_man_li').hide();
        $('#body_man').val('');
        $('#dress_li').show();
		return true;
	}
}


function sort(mode,page) {
	if (page=='') {
		var href=window.location.href;
		if (href.search(/s=[azon]/i)>0) {
			href=href.replace(/s=[azon]/i,'s='+mode);
		} else {
//			href+='&amp;s='+mode;
			if (href.search(/\?/)>0) {
				href+='&s='+mode;
			} else {
				href+='?s='+mode;
			}
		}
//		alert(href);
//		return false;
		window.location.href=href;
	} else {
		window.location.href=APPURL+page+'/?s='+mode;
	}
}

function go_to(url) {
	window.location.href=url;
}

function cancel_booking(id) {

    if (confirm('Are you sure you want to cancel this booking ?')) {

        $.ajax({
            type: "POST",
            url: APPURL+"ajax/a_cancel_booking.php",
            data: "id="+id,
            processData: false,//the automatic conversion of data to strings is prevented
            success: function(responseText) {
                    if (responseText == 'ok') {
                        window.location.reload();
                    } else {
                        alert('You can\'t cancel this booking');
                    }
            }

        });

    }

}

function filter_bookings(status) {
    
    $.ajax({
        type: "POST",
        url: APPURL+"ajax/a_filter_bookings.php",
        data: "status="+status,
        processData: false,//the automatic conversion of data to strings is prevented
        success: function(responseText) {
            if(responseText == 'ok') {
                window.location.reload();
            }
        }

    });

}
