/**
 *
 *
 *
 **/

/* Product page - start */

/**
 * Function to switch between tabs on the product page.
 *
 * @access public
 * @return void
 **/
function productTabOnClick(tab) {
	var tabs = Array('first','second','third', 'fourth', 'fifth');

	/* Select header */
	for(var i=0;i<tabs.length;i++) {
		if(document.getElementById('tabs_product_'+tabs[i]+'_header') != null) {
			if(tab == tabs[i]) {
				document.getElementById('tabs_product_'+tabs[i]+'_header').className = 'tabs_product_header_selected';
			} else {
				document.getElementById('tabs_product_'+tabs[i]+'_header').className = 'tabs_product_header';
			}
		}
	}
	/* Select "page" */
	for(var i=0;i<tabs.length;i++) {
		if(document.getElementById('tabs_product_'+tabs[i]) != null) {
			if(tab == tabs[i]) {
				document.getElementById('tabs_product_'+tabs[i]).className = 'tabs_product_selected';
			} else {
				document.getElementById('tabs_product_'+tabs[i]).className = 'tabs_product';
			}
		}
	}
}

/**
 * Function to rotate big product thumb on the product page.
 *
 * @access public
 * @return void
 **/
function productThumbOnClick(thumbNo) {
	if (document.images) {
		var big = document.getElementById('big_mama_of_thumbs');
		var thumb = document.getElementById('thumb'+thumbNo);

		big.src = window.largeThumbURL[thumbNo];
		big.alt = thumb.alt;

		big.parentNode.href = window.productImageURL[thumbNo];
	}
}

function openProductImagePopup(url) {
    window.open(url,'_blank',
        'directories=no,location=no,menubar=no,status=no,toolbar=no,height=600,width=700'
    );
}

/* Product page - end */


/** Checkout - delbetaling GEMB - start */

/** Hide the given element if it doesn't contain a checked radio button. */
function checkhideradio(divid) {
	var div=document.getElementById(divid);
	if (div) {
		var inputs=div.getElementsByTagName('input');
		var found=false;
		for(var i=0;i<inputs.length;i++) {
			if (inputs[i].type=='radio') {
				if (inputs[i].checked) {
					found=true;
					break;
				}
			}
		}
		showhideIf(divid,found);
	}
}

/** Update the locked status of various fields, based on arbeidsforhold. */
function updateLocking() {
	var key='fast';
	var af=document.getElementById('arbeidsforhold');
	if (af) {
		key=af.value;
	}
	var allgroups=new Array();
	allgroups.push('stilling-prosent');
	allgroups.push('ansatt-til');
	allgroups.push('arbeidsgiver');
	allgroups.push('arbeidsgiver-tlf');
	allgroups.push('ansatt-fra');
	var lockgroups=new Array();
	if (key=='fast') {
		lockgroups.push('ansatt-til');
	} else if (key=='midlertidig' || key=='egendrivende') {
		// no locked groups
	} else {
		lockgroups=allgroups;
	}
	for(var i=0;i<allgroups.length;i++) {
		setGroupDisabled(allgroups[i],lockgroups.indexOf(allgroups[i])!=-1);
	}
}

/*
Add indexOf to Array prototype for those browsers (iow IE) that don't have it.
Have to do this because updateLocking uses it.
This implementation was lifted from the mozilla development site:
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
*/
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


/** Set the disabled status of the fields in a "group".
 * The group's class is set to "group-disabled" or "" depending on status.
 * @param groupName The id of the group element.
 * @param disabled What to set the disabled status to.
 */
function setGroupDisabled(groupName,disabled) {
	var group=document.getElementById(groupName);
	if (!group) {
		return;
	}
	if (disabled) {
		group.className='group-disabled';
	} else {
		group.className='';
	}
	var elements=group.getElementsByTagName('select');
	for(var i=0;i<elements.length;i++) {
		elements[i].disabled=disabled;
	}
	elements=group.getElementsByTagName('input');
	for(var i=0;i<elements.length;i++) {
		elements[i].disabled=disabled;
	}
}

