

$(document).ready(function(){

	var wrapperId='#wrapper';
	var msgeBox='#msgbox';

	var loginWating="Validating..";
	var loginError="Error login in";
	
	var objUsername='#u';
	var objPassword='#p';
	var objLoginForm="#lf";
	
	var hidusrnme ='#hidusrnme';
	
	var filePostFile="secure/server.php";
	
	//alert('hello');
	
	
	$.getJSON(filePostFile,{hidusrnme:$(hidusrnme).val()},function(data){
		
		
				
		if(data.status==true){
		
		}else{
		
			$(wrapperId).html(data.message).slideDown('slow',function() 
			{
				$(objLoginForm).submit(function()
				{
					
					var _u=$(objUsername).val();
					var _p=$(objPassword).val();
					
					if(_u.length<=0)
					{
						alert('Invalid Username');
						$(objUsername).focus();
						
					} else if(_p.length<=0){
						
						alert('Invalid Password');
						$(objPassword).focus();
						
					}else { 
						$(msgeBox).removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
						$.post(filePostFile,{ u:_u, p:_p },function(data)
						{							
							
							if(data=="{'logedin'}")
							{								
								$(msgeBox).removeClass().addClass('messageboxok').text('Loged in....').fadeOut(1000);
								window.location.href='logedin.php?page=quotes';	
								
							}else
							{
									$(msgeBox).removeClass().addClass('messageboxerror').text('Error login in....').fadeOut(1000);
									alert('Error Login.. please try again..!');
							}
								
						});//------ Post
						
						
					}
					
					return false;
				}); // ------- submit()
																	  
			}); // ---------- Slide Down
			
		}
		
	});

	
});

function conv2order(quoteID)
{

		var opsFile='secure/ops.php';
		var msgeBox='#msgbox';
		
		
		$(msgeBox).removeClass().addClass('messagebox').text('Converting....').fadeIn(1000);
		
		
		$.post(opsFile,{action:'c2o',ref:quoteID},function(data)
		{

			if(data=='ok'){
				$(msgeBox).removeClass().addClass('messageboxok').text('Converted....').fadeOut(1000);
				window.location.href='logedin.php?page=orders';
			}
			else{
					$(msgeBox).removeClass().addClass('messageboxerror').text('Cannot Convert! please contact admin....').fadeOut(1000);
				}
		});
}

function updateQuote(quoteID)
{
	var opsFile='secure/ops.php';
	var msgeBox='#msgbox';
	
	var quoteVal='#quoteval';
		
		$(msgeBox).removeClass().addClass('messagebox').text('Updating....').fadeIn(1000);
		
		
		$.post(opsFile,{action:'quo',ref:quoteID,est:$(quoteVal).val()},function(data)
		{
		
			if(data=='ok'){
				$(msgeBox).removeClass().addClass('messageboxok').text('Converted....').fadeOut(1000);
				window.location.href='index.php?page=quotes';
			}
			else{
					$(msgeBox).removeClass().addClass('messageboxerror').text('Error updating....').fadeOut(1000);
				}
		});	
}


function calculate_cost()
{
	var stitch_count = document.getElementById("estimated_stitch_count").value;
	//var stitch_count = 26000;
	var estimated_cost;
	// Calculate estimated cost
	if (stitch_count <= 10000) {
		estimated_cost = 20;
	} else if (stitch_count > 10000) {
		stitch_count = (stitch_count - 10000) / 1000;		
		stitch_count_obj = new Number(stitch_count);
		stitch_count = stitch_count_obj.toString();		
		if (stitch_count.indexOf(".") != -1) {
			var num = stitch_count.split(".");
			//alert("."+num[1]);
			var num_float = "."+num[1];
			num_float = Number(num_float);			
			if (num_float >= (5/10)) {
				estimated_cost = 20 + Number(num[0]) + 1;
			} else {
				estimated_cost = 20 + Number(num[0]);				
			}
		} else {
			estimated_cost = 20 + Number(stitch_count);	
		}
	}
	
	if (stitch_count == 0) {
		document.getElementById("quoteval").value = "";
	} else {
		document.getElementById("quoteval").value = estimated_cost;
	}
}