var cachedSourceElement;
var cachedSourceForm;
var cachedHTMLMessages=false;
var cachedHTMLMessageObject;
var slsh_objA;
var slsh_objB;
function submitInputform(cElem,cForm){
	cElem.disabled=true;
	if(cForm.className.indexOf('formParamsSaved')!=-1)
		cForm.className=cForm.className.replace("formParamsSaved","");
	if(cForm.className.indexOf('fieldsInError')!=-1){
		cForm.className=cForm.className.replace("fieldsInError","");
		var errInpts=cForm.getElementsByTagName('INPUT');
		for(var ii=0; ii<errInpts.length-1; ii++){
			if(errInpts[ii].className.indexOf('error')!=-1){
				errInpts[ii].className=errInpts[ii].className.replace("error","");
			}
		}
	}
	if(cachedHTMLMessages==true){
		cachedSourceForm.removeChild(cachedHTMLMessageObject);
		cachedHTMLMessages=false;
	}
	cachedSourceElement=cElem;
	cachedSourceForm=cForm;
	if(submitFormCheckErrorFields(cElem,cForm)==true) return;
	submitForm(cForm,cForm.action+"&export=1",submitFormSubmitted);
}

function submitFormCheckErrorFields(cElem,cForm){
	var oInps=cForm.getElementsByTagName('INPUT');
	var calcRF=0;
	var calcRFE=0;
	for(var ii=0; ii<oInps.length-1; ii++){
		if(oInps[ii].getAttribute('required')=='1' && oInps[ii].parentNode.parentNode.style.display==''){
			calcRF=calcRF*1+1;
			if(oInps[ii].value.length<1){
				calcRFE=calcRFE*1+1;
				if(oInps[ii].className.length>0) oInps[ii].className=oInps[ii].className+' error';
				else oInps[ii].className='error';
			}
		}
	}
	if(calcRFE>0){
		cForm.className='fieldsInError';
		cElem.disabled=false;
		showAjaxMessages(cForm.getAttribute('reqFldErrMsg'),'form',0);
	}
	return (calcRFE>0 ? true : false);

}

function submitFormSubmitted(xmlHttpReq){
	if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
		var returnText=xmlHttpReq.responseText;
		//if(document.getElementById('blablabox')!=null) alert(returnText);
		//alert(returnText);
		var tmpXML=xmlHttpReq.responseXML;
		var XML_mainParams=tmpXML.getElementsByTagName('submitFormTags')[0];
		var ajaxMsgText=XML_getNodeValue('errorMessage',XML_mainParams);
		var ajaxMsgType=XML_getNodeValue('messageType',XML_mainParams);
		var ajaxOKMsg=XML_getNodeValue('submitMessage',XML_mainParams);
		var ajaxKeepForm=XML_getNodeValue('keepFormVisible',XML_mainParams);
		
		if(XML_getNodeValue('ajaxGranted',XML_mainParams)==1){
			if(XML_getNodeValue('nexttarget',XML_mainParams)){
				window.location=XML_getNodeValue('nexttarget',XML_mainParams);
				return;
			}
		}
		if(XML_getNodeValue('fieldsInError',XML_mainParams)==1 && cachedSourceForm!=null){
			cachedSourceForm.className='fieldsInError';
		}
		if(XML_getNodeValue('runFunction',XML_mainParams))
			eval(XML_getNodeValue('runFunction',XML_mainParams));
		if(cachedSourceForm.getAttribute('errorScript')){
			eval(cachedSourceForm.getAttribute('errorScript')+"(cachedSourceForm);");
			cachedSourceElement.disabled=false;
			return;
		}
		if(ajaxKeepForm==1) cachedSourceForm.className='formParamsSaved';
		if(ajaxMsgText.length>1) showAjaxMessages(ajaxMsgText,ajaxMsgType,0);
		if(ajaxOKMsg.length>1) showAjaxMessages(ajaxOKMsg,ajaxMsgType,(ajaxKeepForm==1 ? 2 : 1));
		else cachedSourceElement.disabled=false;
	}
	
}