/** Checkout - delbetaling GEMB - end */


/** Basket - product insurance - start */

function showProductInsuranceInfo() {
	var box=document.getElementById('product-insurance-info');
	if (box) {
		box.style.display='';
		return true;
	}
	return false;
}
function hideProductInsuranceInfo() {
	var box=document.getElementById('product-insurance-info');
	if (box) {
		box.style.display='none';
	}
	return false;
}

/** Basket - product insurance - end */


function showhide( elm_id ){
	var elm = document.getElementById( elm_id );
	var current_state=elm.style.display;
	if(current_state == 'none'){
		elm.style.display='';
	}else{
		elm.style.display='none';
	}
}

/** Show/hide an element if the given checkbox is checked or not.
 * @param elem_id The id of the element to show/hide.
 * @param checkbox The checkbox which checked status determines show or hide.
 * @param inverse Set to true if the visibility check should be inverted.
 */
function showhideIfChecked(elem_id,checkbox,inverse) {
	if (undefined==checkbox || !checkbox) { return; }
	if (undefined==checkbox.checked && undefined!=checkbox.length) {
		checkbox=document.getElementById(checkbox);
	}
	if (undefined==checkbox.checked) { return; }
	if (undefined==inverse) { inverse=false; }
	var elem=document.getElementById(elem_id);
	if (elem) {
		if(!checkbox.checked != !inverse) {
			elem.style.display='';
		} else {
			elem.style.display='none';
		}
	}
}

function showhideIf(elem_id,show) {

	var elem=document.getElementById(elem_id);
	if (elem) {
		if(show) {
			elem.style.display='';
		} else {
			elem.style.display='none';
		}
	}
}


function isLoading(on) {
	var scl=document.getElementById('loading-indicator');
	if (scl) {
		if (undefined==on || on) {
			scl.style.visibility='';
		} else {
			scl.style.visibility='hidden';
		}
	}
}


/*
Utsending av tips-eposter
Henter verdier fra en form og sender de inn til en XAJAX-funksjon med samme navn.
*/
function tipsEnVenn() {
	/* Hindrer at utålmodige folk sender ut flere e-poster til samme person */
	document.getElementById('SendButton').disabled = true;
	setTimeout("document.getElementById('SendButton').disabled = false;",5000);

	var form = document.getElementById('tips-en-venn');
	var dittNavn = '';
	var dinEpost = '';
	var mottakerensEpost = '';
	var tittel = '';
	var kommentar = '';
	var nodeId = '';

	for(var i=0;i<form.elements.length;i++) {
		switch(form.elements[i].name) {
			case 'YourName':
				dittNavn = form.elements[i].value;
				break;
			case 'YourEmail':
				dinEpost = form.elements[i].value;
				break;
			case 'ReceiversEmail':
				mottakerensEpost = form.elements[i].value;
				break;
			case 'Subject':
				tittel = form.elements[i].value;
				break;
			case 'Comment':
				kommentar = form.elements[i].value;
				break;
			case 'NodeID':
				nodeId = form.elements[i].value;
				break;
		}
	}
	xajax_tipsEnVenn(dittNavn,dinEpost,mottakerensEpost,tittel,kommentar,nodeId);
	return false;
}

/** Checkout - betaling - start */

function setChecked(id,check) {
	if (undefined==check) {
		check=true;
	}
	var el=document.getElementById(id);
	if (el) {
		el.checked=check;
	}
}

/** Checkout - betaling - end */

/** Alertbox - start **/

