// ---------------------- User ----------------------
function user() {
	var currentLocation  = window.location;
	window.location = 'user.php?action=user&ref='+currentLocation;
}
function user_module() {
	var currentLocation  = window.location;
	window.location = '../user.php?action=user&ref='+currentLocation;
}

// ---------------------- Fadein Fadeout Div ----------------------
var time=200;
function fadeindiv(divname,time){
	$(divname).fadeIn(time);
}
function fadeoutdiv(divname,time){
	$(divname).fadeOut(time);
}

// ---------------------- Top Info Div ----------------------

$( function() {
	$('#top_info_div').click(function() {
		$('#top_info_div').animate({top:"-=35px",opacity:0}, "slow");
	});
    $(document).bind('reveal.facebox', function() {
        var rep = $('#facebox  div.content').html();
        if(rep ==  0 ){
            window.location.href ="/index.php";
        }
    });
    $(".defaultText").focus(function() {
        if ($(this).val() == $(this)[0].title) {
            $(this).val("");
        }
    });
    $(".defaultText").blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this)[0].title);
        }
    });
});

function show_top_info(message) {
	$('#top_info_message').html(message);
	$('#top_info_div').animate({top:$(window).scrollTop()+"px",opacity:1.0}, "0");
	$('#top_info_div').fadeIn(500);
	$('#top_info_div').delay(1000).animate({top:"-=35px",opacity:0}, "slow");
}


// Put values

function put_values(elementId, value) {
	$(elementId).val(value);
}

function put_html(elementId, value) {
	$(elementId).html(value);
}

function getValueFacebox(id) {
	var value = $('#facebox '+id).val();
	value = value.replace(/^[0]+/g,"");
	if(value == 0) {
		value = 0
	}
	return value;
}

function putValueFacebox(id, value) {
	$('#facebox '+id).val(value);
}

function putValue(id, value) {
	$(id).val(value);
}

function getValue(id) {
	return $(id).val();
}

function getAndPutByIdFacebox(id) {
	value = $('#facebox '+id).val();
	$(id).val(value);
}

function gotoWindow(url) {
	window.location = url;
}
//--------to get Countries,states and cities data in drop down

function getState(selectObj) {
    if(selectObj > 0) {
		var countryId = selectObj;
        if(countryId != 101) {
            $("#tstate").show();
            $("#state").hide();
        } else{
            $("#tstate").hide();
            $("#state").show();
        }
	} 
}
function getStatesResp(resp) {
    $("#state").get(0).options.length = 0;
    $("#state").get(0).options[0] = new Option("Select State", "-1");
    $.each(resp,function(index,item){
     $("#state").get(0).options[$("#state").get(0).options.length] = new Option(item.state_name, item.state_name);
  });
}


function goToByScroll(id){
	$('html,body').animate({scrollTop: $(id).offset().top},'slow');
}

function submitForm(formId) {
	$(formId).submit();
}

function clearForm(ele) {
	$('#button_save').removeAttr('disabled').removeClass('button_calltoaction_disabled').addClass('button_calltoaction');
	$('#button_update').removeAttr('disabled').removeClass('button_calltoaction_disabled').addClass('button_calltoaction');
	$('#save').removeAttr('disabled').removeClass('button_calltoaction_disabled').addClass('button_calltoaction');
	$('#update').removeAttr('disabled').removeClass('button_calltoaction_disabled').addClass('button_calltoaction');
	$(ele).find(':input').each(function() {
		switch(this.type) {
			case 'password':
			case 'select-multiple':
			case 'select-one':
			case 'text':
			case 'textarea':
			case 'hidden':
					$(this).val('');
					break;
			case 'checkbox':
			case 'radio':
				this.checked = false;
		}
	});
	$('#registration_old').attr('checked', 'checked');
}

function reserSrNo(id) {
	$.each($(id).children("tr"),function(ind,obj){
		$(obj).children("td").eq(0).html(ind+1);
	});
}

function reserTableEvenOdd(klass) {
	$(klass+' tbody tr').each(function(index, obj) {
		$(obj).removeClass('even').removeClass('odd');
		if(index % 2 == 0) {
			$(obj).addClass('odd');
		} else {
			$(obj).addClass('even');
		}
	});
}


