var url = './ajax/ajax.php?';


function setImageRate(rateObj){
	var imageId = $('#hid_image_id').val();
	var rateVal = rateObj.value;
	$.ajax({
		type: "POST",
		data: {
			action: "imageRating",
			imageId: imageId,
			rateVal: rateVal,
			time: Math.random()
		},
		url: url,
		success: function(data){

					alert(data);
//					$('#div_login_error').html(data);
		}
	});



}

function login(){

	var sUsername = $('#txt_login_username').val();
	var sPass     = $('#txt_login_password').val();
	if(validateLoginForm()){
		$.ajax({
			type: "POST",
			data: {
				action: "login",
				username: sUsername,
				password: sPass,
				time: Math.random()
			},
			url: url,
			success: function(data){
					if(data == '1'){
						callApp(true);
						$('#div_login_popup_id').css({visibility: "hidden", display: "none"});;
						$('#hid_is_logged').val('1');
//						callApp(true);
					}else{
//						callApp(false);
						$('#div_login_error').html(data);
					}
			}
		});
	}
    return false;
}

function register(){
	var sUsername = $('#txt_register_username').val();
	var sPass     = $('#txt_register_password').val();
	var cPass     = $('#txt_register_cpassword').val();

	if(validateRegistrationForm()){

		$.ajax({
			type: "POST",
			data: {
				action: "register",
				username: sUsername,
				password: sPass,
				time: Math.random()
			},
			url: url,
			success: function(data){
					if(data == '1'){
						callApp(true);
						$('#div_login_popup_id').css({visibility: "hidden", display: "none"});;
						$('#hid_is_logged').val('1');
					}else{
						$('#div_register_error').html(data);
//						callApp(false);
					}
	//			$('#message_div').html('pramod');
			}
		});
	}
    return false;
}

function checkLogin(){
	$.ajax({
		type: "POST",
		data: {
			action: "isLogin",
			time: Math.random()
		},
		url: url,
		success: function(data){
				if(data == 1){
					isLogged = true;
				}else{
					isLogged = false;
				}
		}
	});

	return false;
}

function saveCustomerInfoAndGoReview(){

	if(validateCheckOutFormOnSubmit()){

		var shipFirstName		= $('#txt_ship_first_name').val();
		var shipLastName		= $('#txt_ship_last_name').val();
		var shipCompany			= $('#txt_ship_company').val();
		var shipAddress1		= $('#txt_ship_address1').val();
		var shipAddress2		= $('#txt_ship_address2').val();
		var shipCity			= $('#txt_ship_city').val();
		var shipCountry			= $('#txt_ship_country').val();
		var shipState			= $('#txt_ship_state').val();
		var shipZip				= $('#txt_ship_zip').val();
		var shipPhone			= $('#txt_ship_phone').val();
		var shipEmail			= $('#txt_ship_email').val();
		var billFirstName		= $('#txt_bill_first_name').val();
		var billLastName		= $('#txt_bill_last_name').val();
		var billCompany			= $('#txt_bill_company').val();
		var billAddress1		= $('#txt_bill_address1').val();
		var billAddress2		= $('#txt_bill_address2').val();
		var billCity			= $('#txt_bill_city').val();
		var billCountry			= $('#txt_bill_country').val();
		var billState			= $('#txt_bill_state').val();
		var billZip				= $('#txt_bill_zip').val();
		var billPhone			= $('#txt_bill_phone').val();
		var billEmail			= $('#txt_bill_email').val();
		//var shipBy				= $("input[name='rad_ship_by']:checked").val();
		var paymentMethod		= $("input[name='rad_payment_method']:checked").val();
		var comments			= $('#txt_comments').val();

		$.ajax({
			type: "POST",
			data: {
				action: "customerOrderInfo",
				shipFirstName: shipFirstName,
				shipLastName: shipLastName,
				shipCompany: shipCompany,
				shipAddress1: shipAddress1,
				shipAddress2: shipAddress2,
				shipCity: shipCity,
				shipCountry: shipCountry,
				shipState: shipState,
				shipZip: shipZip,
				shipPhone: shipPhone,
				shipEmail: shipEmail,
				billFirstName: billFirstName,
				billLastName: billLastName,
				billCompany: billCompany,
				billAddress1: billAddress1,
				billAddress2: billAddress2,
				billCity: billCity,
				billCountry: billCountry,
				billState: billState,
				billZip: billZip,
				billPhone: billPhone,
				billEmail: billEmail,
//				shipBy: shipBy,
				paymentMethod: paymentMethod,
				comments: comments,
				time: Math.random()
			},
			url: url,
			success: function(data){
				
				window.location="review.php";
				//alert(data);
			}
		});
	}
    return false;

}


function saveOrder(){
	if($('#hid_is_logged').val() == 'false'){
		showLoginDiv();
		return false;
	}

	$.ajax({
		type: "POST",
		data: {
			action: "saveOrder",
			time: Math.random()
		},
		url: url,
		success: function(data){
			window.location="thanks.php";
			//alert("Order has been saved");
		}
	});
    return false;

}



function checkoutTotalPreviewHtml(val){

	$.ajax({
		type: "POST",
		data: {
			action: "checkoutTotalPreview",
			shipCountry: val,
			time: Math.random()
		},
		url: url,
		success: function(data){
			$('#div_total_preview').html(data);
		}
	});
    return false;

}



function showLoginDiv(){
	$('#div_login_popup_id').css({visibility: "visible", display: "block"});
}

function hideLoginDiv(){
	$('#div_login_popup_id').css({visibility: "hidden", display: "none"});

}


// Internet Explorer and Mozilla-based browsers refer to the Flash application 
// object differently.
// This function returns the appropriate reference, depending on the browser.
function getMyApp(appName) {
   if (navigator.appName.indexOf ("Microsoft") !=-1) {
	   return window[appName];
   } else {
	   return document[appName];
   }
}

function callApp(flag) {
	flashObj = getMyApp("MemoOnline");
	if(flashObj){
		getMyApp("MemoOnline").callFromJS(flag);
	}
}