function showAjaxMessages(cMsg,cTyp,cMsgT){
	if(cTyp!='form'){
		alert(cMsg);
		return;
	}
	if(cachedSourceForm!=null){
		if(cMsgT==1){
			cachedSourceForm.innerHTML='<div class="submitMessageDIV">'+cMsg+'</div>';
		}else{
			var nextElem=cachedSourceForm.getElementsByTagName('INPUT')[0];
			var msgDiv=document.createElement('DIV');
			msgDiv.className=(cMsgT==2 ? 'submitMessageDIV' : 'errorMessageDIV');
			msgDiv.innerHTML=cMsg;
			cachedSourceForm.insertBefore(msgDiv,nextElem);
			cachedHTMLMessages=true;
			cachedHTMLMessageObject=msgDiv;
			if(cMsgT==2) cachedSourceElement.disabled=false;
		}
	}
}

function calcFundRatesAndSums(cElem,cSrc){
	var oRow=cElem.parentNode.parentNode;
	var oTbl=oRow.parentNode.parentNode;
	var oBaseSum=oTbl.rows[oRow.rowIndex+(cSrc=='basesum' ? 0 : -1)].cells.item(1).getElementsByTagName('INPUT')[0];
	var oFundRate=oTbl.rows[oRow.rowIndex+(cSrc=='basesum' ? 1 : 0)].cells.item(1).getElementsByTagName('INPUT')[0];
	var oFundSum=oTbl.rows[oRow.rowIndex+(cSrc=='basesum' ? 2 : 1)].cells.item(1).getElementsByTagName('INPUT')[0];
	oFundSum.value=fix_value(oBaseSum.value*oFundRate.value/100,2);

}

function switchMemberType(cElem){
	var oRow=cElem.parentNode.parentNode;
	var oTbl=oRow.parentNode.parentNode;
	document.getElementsByName('MEMBER[is_private]')[0].value=cElem.value;
	oTbl.rows[oRow.rowIndex+1].style.display=(cElem.value=='1' ? 'none' : '');
	oTbl.rows[oRow.rowIndex+2].style.display=(cElem.value=='1' ? 'none' : '');
	oTbl.rows[oRow.rowIndex+3].style.display=(cElem.value=='1' ? 'none' : '');
}


function submitRemoveFromCart(cId,cForm){
	submitForm(cForm,cForm.action+'&ajitemid='+cId+'&action=removeFromCart',submitRemoveFromCartSubmitted);
}
function submitRemoveFromCartSubmitted(xmlHttpReq){
	if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
		var returnText=xmlHttpReq.responseText;
		//alert(returnText);
		window.location=window.location.href;
	}
}

function openSlideShow(cId,cForm){
	setSlideshowWindow();
	submitForm(cForm,cForm.action+'&ajprid='+cId+'&action=slideShow',openSlideShowSubmitted);
}

function openSlideShowSubmitted(xmlHttpReq){
	if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
		var returnText=xmlHttpReq.responseText;
		slsh_objB.innerHTML=slideshowWindowContents()+returnText;
		$(function() {
			$('.ad-gallery').adGallery({
				loader_image: slideShow_var.loader_image,
				width: 830,
				height: 600,
				thumb_opacity: 1,
				start_at_index: 0,
				animate_first_image: true,
				animation_speed: 400,
				display_next_and_prev: true,
				display_back_and_forward: true,
				scroll_jump: 1,
				effect: slideShow_var.effect,
				enable_keyboard_move: true,
				cycle: true
			});
		});
	}
}

function slideshowWindowContents(cT){
	if(cT==null) var cT='';
	var htmlRet='<div style="width:100%; text-align:right; height:20px; background:gray;"><a href="javascript:void(0);" onclick="closeSlideshowWindow()" style="margin-right:2px;">[X]</a></div>';
	if(cT=='loading')
		htmlRet+='<div style="top:48%; left:48%; width:60px; height:60px; position:relative;"><img src="'+slideShow_var.loader_image+'" border=0></div>';
	return htmlRet;
}

