var quickquote = function(pCntrArticleId) {

	var _sCntrArticleId = pCntrArticleId;
	var _sArticleId = _sCntrArticleId.substr(_sCntrArticleId.indexOf('_') + 1);
	var _oUsername = $('#qq_oxusername');
	var _oRemark = $('#qq_oxremark');
	var _oBrandingfile = $('#qq_brandingfile');
	var _oBrandingfile_error = $('#qq_brandingfile_error');
	var _oDivMsg = $('#quickquote div#msg');
	var _oDivMsgQuote = $('#quickquote div#msg_quote');
	var _oDivMsgFileupload = $('#quickquote div#msg_fileupload');
	var _oDivMsgLoader = $('#quickquote div#msg_loader');
	var _oDivMsgActions = $('#quickquote div#msg_actions');
	var _oDivForm = $('#quickquote div#form');
	var _oInputQuoteId = $('#quickquote #quoteId');

	var _oFileUploader = null;

	var _bQuoteError = false;
	var _bFileUploadComplete = false;
	
	var google_conversion_id = 1033982309;
	var google_conversion_language = "en";
	var google_conversion_format = "2";
	var google_conversion_color = "ffffff";
	var google_conversion_label = "YF_pCNvx6gEQ5aKF7QM";
	var google_conversion_value = 0;
	
	
	this.init = function() {
		$('#qq_articleTitle').html(
				$('input[name="qq_title_' + _sCntrArticleId + '"]').val());
		$('#qq_articleIcon').attr("src",
				$('input[name="qq_icon_' + _sCntrArticleId + '"]').val()).attr(
				"alt",
				$('input[name="qq_title_' + _sCntrArticleId + '"]').val());
		$('#qq_articleArtnum').html(_sArticleId);
		$('#qq_articleAid').val(
				$('input[name="qq_aid_' + _sCntrArticleId + '"]').val());
		$('#qq_articleAnid').val(
				$('input[name="qq_anid_' + _sCntrArticleId + '"]').val());
		$('#qq_articleInvadrTitle').val(
				$('input[name="qq_title_' + _sCntrArticleId + '"]').val());
		$('#qq_articleInvadrArtnum').val(_sArticleId);

		_oDivMsg.removeClass("show");
		_oDivMsgQuote.removeClass("error").html("");
		_oDivMsgFileupload.removeClass("error").html("");
		_oDivMsgActions.hide();
		_oBrandingfile_error.html("").hide();
		_oFileUploader = null;
		
		if(_oBrandingfile.length == 0) {
			$('<br />').insertAfter(_oBrandingfile_error);
			$('<input type="file" id="qq_brandingfile" name="brandingfile" />').insertAfter(_oBrandingfile_error);
		}
		
		_oDivForm.show();
	};

	this.submitQQForm = function() {
		var error = false;

		var _sEmailTpl = /^[^@]+@[^@]+.[a-z]{2,}$/i;

		$(
				'#quickquote label, #quickquote input, #quickquote select, #quickquote textarea, #quickquote div')
				.removeClass("error");
		_oBrandingfile_error.html("").hide();

		if(document.getElementById('qq_brandingfile').value != "") {
			if (!validateUploadFile()) {
				_oBrandingfile.addClass("error");
				$('label[for="qq_brandingfile"]').addClass("error");
				error = true;
				_oBrandingfile.focus();
			}			
		}


		if (_oRemark.val() == "") {
			_oRemark.addClass("error");
			$('label[for="qq_oxremark"]').addClass("error");
			error = true;
			_oRemark.focus();
		}

		if (_oUsername.val().match(_sEmailTpl) == null) {
			_oUsername.addClass("error");
			$('label[for="qq_oxusername"]').addClass("error");
			error = true;
			_oUsername.focus();
		}

		if (error == false) {
			var dataString = "invadr[qq_articletitle]="
					+ $('#qq_articleInvadrTitle').val();
			dataString += "&invadr[qq_articlenr]="
					+ $('#qq_articleInvadrArtnum').val();
			dataString += "&invadr[qq_oxusername]=" + _oUsername.val();
			dataString += "&invadr[qq_oxsal]=" + $('#qq_oxsal').val();
			dataString += "&invadr[qq_oxname]=" + $('#qq_oxname').val();
			dataString += "&invadr[qq_oxcompany]]=" + $('#qq_oxcompany').val();
			dataString += "&invadr[qq_oxcountryid]="
					+ $('#qq_oxcountry_select').val();
			dataString += "&invadr[qq_oxfon]=" + $('#qq_oxfon').val();
			dataString += "&invadr[qq_oxquantity]=" + $('#qq_oxquantity').val();
			dataString += "&invadr[qq_oxspecification]="
					+ $('#qq_oxspecification').val();
			dataString += "&invadr[qq_oxremark]=" + _oRemark.val();

			_oDivForm.hide();
			_oDivMsg.addClass("show");
			_oDivMsgLoader.addClass("loading");

			$.ajax( {
				type : "POST",
				url : "/index.php",
				dataType : "json",
				data : "cl=info&fnc=processQQRequest&" + dataString,
				success : function(data) {
					_oDivMsgLoader.removeClass("loading");

					_oDivMsgQuote.html("<h2>" + data.msgTitle + "</h2><p>"
							+ data.msgContent + "</p>");
					
					if (data.status == "error") {
						_oDivMsgQuote.addClass("error");
						_bQuoteError = true;
					}
					else {
//						$.getScript("http://www.googleadservices.com/pagead/conversion.js")
					}
					
					if (_oBrandingfile.val() != "") {
						if (_oFileUploader == null) {
							_oFileUploader = createUploader();
						}
						_oFileUploader._options.params.quoteId = _oUsername.val();
						_oFileUploader._onInputChange(document
								.getElementById('qq_brandingfile'));
					}
					else {
						_bFileUploadComplete = true;
					}
					showActionButtons();
				}
			});
		}

		return false;
	};

	var createUploader = function() {
		$('#msg .dot_sep').show();
		_oDivMsgFileupload.show();
		_oDivMsgLoader.addClass("loading");
		_oDivMsgFileupload.append("<div id=\"fileuploader\"></div>");
		
		if(_oInputQuoteId.val() == "") {
			
		}
		var oFileUploader = null;
		if (document.getElementById('fileuploader')) {
			oFileUploader = new qq.FileUploader(
					{
						element : document.getElementById('fileuploader'),
						action : '/modules/mhwd_fileuploader/mhwd_fileuploader_ajax.php',
						params : {
							cl : 'info',
							fnc : 'uploadFile',
							quoteId : ''
						},
						allowedExtensions : [ 'jpg', 'jpeg', 'png', 'gif',
								'pdf', 'bmp', 'ai', 'eps', 'cdr', 'psd', 'tif' ],
						sizeLimit : 5242880,
						onSubmit : function() {
							_oDivMsgLoader.addClass("loading");
						},
						onComplete : function(id, fileName, responseJSON) {
							_oDivMsgLoader.removeClass("loading");
							if (responseJSON.success) {
								_oDivMsgFileupload.append("<p>"
										+ responseJSON.successmsg + "</p>");
							} else {
								_oDivMsgFileupload.addClass("error");
								_oDivMsgFileupload.append("<h2>"
										+ responseJSON.errortitle + "</h2>");
								_oDivMsgFileupload.append("<p>"
										+ responseJSON.errormsg + "</p>");
							}

							_bFileUploadComplete = true;
							showActionButtons();
						},
						onCancel : function() {
							_oDivMsgLoader.removeClass("loading");
							var oUploadLabel = $('#fileuploader .qq-upload-label');
							var oUploadFile = $('#fileuploader .qq-upload-file');
							var oList = $('#fileuploader .qq-upload-list');
							oList.append("<li>");
							oUploadLabel.appendTo(oList);
							oUploadFile.appendTo(oList);
							oList.append(fu_cancelledText + "</li>");

							_bFileUploadComplete = true;
							showActionButtons();
						},
						messages : {
							typeError : fu_typeErrorText,
							sizeError : fu_sizeErrorText,
							minSizeError : fu_minSizeErrorText,
							emptyError : fu_emptyErrorText,
							onLeave : fu_onLeaveText
						},
						showMessage : function(message) {
							_oBrandingfile_error.addClass("error");
							_oBrandingfile_error.append("<p>" + message
									+ "</p>");
							_oBrandingfile_error.show();
						},
						template : '<div class="qq-uploader">'
								+ '<div class="qq-upload-button" style="display: none;"></div>'
								+ '	<ul class="qq-upload-list"></ul>'
								+ '</div>',
						fileTemplate : '<li>'
								+ '	<span class="qq-upload-label">'
								+ fu_qq_uploadInProgressLabel
								+ '&nbsp;</span>'
								+ '	<span class="qq-upload-file"></span>'
								+ '	<span class="qq-upload-spinner" style="display: none;"></span>'
								+ '	<span class="qq-upload-size"></span>'
								+ '	<a class="qq-upload-cancel" href="#">'
								+ fu_cancelText + '</a>'
								+ '	<span class="qq-upload-failed-text">'
								+ fu_failedText + '</span>' + '</li>'

					});
		}

		return oFileUploader;
	};

	var showActionButtons = function() {
		if (_bQuoteError || _bFileUploadComplete) {
			_oDivMsgActions.show();
		}
	};

	var validateUploadFile = function() {
		var oFile = document.getElementById('qq_brandingfile');

		if (_oFileUploader == null) {
			_oFileUploader = createUploader();
		}

		var blResponse = false;
		if (oFile.files) {
			blResponse = _oFileUploader._validateFile(oFile.files[0]);
		} else {
			blResponse = _oFileUploader._validateFile(oFile);
		}
		return blResponse;
	};
};
