// CouponInputBox.js v0.7
//
// Copyright (c) 2009 internet2day GmbH
// Author: Werner G Liedl | http://www.internet2day.de
//
// If you care about the file size ...
//   Be a pro: 
//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//

/*-----------------------------------------------------------------------------------------------*/

if(typeof Prototype == 'undefined')
  throw("couponinputbox.js requires including Prototype library!");

var CouponInputBoxInstances = new Array();
var CouponInputBox = Class.create();	
CouponInputBox.prototype = {
	/**
	 * @var int
	 * The size of a scrollbar in px
	 */
	myIndex : 0,
	/**
	 * @var object
	 * The input box
	 */
	couponInputBox : null,
	/**
	 * @var object
	 * The dimmed background box
	 * Comprizes the whole window
	 */
	bgBox : null,
	/**
	 * @var int
	 * The size of a scrollbar in px
	 */
	scrollbarOffset : 20,
	/**
	 * @var string
	 * The title bar text
	 */
	heading: '',
	/**
	 * @var string
	 * The error message line
	 */
	errMsg: '',
	/**
	 * @var string
	 * An additional hint how to proceed
	 */
	hint: '',
	/**
	 * @var string
	 * The (existing) coupon code
	 */
	couponCode: '',
	/**
	 * @var string
	 * The width of the box in px
	 */
	boxWidth: 100,
	/**
	 * @var int
	 * The height of the box in px (not used => auto)
	 */
	boxHeight: 50,
	/**
	 * @var string
	 * The label of the button that returns 1
	 */
	submitLabel: 'Submit',
	/**
	 * @var string
	 * The label of the button that returns 0
	 */
	cancelLabel: 'Cancel',
	/**
	 * @var function
	 * The call back function to pass the users choice to
	 */
	onChoiceMade: Prototype.emptyFunction,
	/**
	 * @var function
	 * The existing onresize function
	 */
	mem_resize: null,
	/**
	 * @var function
	 * The existing onscroll function
	 */
	mem_scroll: null,
	
	/**
	 * Initializes the class
	 * 
	 * @access public
	 * @param void
	 * @return void
	 */
	initialize : function() {
		this._setupBox();
		this.myIndex = CouponInputBoxInstances.length;
		CouponInputBoxInstances[this.myIndex] = this;
	},
	
	/**
	 * Use the confirm box
	 * produces 
	 * 
	 * @access public
	 * @param int		the confirm box width
	 * @param int		the confirm box height
	 * @param string	the question to be asked
	 * @param string	the submitButton label
	 * @param string	the cancelButton label
	 * @param function	the callback function to receive the user's choice (input or null)
	 * @return void
	 */
	show : function(msgBoxWidth,msgBoxHeight,msgTitle,errTxt,hintTxt,msgBtnSubmit,msgBtnCancel,msgCallback,aCC) {
		this.boxWidth = msgBoxWidth;
		this.boxHeight = msgBoxHeight;
		this.heading = msgTitle;
		this.errMsg = errTxt;
		this.hint = hintTxt;
		this.submitLabel = msgBtnSubmit;
		this.cancelLabel = msgBtnCancel;
		this.onChoiceMade = msgCallback;
		this.couponCode = aCC;
	 
		this._show();
	},

	close : function(val){
		window.onresize = this.mem_resize;
		window.onscroll = this.mem_scroll;
			
		if ( this.couponInputBox != null ) {
			this.couponInputBox.style.display  = "none";
			this.couponInputBox.style.left   = "-10px";
			this.couponInputBox.style.top    = "-10px";
			this.couponInputBox.style.width  = "0px";
			this.couponInputBox.style.height = "0px";
			//this.couponInputBox.style.overflow = "clip";
		}
		
		if ( this.bgBox != null ) {
			this.bgBox.style.display  = "none";		
			this.bgBox.style.left   = "-10px";
			this.bgBox.style.top    = "-10px";
			this.bgBox.style.width  = "0px";
			this.bgBox.style.height = "0px";
			//this.bgBox.style.overflow = "clip";
		}	
	},
	
	_setupBox : function() {
		this.couponInputBox = ( document.all ? document.all.cib_box : document.getElementById("cib_box") );
		if ( this.couponInputBox == null ) {
			this.couponInputBox = document.createElement("div");
			this.couponInputBox.id = "cib_box";
	    	document.body.appendChild(this.couponInputBox);
		}
		
		this.bgBox = ( document.all ? document.all.cib_box_bg : document.getElementById("cib_box_bg") );
		if ( this.bgBox == null ) {
			this.bgBox = document.createElement("div");
			this.bgBox.id = "cib_box_bg";
	    	document.body.appendChild(this.bgBox);
		}
	},
	
	_winWidth : function() {
		if (window.innerWidth) {
			return window.innerWidth
		} else if (document.documentElement && document.documentElement.clientWidth) {
			return document.documentElement.clientWidth
		} else if (document.body) {
			return document.body.clientWidth
		}
		return 0;
	},
	
	_winHeight : function() {
		if (window.innerHeight) {
			return window.innerHeight
		} else if (document.documentElement && document.documentElement.clientHeight) {
			return document.documentElement.clientHeight
		} else if (document.body) {
			return document.body.clientHeight
		}
		return 0;
	},

	_show : function() {	
		if ( this.couponInputBox != null ) {
			var ww = this._winWidth();
			var wh = this._winHeight();
			var iebody = ( (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body );
			var dsocleft = ( document.all ? iebody.scrollLeft : window.pageXOffset-this.scrollbarOffset );
			var dsoctop  = ( document.all ? iebody.scrollTop : window.pageYOffset-this.scrollbarOffset );
			
			var xPos = Math.floor( (ww - this.boxWidth) / 2 + 0.5 + dsocleft);
			var yPos = Math.floor( (wh - this.boxHeight) / 2 + 0.5 + dsoctop );
			
			if ( xPos < 0 ) xPos = 0;
			if ( yPos < 0 ) yPos = 0;
					
			this.couponInputBox.style.left   = xPos+"px";
			this.couponInputBox.style.top    = yPos+"px";
			this.couponInputBox.style.width  = this.boxWidth+"px";
			this.couponInputBox.style.height = "auto"; // this.boxHeight+"px";
			//this.couponInputBox.style.overflow = "none";
			
			
			var ccC = ccN1 = ccN2 = ccN3 = "";
			
			if ( this.couponCode && this.couponCode.length == 11 ) {
				ccC = this.couponCode.substring(0,2);
				ccN1 = this.couponCode.substring(2,5);
				ccN2 = this.couponCode.substring(5,8);
				ccN3 = this.couponCode.substring(8);
			}
			
			
			var titleLine = ( this.heading.length ? '<p id="cib_title">' + this.heading + '</p>' : '' );
			var errLine = ( this.errMsg.length ? '<p id="cib_error">' + this.errMsg + '</p>' : '<p id="cib_error">&nbsp;</p>' );
			var hintLine = ( this.hint.length ? '<p id="cib_hint">' + this.hint + '</p>' : '<p id="cib_hint" style="display:none;">&nbsp;</p>' );
			var inputLine = '<form name="couponForm"><p id="cib_input"><div class="cib_left">Hier Gutscheincode eingeben:</div><div class="cib_right">' +
				'<input name="cib_couponCampaign" type="text" class="formularfeld02c" maxlength="2" onkeyup="skip2next(this,\'cib_couponNumber0\',2);" value="'+ccC+'">' +
				'&nbsp;<b>.</b>&nbsp;<input name="cib_couponNumber0" type="text" class="formularfeld03c" maxlength="3" value="'+ccN1+'" onkeyup=\"skip2next(this,\'cib_couponNumber1\',3);">' +
				'&nbsp;<b>.</b>&nbsp;<input name="cib_couponNumber1" type="text" class="formularfeld03c" maxlength="3" value="'+ccN2+'" onkeyup=\"skip2next(this,\'cib_couponNumber2\',3);">' +
				'&nbsp;<b>.</b>&nbsp;<input name="cib_couponNumber2" type="text" class="formularfeld03c" maxlength="3" value="'+ccN3+'"></div></p></form>';
			var submitButton = ( this.submitLabel.length ? '<button class="cib_last" type="button" onclick="CouponInputBoxInstances['+this.myIndex+']._forwardResult(1);">'+this.submitLabel+'</button>' : '' );
			var cancelButton = ( this.cancelLabel.length ? '<button class="cib_first" type="button" onclick="CouponInputBoxInstances['+this.myIndex+']._forwardResult(0);">'+this.cancelLabel+'</button>' : '' );
			var buttonLine = '<p id="cib_buttons">' + cancelButton + submitButton + '</p>';
			this.couponInputBox.innerHTML =  titleLine + inputLine + errLine + hintLine + buttonLine;
			
			if ( this.bgBox != null ) {
				this.bgBox.style.left   = dsocleft+"px";
				this.bgBox.style.top    = dsoctop+"px";
				this.bgBox.style.width  = ww+"px";
				this.bgBox.style.height = wh+"px";
				//this.bgBox.style.overflow = "none";
				this.bgBox.style.display  = "block";
			}
			this.couponInputBox.style.display  = "block";
			
			window.onresize = this._show.bind(this);
			window.onscroll = this._show.bind(this)
		}
	},
		
	_forwardResult : function(val){	
		if ( typeof this.onChoiceMade == 'function' ) {
			result = 0;
			if ( val == 1 ) {
				result = "";
				var elem = document.forms.couponForm.elements.cib_couponCampaign;
				if ( elem != null )
					result += elem.value;
				elem = document.forms.couponForm.elements.cib_couponNumber0;
				if ( elem != null )
					result += elem.value;
				elem = document.forms.couponForm.elements.cib_couponNumber1;
				if ( elem != null )
					result += elem.value;
				elem = document.forms.couponForm.elements.cib_couponNumber2;
				if ( elem != null )
					result += elem.value;
			}
			
			this.onChoiceMade(result);
		}
	}
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', couponInputBoxInit, false);

//
//	Set up all we need
//
var myCouponInputBox = null;
function couponInputBoxInit() {
	myCouponInputBox = new CouponInputBox();
}

