function show_error(message, fName, text)
{
	jQuery('#blinder').show(0);
	
	
	
	error_div = document.createElement("div");
	error_div.id = 'loloi_error';
	if (message === "conf") {
			order_mess = "<strong style='color:red;'>ATTENTION</strong><br /><br />Are you sure you want to cancel?<br /><br /><div align='right'><button class='btn' onclick='go_home();'>Go to home page</button>&nbsp;&nbsp;<button class='btn' id='cancel_message' onclick='hide_error_mess();'>Cancel</button></div>";
		}
		
	if (message === "del") {
			order_mess = "<strong style='color:red;'>ATTENTION</strong><br /><br />You are about to delete data. It cannot be restored at a later time! Continue?<br /><br /><div align='right'><button class='btn_r' onclick=do_it('"+ fName +"');>Continue</button>&nbsp;&nbsp;<button class='btn' id='cancel_message' onclick='hide_error_mess();'>Cancel</button></div>";
		}
		
	if (message === "err") {
			order_mess = "<strong style='color:red;'>Error</strong><br /><br />"+ text +"<br /><br /><div align='right'><button class='btn' id='cancel_message' onclick='hide_error_mess();'>Cancel</button></div>";
		}
		
	if (message === "del_link") {
			order_mess = "<strong style='color:red;'>ATTENTION</strong><br /><br />You are about to delete data. It cannot be restored at a later time! Continue?<br /><br /><div align='right'><button class='btn_r' onclick=location.href='"+ fName +"';>Delete</button>&nbsp;&nbsp;<button class='btn' id='cancel_message' onclick='hide_error_mess();'>Cancel</button></div>";
		}
		
	if (message === "lnk") {
			order_mess = "<strong style='color:red;'>ATTENTION</strong><br /><br />"+ text +"<br /><br /><div align='right'><button class='btn' onclick=go_link('"+ fName +"');>Continue</button>&nbsp;&nbsp;<button class='btn' id='cancel_message' onclick='hide_error_mess();'>Close</button></div>";
		}
	error_div.innerHTML = order_mess;
	main_wrapper = document.getElementById("main-wrapper");
	document.body.insertBefore(error_div, main_wrapper);
}
function hide_error_mess(){
		if(jQuery('#loloi_error').is(':visible')){
			jQuery('#loloi_error').remove();
			jQuery('#blinder').hide(0);
		}
	}
function go_home(){
		window.location= 'http://' + location.host + '/home/';
	}

function do_it(name){
		//document.+fname+.submit();
		document.getElementById(name).submit();
		//document.+name+.submit();
	}
	
function go_link(lnk){
		//document.+fname+.submit();
		hide_error_mess();
		window.location.href=''+ lnk +'';
		//document.+name+.submit();
	}
	
// Close All Error With Escape Key --------------------------------- >>

jQuery(document).keydown(function(e) {
	if (e.keyCode === 27) {
		hide_error_mess();
	}
});








//-------------- All Search ----------------------->

function getXMLHTTP() {
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
return xmlhttp;
}
	
	function get_req(req) {
		var add = 'http://' + location.host + '/';
		var strURL= add + "search/front/?s=" + req;
		var req = getXMLHTTP();
			if (req) {
				req.onreadystatechange = function() {
					if (req.readyState === 4) {
						// only if "OK"
						if($('#main_search_field').val() !== ""){	
							if (req.status === 200) {
								if($('#advance_search_wrapper').is(':visible')){
									$('#advance_search_wrapper').animate({
											marginTop: "-450px" 
									});
									setTimeout(function(){
										$('#advance_search_wrapper').hide(0);		
									}, 500);
								}
								
								
								document.getElementById('main_search_res_wrapper').innerHTML=req.responseText;
								$('#main_search_res_wrapper').slideDown();
								setTimeout(function(){
									if (document.getElementById('load_before')){
										document.getElementById('load_before').innerHTML = "<span style='color:red'>Nothing Found....</span>";
										}
												
									}, 2500);
							} else {
								document.getElementById('main_search_res_wrapper').innerHTML="<img src='"+ add +"images/error.png' />";
								$('#main_search_res_wrapper').slideDown();
							}
						}
						else {
							document.getElementById('main_search_res_wrapper').innerHTML="";
							$('#main_search_res_wrapper').slideUp();							
							};
					};			
				};		
				req.open("GET", strURL, true);
				req.send(null);
			}
	}
$(function(){
	var s = document.getElementById('main_search_field');
	$('#main_search_field').blur(function(){
		if($(s).val() === "Search by Style, Collection or Size"){
			$('#main_search_res_wrapper').slideUp();	
		}								  
	});
		   
})