function AlertBox(title,body,id) {
    this._title = title;
    this._body = body;
    if(id != null) {
        this._id = id;
    } else {
        this._id = 'nxcalertbox';
    }
}
AlertBox.prototype._id = '';
AlertBox.prototype._title = '';
AlertBox.prototype._body = '';
AlertBox.prototype._showCancel = false;
AlertBox.prototype._showContinue = false;
AlertBox.prototype._showOK = false;
AlertBox.prototype._status = '';
AlertBox.prototype._clickId = false;
AlertBox.prototype._alertbox = null;
AlertBox.prototype.Show = function() {
    if(this._alertbox == null) {
        var alertbox = document.createElement('div');
        alertbox.setAttribute('id',this._id);
        alertbox.className = 'alertbox';
        alertbox.MessageBox = this;

        var title = document.createElement('div');
        title.className = 'title';
        title.appendChild(document.createTextNode(this._title));
        alertbox.appendChild(title);

        var body = document.createElement('div');
        body.className = 'body';
        body.appendChild(document.createTextNode(this._body));
        alertbox.appendChild(body);

        if(this._showOK) {
            var okButton = document.createElement('img');
            okButton.setAttribute('src','/extension/euronics/design/euronics/images/alert_ok.png');
            okButton.setAttribute('alt','OK');
            okButton.onclick = function() {
                this.parentNode.MessageBox._status = 'OK';
                this.parentNode.style.display = 'none';
            }
            alertbox.appendChild(okButton);
        }

        if(this._showCancel) {
            var cancelButton = document.createElement('img');
            cancelButton.setAttribute('src','/extension/euronics/design/euronics/images/alert_avbryt.png');
            cancelButton.setAttribute('alt','Cancel');
            cancelButton.onclick = function() {
                this.parentNode.MessageBox._status = 'Cancel';
                this.parentNode.style.display = 'none';
            }
            alertbox.appendChild(cancelButton);
        }

        if(this._showContinue) {
            var continueButton = document.createElement('img');
            continueButton.setAttribute('src','/extension/euronics/design/euronics/images/alert_fortsett.png');
            continueButton.setAttribute('alt','Continue');
            continueButton.onclick = function() {
                this.parentNode.MessageBox._status = 'Continue';
                this.parentNode.style.display = 'none';
                document.getElementById(this.parentNode.MessageBox._clickId).click();
            }
            alertbox.appendChild(continueButton);
        }

        document.getElementById('page').insertBefore(alertbox,document.getElementById('page').firstChild);
        this._alertbox = document.getElementById(this._id);
    }
    this._status = '';
	this._alertbox.style['left'] = '323px';
    this._alertbox.style['top'] = '220px';
    this._alertbox.style.display = 'block';
}

/** Alertbox - end **/

var warningProductNotAvailableAlertBox = new AlertBox('Beklager','Dette produktet er utsolgt og kan ikke kjøpes.');
warningProductNotAvailableAlertBox._showOK = true;

function warningProductNotAvailable() {
    window.warningProductNotAvailableAlertBox.Show();
}

var productNotInStockAlertBox = new AlertBox('OBS!','Dette produktet er ikke på lager. Du kan likevel fortsette for å bestille produktet.','notinstockmb');
productNotInStockAlertBox._showContinue = true;
productNotInStockAlertBox._showCancel = true;

function productNotInStock(buttonId) {
    window.productNotInStockAlertBox._clickId = buttonId;
    window.productNotInStockAlertBox.Show();
}


function showthankspage(text) {
 $.prompt( text );
 return false;
}


/** Coupon */

function setDiscount(e,obj) {
	var keynum;
	if (window.event) { // IE
		keynum=e.keyCode;
	} else if (e.which) { // Netscape/Firefox/Opera
		keynum=e.which;
	}
	if (keynum==13) {
		isLoading();
		xajax_checkcode(obj.value);
		obj.value='';
		return false;
	}
}

/** GEMB product calc popup **/
gembProductCalcPopupHeight=410;
gembProductCalcPopupWidth=570;

function onPageLoad() {
	
	xajax_populateUserinfo();
	
	
}



/**
add to wishlist action. Calls on product extnesion/euronics_09/..path..templates/listitem/* templates
*/
function addToWishlist(postto,node) {
    $.post(postto,{'ActionAddToWishList':'1','ContentObjectID':node,'customAction':1},function(data){
//        alert(data);
    });
}