function checkIsNaN(value) {
	if(isNaN(value)) {
		return 100;
	} else {
		return value;
	}
}

function checkForPercentage(value) {
	if(value == '' || isNaN(value)) {
		return 0;
	} else {
		return value;
	}
}

function resetSrNo() {
	$('.srno').each( function(ind,obj) {
		$(obj).html(ind+1);
	});
}

// -------------------------------- Multiple Delete --------------------------------
$(function() {
	$('#action').hide();
	$("#check_all").live( 'click', function(){
		if($(this).is(':checked')) {
			$(".checkbox").each(function(index, obj){
				$(obj).attr('checked', 'checked');
				$(obj).parents('tr').eq(0).addClass('rowselected');
			});
			$('#action').show();
		} else {
			$(".checkbox").each(function(index, obj){
				$(obj).removeAttr('checked');
				$(obj).parents('tr').eq(0).removeClass('rowselected');
			});
			$('#action').hide();
		}
	});
	$(".checkbox").live('click', function(){
		if($(this).is(':checked')) {
			$(this).parents('tr').eq(0).addClass('rowselected');
		} else {
			$(this).parents('tr').eq(0).removeClass('rowselected');
		}
		var flag = false;
		$('.checkbox').each( function(index, obj) {
			if($(this).is(':checked')) {
				flag = true;
			}
		});
		if(flag) {
			$('#action').show();
		} else {
			$('#action').hide();
		}
	});
});

function buyPin(amount) {
	$.ajax({
		type: 'POST',
		url: '/orders.php?action=order',
		data: 'amount='+amount,
		success: function(data) {
			if(data.error == 0){
				$('#container').append(data.form);
				$('#'+data.form_name).submit();
			} else {
				alert("There was some error. Please try again.");
			}
		},
		dataType: 'json'
	});
}

function checkApprovedStatus(uuid) {
	$.ajax({
		type: 'POST',
		url: '/orders.php?action=checkapproval',
		data: 'order='+uuid,
		success: function(data) {
			if(data.po_uuid != 0){
				if(data.approve_status != 0) {
					switch(data.approve_status) { 
						case 1:
							var approval = 'Approved';
							break;
						case 2:
							approval = 'Declined';
							break;
						case 3:
							approval = 'Awaiting Cheque';
							break;
						case 4:
							approval = 'Refund';
							break;
						case 5:
							approval = 'Cancel';
							break;
						default:
							approval = 'Not available';
							break;
					}
					$('#approval_status').html(approval);
					$('#please_wait').hide();
					$('#back_to_orders').show();
				} else {
					setTimeout('checkApprovedStatus('+uuid+')', 5000);
				}
			} else {
				alert("There was some error. Please try again.");
			}
		},
		dataType: 'json'
	});
}

function showOrderDetails(orderId) {
	$(location).attr('href', '/orders.php?action=details&order='+orderId);
}

function makePayment(orderId) {
	$.ajax({
		type: 'POST',
		url: 'dealers_account.php?action=make_payment',
		data: 'id='+orderId,
		success: function(data) {
			if(data.error == 0){
				$('#container').append(data.form);
				$('#'+data.form_name).submit();
			} else {
				alert("There was some error. Please try again.");
			}
		},
		dataType: 'json'
	});
}

function reserSrNo(id) {
	$.each($(id).children("tr"),function(ind,obj){
		$(obj).children("td").eq(0).html(ind+1);
	});
}


function showNotification(defaultMessage, customMessage) {
	var showMessage = defaultMessage;
	if(typeof customMessage != 'undefined' && defaultMessage.length > 0) {
		showMessage = customMessage + ' ' + defaultMessage;
	} else if(typeof defaultMessage == 'undefined') {
		showMessage = customMessage;
	}
	if(showMessage.length > 0) {
		$('#notification').fadeIn(250);
		$('#notification span').html(showMessage);
		setTimeout(function() {
			$('#notification').fadeOut()
		}, 2500);
	}
}