//-------------- All Search ----------------------->


function adv_search() {
	$('#main_search_res_wrapper').slideUp();
	if($('#advance_search_wrapper').is(':visible')) {
			$('#advance_search_wrapper').animate({
				marginTop: "-450px"							 
			});
			setTimeout(function(){
				$('#advance_search_wrapper').hide(0);		
			}, 500);			
		}
	else {
			$('#advance_search_wrapper').show(0).css('marginTop','-450px');
			$('#advance_search_wrapper').animate({
				marginTop: "0px"									 
			});
		}
	
}



//-------------- Front Search Fetching Data ----------------------->


function reloadSearch() {
  if(!isLoading){
      var q = $('#main_search_field').val();
       if (q.length >= 3) {
          isLoading = true;
           // ajax fetch the data
           //$('#searchterms').html("fetching...'" + q + "'");
       		get_req(q);
           // enforce the delay
           setTimeout(function(){
             isLoading=false;
             if(isDirty){
               isDirty = false;
               reloadSearch();
             }
           }, delay);
       }
     }
};

var delay = 1000;
var isLoading = false;
var isDirty = false;
$(function() {
    //reloadSearch();
	$('#main_search_field').keyup(function(){
		isDirty = true;
		reloadSearch();
	  });
});

//-------------- END Front Search Fetching Data ----------------------->


function show_login_frm(type){
	if(type === "b") {
		$('#log_fir').show(0);
	}
	
	if(type === "a") {
		$('#log_fir').hide(0);
	}
	
	if($('#login_gate_form').is(':visible')){
		$('#login_gate_form').effect( 'shake', 100 );	
	}
	else {
		$('#login_gate_form').slideDown(function(){
			$('.l_in').fadeIn();	
		});
	}
};

$(function(){
	$('.l_in').hide();
	
	/*$('#login_gate').click(function(){
		if($('#login_gate_form').is(':visible')){
			$('.l_in').fadeOut(function(){
				$('#login_gate_form').slideUp();
			});			
		}
		else {
			$('#login_gate_form').slideDown(function(){
				$('.l_in').fadeIn();	
			});
		}
	});*/
	
	$('#gate_user').blur(function(){
		if($(this).val() === ""){
			$(this).val("Username");
		}
	});
	
	$('#gate_user').focus(function(){
		if($(this).val() === "Username"){
			$(this).val("");
		}
	});
	
	
	$('#gate_pass').blur(function(){
		if($(this).val() === ""){
			$(this).val("////////");
		}
	});
	
	$('#gate_pass').focus(function(){
		if($(this).val() === "////////"){
			$(this).val("");
		}
	});
	
	$('#stb_login_gate_form').click(function(){
		var user = $('#gate_user').val();
		var pass = $('#gate_pass').val();
		if(user === "" || user === "Username") {
			show_error('err', '', 'Username is Required!')
		}
		else if(pass === "" || pass === "////////"){
			show_error('err', '', 'Password is Required!')
		}
		else {
			$('#login_gate_form').submit();
		}
	});
});






// Log Out

function do_out() {
	
	$('#blinder').show(0);
	b_div = document.createElement("div");
	b_div.id = 'loloi_b2b_wrapper';
	b_div.innerHTML = "<img src='http://loloirugs.com/images/loading64.gif' style='margin-top: 50px;' /><br /><br />Please Wait..";
	b_main_to_ad = document.getElementById("main-wrapper");
	document.body.insertBefore(b_div, b_main_to_ad);
	
	
	
	//Ie Fix
	b_div_sh = document.createElement("div");
	b_div_sh.id = 'loloi_b2b_wrapper_shadow';
	b_main_to_sh = document.getElementById("main-wrapper");
	document.body.insertBefore(b_div_sh, b_main_to_sh);
	$('#loloi_b2b_wrapper_shadow').css('opacity','0.3')
	
	
	frame = '<iframe name="loloi_b2b_frame" id="loloi_b2b_frame" src=""></iframe>';
	$('#loloi_b2b_wrapper').append(frame);
	$('#loloi_b2b_frame').attr('src', "http://loloirugs.com/logout/");
	$('#loloi_b2b_frame').css({
		position :	'absolute',
		opacity: '0.0'
	});
	
	
	
	setTimeout(function(){
		$('#loloi_b2b_wrapper_shadow').fadeOut();
		$('#loloi_b2b_wrapper').fadeOut();
		$('#blinder').hide(0);
		window.location.reload();
		
			
	},3000);
}