function setSlideshowWindow(){
	var tmpDiv=document.createElement("DIV");
	var tmpDivB=document.createElement("DIV");
	var winHg=Math.round((get_windowSize()-30),0);
	if(winHg>600) winHg=600;
	var winTp=(get_windowScroll()+15);
	if(get_windowSize()-30>winHg) winTp=get_windowScroll()+Math.round((get_windowSize()-30-winHg)/2,0);
	slsh_objA=tmpDiv;
	slsh_objB=tmpDivB;
	tmpDiv.style.cssText='width:' + document.body.clientWidth+'px; height:' + document.body.clientHeight+'px; background:#373737; opacity:0.8;filter:alpha(opacity=80); position:absolute; top:0px; left:0px;z-index:1000; padding-bottom:3px;';
//	tmpDivB.style.cssText='z-index:1001; opacity:1;filter:alpha(opacity=100);position:absolute; float:center; width:800px; height:'+Math.round((get_windowSize()-30),0)+'px; background: white; top:'+(get_windowScroll()+15)+'px; left:'+Math.round(((get_windowSize('width')-800)/2),0)+'px;display:block; border:1px solid gray; overflow:hidden;';
	tmpDivB.style.cssText='z-index:1001; opacity:1;filter:alpha(opacity=100);position:absolute; float:center; width:800px; height:'+winHg+'px; background: white; top:'+winTp+'px; left:'+Math.round(((get_windowSize('width')-800)/2),0)+'px;display:block; border:1px solid gray; overflow:hidden;';
	tmpDivB.innerHTML=slideshowWindowContents('loading');
	tmpDiv.onclick=function(){closeSlideshowWindow();}
	document.body.scroll='no';
	var obElems=document.getElementsByTagName('OBJECT');
	if(obElems.length>0){
		obElems[0].parentNode.style.display='none';
	}
	//tmpDiv.appendChild(tmpDivB);
	document.body.appendChild(tmpDiv);
	document.body.appendChild(tmpDivB);
}

function closeSlideshowWindow(){
	slsh_objA.parentNode.removeChild(slsh_objB);
	slsh_objA.parentNode.removeChild(slsh_objA);
	var obElems=document.getElementsByTagName('OBJECT');
	if(obElems.length>0){
		obElems[0].parentNode.style.display='';
	}
	document.body.scroll='';
}

function updateDynamicElements(cElem,cRow,cForm){
	if(document.getElementById('element_addToCart_'+cRow)!=null){
		document.getElementById('element_addToCart_'+cRow).onclick=function(){
			cartClass.addToCart(cElem,cElem.value,cForm);
		}
	}
	if(document.getElementById('element_productOfferPrice_'+cRow)!=null){
		document.getElementById('element_productOfferPrice_'+cRow).innerHTML=cElem.options[cElem.options.selectedIndex].getAttribute('productOfferPrice');
	}

}


