/*
function applyCoupon() {
	var code = $("input.coupon").val();
	$.ajax({
	   type: "POST",
	   url: "ajax_functions.php",
	   data: constructQueryString('applyCoupon',code),
	   dataType: "json",
	   success: function(result) {
			if(result['result'] == 'true') {
				$(".couponName").html(result['coupon_name']);
				$(".couponDiscount").html(result['coupon_discount']);
				$(".finalTotal").html(result['final_total']);
				$(".hid_final_total").val(result['final_total']);
				$("input.coupon").hide();
				$(".couponButton").html('<input type="button" value="Remove" class="boxSubmit" onclick="removeCoupon()"/>');
			} else {
				alert(result['message']);
			}
	   }
	 });	
}

function removeCoupon() {
	$.ajax({
	   type: "POST",
	   url: "ajax_functions.php",
	   data: constructQueryString('removeCoupon'),
	   dataType: "json",
	   success: function(result) {
		  if(result['result'] == 'true') {
			  $("input.coupon").val('');
			  $(".couponName").html('');
			  $("input.coupon").show();
			  $(".couponDiscount").html('0.00');
			  $(".finalTotal").html(result['final_total']);
			  $(".hid_final_total").val(result['final_total']);
			  $(".couponButton").html('<input type="button" value="Apply" class="boxSubmit" onclick="applyCoupon()"/>');
		  }
	   }
	 });	
}

**
*
*
*
UTILITY FUNCTIONS
*
*
*/


function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


function clearFields(className) {
	$("."+ className).each(function() {
		var id = $(this).attr("id");
			$(this).val('');
		
		} );
}

function constructQueryString() {
		if(arguments.length == 1 && typeof(arguments[0]) == "object") {
			arguments = arguments[0];
		}
		var qs = 'function='+arguments[0];
		for(i = 0; i < arguments.length-1; i++) {
			qs += '\&arg_' + i + '=' + URLEncode(arguments[i+1]);
		}
		return qs;
}

function array_append(arr,val){
	arr[arr.length]=val;
}

jQuery.fn.option = function (value) {
return this.each(function() {
        var sel = $(this)[0];
        for ( var i=0; i<sel.length; i++ )
                if (sel[i].value == value)
                        sel.selectedIndex = i;
        });
};

function print_result(result) {
	if (result['result']) {
		show_message(result['message']);
	} else {
		show_error(result['message']);
	}
}

function hide_error() {
	$("#cpack_error_box").hide();
}

function show_error(error_text) { 
	if(error_text.length > 0) {
		$("#cpack_error_box div.warning").html(error_text);	
		$("#cpack_error_box").show();
	}
}

function hide_message() {
	$("#cpack_message_box").hide();
}

function show_message(message_text) {
	if(message_text.length > 0) {
		$("#cpack_message_box div.message").html(message_text);	
		$("#cpack_message_box").show();
		setTimeout(function() { 
							$("#cpack_message_box").fadeOut("slow");
					}, 3500 );
	}
}

function objPropExists(object,property) {
	if(typeof(object) == "undefined") {
		return false;	
	}
	if(typeof(object[property]) != "undefined") {
		return true;	
	}
	return false;
}

function objPropLength(object) {
	var j = 0;
	for(var i in object) {
		j++;
	}
	return j;
}

function copy_form(from, to) {
    $("." + from).each(function() {
        var classes = $(this).attr("class").split(" ");
        var field = classes[1]; //second attribute of class is fieldname
        $("." + field + "." + to).val($(this).val());
    });
}	

function isEmpty(str) {
	var myRe = /^\s*$/;
	return myRe.test(str);	
}