var cartClass=new function(){

	this.addToCart=function(cElem,cId,cForm){
		this.smallCart=document.getElementById('element_cartPreviewContent');
		if(document.getElementById('element_subProductPicker_'+cId)!=null){
			cId=document.getElementById('element_subProductPicker_'+cId).value;
		}
		eval("this.form="+cForm+";");
		this.ajax=new AjaxClassCollection(this.form.action,cForm,'cartClass.ajax','');
		this.triggers.setElement(cElem);
		//alert("OLI SIIN");
		this.triggers.disable();
		this.ajax.runAjax('addToCart','','&ajprid='+cId+'&action=addToCart','cartClass.addToCartSubmitted')
	}
	this.addToCartSubmitted=function(xmlHttpReq){
		if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
			//var returnText=xmlHttpReq.responseText;
			//alert(returnText);
			this.retXML=xmlHttpReq.responseXML;
			this.XML_mainParams=this.retXML.getElementsByTagName('submitFormTags')[0];
			this.flyElemInner=XML_getNodeValue('cartImage',this.XML_mainParams);
			this.flyToCart();			
		}
	}
	
	this.subProductPicked=function(cElem,cRow,cForm){
		if(document.getElementById('element_addToCart_'+cRow)!=null){
			document.getElementById('element_addToCart_'+cRow).onclick=function(){
				cartClass.addToCart(cElem,cElem.value,cForm);
			}
		}
		if(document.getElementById('element_productOfferPrice_'+cRow)!=null){
			document.getElementById('element_productOfferPrice_'+cRow).innerHTML=cElem.options[cElem.options.selectedIndex].getAttribute('productOfferPrice');
		}
	}

	this.updateSmallCart=function(){
		var ajaxMsgText=XML_getNodeValue('errorMessage',this.XML_mainParams);
		var ajaxMsgType=XML_getNodeValue('messageType',this.XML_mainParams);
		var ajaxOKMsg=XML_getNodeValue('submitMessage',this.XML_mainParams);
		var ajaxKeepForm=XML_getNodeValue('keepFormVisible',this.XML_mainParams);
		
		if(XML_getNodeValue('ajaxGranted',this.XML_mainParams)==1){
			if(this.smallCart==null){
				alert(ajaxOKMsg);
			}else{
				this.smallCart.innerHTML=XML_getNodeValue('htmlcontent',this.XML_mainParams);
			}
			
		}else{
			alert("Adding to cart failed ...");
		}
		this.triggers.enable();

	}

	this.flyToCart=function(){
		if(this.smallCart==null){
			this.updateSmallCart();
			return;
		}
		this.times=0;
		this.x1=this.triggers.x1;
		this.x2=findPosX(this.smallCart);
		this.y1=this.triggers.y1;
		this.y2=findPosY(this.smallCart);
		this.dx=this.x2-this.x1;
		this.dy=this.y2-this.y1;
		this.dif=Math.abs(this.dy/this.dx);
		this.fy=this.y1;
		this.fx=this.x1;
		this.ccx=0;
		this.ccy=0;
		this.speed=1;
		this.pxstep=30;
		this.flyElem=document.createElement('DIV');
		if(this.flyElemInner.length>2){
			this.flyElem.innerHTML=this.flyElemInner;
			this.flyElem.style.cssText='position:absolute;';
		}else{
			this.flyElem.style.cssText='width:50px; height:50px; position:absolute; background:yellow; border:1px dotted gray;';
		}
		document.body.appendChild(this.flyElem);
		this.proceedFly();
	}
	//findPosX
	this.proceedFly=function(){
		this.times=this.times+1;
		this.cx=(this.dx<0 ? -1 : 1)*this.pxstep*(this.dif<1 ? 1 : this.dif);
		this.cy=(this.dy<0 ? -1 : 1)*this.pxstep*(this.dif<1 ? this.dif : 1);
		this.ccx=this.ccx+this.cx;
		this.ccy=this.ccy+this.cy;
		this.fx=this.fx+this.cx;
		this.fy=this.fy+this.cy;
		this.updateFlyElem();
		if(this.times>1000 || Math.abs(this.ccy)>Math.abs(this.dy) || Math.abs(this.ccx)>Math.abs(this.dx)){
			this.updateSmallCart();
			this.flyElem.parentNode.removeChild(this.flyElem);
			return;
		}
		if(this.speed==0) this.proceedFly();
		else setTimeout('cartClass.proceedFly()',this.speed);
	}

	this.updateFlyElem=function(){
		this.flyElem.style.top=this.fy+"px";
		this.flyElem.style.left=this.fx+"px";
	}

	this.triggers=new function(){
		this.allElements;
		this.object;
		this.setElement=function(cElem){
			this.object=cElem;
			this.x1=findPosX(cElem);
			this.y1=findPosY(cElem);
		}
		this.disable=function(){
			this.runEnDis(true);

		}
		this.enable=function(){
			this.runEnDis(false);
		}
		
		this.runEnDis=function(cHU){
			if(cHU==true)
				this.allElements=document.getElementsByTagName(this.object.tagName);
			for(var ii=0; ii<this.allElements.length; ii++){
				if(this.allElements[ii].id.indexOf('element_addToCart')!=-1){
					if(this.object.tagName=='INPUT') this.allElements[ii].disabled=cHU;
					else if (this.object.tagName=='A'){
						this.allElements[ii].style.display=(cHU==true ? 'none' : '');
					}
				}
			}
		}
		
	}

}




