/*
############################################################
##
## 2007 (c) PiSolution GmbH
##
## File    : $Id: reload.js,v 1.14 2011/09/27 09:04:07 crestorff Exp $
## Version : $Revision: 1.14 $
##
## Purpose : Projekt - direktaufladen.de
##
## Created : 07/01/01
## Author  : frank pietzsch
##
## Update  : $Date: 2011/09/27 09:04:07 $
## Author  : $Author: crestorff $
## 
## Remarks : js for reload page
##
## Log     : $Log: reload.js,v $
## Log     : Revision 1.14  2011/09/27 09:04:07  crestorff
## Log     : bugfix: elements after gap in id were not resized
## Log     :
## Log     : Revision 1.13  2011/09/23 15:59:38  frank
## Log     : round the calculated size
## Log     :
## Log     : Revision 1.12  2011/09/23 14:44:53  frank
## Log     : *** empty log message ***
## Log     :
## Log     : Revision 1.11  2011/09/23 12:01:53  crestorff
## Log     : modified all pages for mobile devices
## Log     :
## Log     : Revision 1.10  2011/09/22 13:03:16  crestorff
## Log     : changed layout for product-selection-page
## Log     : better fit with mobile devices
## Log     :
## Log     : Revision 1.9  2011/09/21 13:51:31  crestorff
## Log     : change of width for mobile devices
## Log     :
## Log     : Revision 1.8  2011/07/18 06:55:10  frank
## Log     : check with javascript if the frameset exist
## Log     :
## Log     : Revision 1.7  2011/07/08 11:09:41  frank
## Log     : *** empty log message ***
## Log     :
## Log     : Revision 1.6  2011/07/08 10:29:25  frank
## Log     : make the callbacklink functionality smaller
## Log     :
## Log     : Revision 1.5  2011/07/05 14:38:59  frank
## Log     : add callback functionality for partner shops
## Log     :
## Log     : Revision 1.4  2010/11/09 17:41:43  frank
## Log     : BUG FIXING: load only existing images
## Log     :
## Log     : Revision 1.3  2010/11/09 17:29:14  frank
## Log     : changes after new SEO
## Log     :
## Log     : Revision 1.2  2010/07/30 14:05:41  frank
## Log     : add merchant parameter to the function: select
## Log     :
## Log     : Revision 1.1.1.1  2010/06/25 09:51:38  frank
## Log     : initial revision
## Log     :
##
############################################################
*/

var next;
var clicked = false;

var ImgArray = new Array();
	ImgArray['images/aufladen.jpg'] = new Image();
	ImgArray['images/aufladen.jpg'].src = 'images/aufladen.jpg';
	ImgArray['images/Aufladen_inaktiv.jpg'] = new Image();
	ImgArray['images/Aufladen_inaktiv.jpg'].src = 'images/Aufladen_inaktiv.jpg';

function changeImage(pic_prod,pic_sup,id_prod,id_sup)
{
	if (ImgArray[pic_prod])
		document.getElementById(id_prod).style.backgroundImage = "url(" + ImgArray[pic_prod].src + ")";
	if (ImgArray[pic_sup])
		document.getElementById(id_sup).style.backgroundImage = "url(" + ImgArray[pic_sup].src + ")";
}

function select(merchant,supId,prId,catId)
{
	document.forms[0].selectedMerchant.value = merchant; 
	document.forms[0].selectedSupplier.value = supId; 
	document.forms[0].selectedProduct.value  = prId;
	document.forms[0].category_id.value      = catId;
	document.forms[0].action.value           = "step2";
	document.forms[0].submit();
}

//function for resizing

function resize(n,element_count)
{
	var i=0;
	var j=0;
	var size;
	// change the size of the 'container' if window-width is greater than 400
	if(window.innerWidth >= 400)
	{
		document.getElementById('mobile_body_container').style.width = window.innerWidth+'px';
		document.getElementsByTagName("p")[0].setAttribute("align","left"); //funktioniert...schreibt das x auf die linke seite
	}
	else
	{
		document.getElementById('mobile_body_container').style.width = '400px';
	}
	//image on agreement
	if(document.getElementById('a0'))
	{
		document.getElementById('a0').style.width = ((window.innerWidth)/5)+'px';
		document.getElementById('a0').style.height = (((window.innerWidth/5)/80)*54)+'px';
	}
	//image on payment
	if(document.getElementById('p0'))
	{
		document.getElementById('p0').style.width = ((window.innerWidth)/5)+'px';
		document.getElementById('p0').style.height = (((window.innerWidth/5)/80)*54)+'px';
	}
	//next button
	if(document.getElementById('next'))
	{
		document.getElementById('next').style.width = ((window.innerWidth)/4)+'px';
		//document.getElementById('next').style.height = (((window.innerWidth/4)/80)*54)+'px';
	}
	//back-button
	if(document.getElementById('img_back'))
	{
		document.getElementById('img_back').style.width = ((window.innerWidth)/4)+'px';
		//document.getElementById('next').style.height = (((window.innerWidth/4)/80)*54)+'px';
	}
	//image for sofortüberweisung.de
	document.getElementById('img_payment').style.width = ((window.innerWidth)/5)+'px';
	document.getElementById('img_payment').style.height = (((window.innerWidth/5)/170)*55)+'px';
	//help-image
	if(document.getElementById('h0'))
	{
		document.getElementById('h0').style.width = ((window.innerWidth)/5)+'px';
		document.getElementById('h0').style.height = (((window.innerWidth/5)/80)*54)+'px';
	}
	// progress_images
	do
	{
		if(document.getElementById('img_prog'+i))
		{
			document.getElementById('img_prog'+i).style.width = (window.innerWidth/5)+'px';
		}
		else
		{
			break;
		}
		i++;
	}while(i)
	i=0;
	// change size of elements (pictures and text)
	// categories first
	do
	{
		if(document.getElementById('cat'+i))
		{
			size = Math.round(((window.innerWidth/4)/80)*15);
			document.getElementById('cat'+i).style.fontSize = (size-5)+'px';
			document.getElementById('cat'+i).style.lineHeight = size+'px';
		}
		else
		{
			break;
		}
		i++;
	}while(i)
	//bottomline
	i=0;
	do
	{
		if(document.getElementById('bot'+i))
		{
			size = Math.round(((window.innerWidth/6)/80)*15);
			document.getElementById('bot'+i).style.fontSize = (size-5)+'px';
		}
		else
		{
			break;
		}
		i++;
	}while(i)
	i=0;
	if(typeof(element_count) == "undefined")
	{
		element_count = 0;
	}
	for(var i = 0; i < element_count; i++)
	{
		var j=0;
		// determine wether an element exists, else cancel resizing
		if(document.getElementById('s'+i))
		{
			//resize price-labes, too
			do
			{
				if(document.getElementById('p'+i+j))
				{
					if(window.innerWidth >= 400)
					{
						document.getElementById('s'+i).style.width = (window.innerWidth/n)+'px';
						size = Math.round(((window.innerWidth/n)/80)*54);
						document.getElementById('s'+i).style.height = size+'px';
						document.getElementById('p'+i+j).style.width = (window.innerWidth/n)+'px';
						size = Math.round(((window.innerWidth/n)/80)*15);
						document.getElementById('p'+i+j).style.height = size+'px';
						document.getElementById('p'+i+j).style.fontSize = (size-5)+'px';
					}
					//minimum size for pictures and labels
					else if(window.innerWidth < 400)
					{
						document.getElementById('s'+i).style.width = '80px';
						document.getElementById('s'+i).style.height = '54px';
						document.getElementById('p'+i+j).style.width = '80px';
						document.getElementById('p'+i+j).style.height = '15px';
					}
				}
				else
				{
					break;
				}
				j++;
			}while(j);
		}
		else
		{
			continue;
		}
	}
}

function changecellcolor(cell,key)
{
	if(key == 'active')
	{
		document.getElementById(cell).style.backgroundColor = '#303133';
	}
	else if(key == 'passive')
	{
		document.getElementById(cell).style.backgroundColor = '#78787A';
	}
}

function goBack(cbl)
{
	if (cbl && parent.frames.length == 0)
	{
		this.location.href=cbl;
	}
	else
	{
		document.forms[0].selectedSupplier.value = -1; 
		document.forms[0].selectedProduct.value  = -1;
		document.forms[0].action.value           = "step1";
		document.forms[0].submit();
	}
	return false;
}

function goBackAsk(cbl)
{
	if (confirm('Haben Sie sich die Kaufbestätigung ausgedruckt ?'))
	{
		if (cbl && parent.frames.length == 0)
		{
			this.location.href=cbl;
		}
		else
		{
			document.forms[0].action.value           = "step1";
			document.forms[0].submit();
		}
	}
	return false;
}

function a_next(reload_method)
{
	if(reload_method == 1  && ! checkPhoneNumber())
		return false;
    if (next)
	{
		document.forms[0].action.value = "step3";
		document.forms[0].submit();
	}
    else window.alert("Akzeptieren Sie bitte die AGBs !");
	return false;
}

function p_next()
{
	if (! clicked)
	{
		clicked = true;
		document.forms[0].action.value = "step4";
		document.forms[0].submit();
	}
	return false;
}

function reverse()
{

	if (document.forms[0].agreeAGB.checked) 
		next = true;
	else 
		next = false;

    if (next) 
		pic = "images/aufladen.jpg";
    else 
		pic = "images/Aufladen_inaktiv.jpg";

    document.getElementById("next").src = ImgArray[pic].src;
}

function checkPhoneNumber()
{
	if(! document.forms[0].phoneNumber.value.match(/^0\d{10,11}\s*$/))
	{
		alert("Format Fehler:\n\nBitte geben Sie Ihre Telefon Nummer wie folgt ein!\n01710123456 [Beispiel Nummer]");
		document.forms[0].phoneNumber.focus();
		return false;
	}
	if(document.forms[0].phoneNumber.value != document.forms[0].repeat_phoneNumber.value)
	{
		alert("Bitte überprüfen Sie nochmals Ihre Rufnummer!");
		return false;
	}

	return true;
}
function retryDirectReload(txid)
{
	if(! document.forms[0].msisdn.value.match(/^0\d{10,11}\s*$/))
	{
		alert("Format Fehler:\n\nBitte geben Sie Ihre Telefon Nummer wie folgt ein!\n01710123456 [Beispiel Nummer]");
		document.forms[0].msisdn.focus();
		return false;
	}
	if(document.forms[0].msisdn.value != document.forms[0].repeat_msisdn.value)
	{
		alert("Bitte überprüfen Sie Ihre Eingabe!");
		return false;
	}
	document.forms[0].txid.value = txid;
	document.forms[0].action.value = "retryDirectReload";
	document.forms[0].submit();
}

function selectCategory(cid)
{
	var form = document.forms[0];
	form.category_id.value = cid;
	form.submit();
	return false;
}

function book_mark(url, titel)
{
	var app = navigator.appName;

	if (app == "Microsoft Internet Explorer")
	{
		window.external.AddFavorite(url,titel)
	}
	else
	{
		if((app == "Netscape") &&
           (typeof window.sidebar=="object") &&
           (typeof window.sidebar.addPanel=="function"))
		{
			window.sidebar.addPanel(titel,url,"");
		}
		else
		{
			var msg='Bookmark: Drücken Sie Strg+D!';
			alert(msg);
		}
	}
}

function toggle_row(div_id)
{
	if (document.getElementById(div_id).style.display == "none")
	{
		document.getElementById(div_id).style.display = "";
	}
	else
	{
		document.getElementById(div_id).style.display = "none";
	}
}

function toggle_div(div_id, position)
{
	if (position == "") position = "120px";
	if (document.getElementById(div_id).style.left[0] == "-")
	{
		document.getElementById(div_id).style.left = position;
	}
	else
	{
		document.getElementById(div_id).style.left = "-1000px";
	}
}

var objDrag = null;     // Element, über dem Maus bewegt wurde
var mouseX   = 0;       // X-Koordinate der Maus
var mouseY   = 0;       // Y-Koordinate der Maus
var offX = 0;           // X-Offset der Maus zur linken oberen Ecke des Elements
var offY = 0;           // Y-Offset der Maus zur linken oberen Ecke des Elements

// Browserweiche
IE = document.all&&!window.opera;
DOM = document.getElementById&&!IE;

// Initialisierungs-Funktion
function init()
{
	// Initialisierung der Überwachung der Events
	document.onmousemove = doDrag;  // Bei Mausbewegung die Fkt. doDrag aufrufen
	document.onmouseup = stopDrag;  // Bei Loslassen der Maustaste die Fkt. stopDrag aufrufen
}

// Wird aufgerufen, wenn die Maus über einer Box gedrückt wird
function startDrag(objElem)
{
	// Objekt der globalen Variabel zuweisen -> hierdurch wird Bewegung möglich
	objDrag = objElem;

	// Offsets im zu bewegenden Element ermitteln
	offX = mouseX - objDrag.offsetLeft;
	offY = mouseY - objDrag.offsetTop;
}

// Wird ausgeführt, wenn die Maus bewegt wird
function doDrag(ereignis)
{
	// Aktuelle Mauskoordinaten bei Mausbewegung ermitteln
	mouseX = (IE) ? window.event.clientX : ereignis.pageX;
	mouseY = (IE) ? window.event.clientY : ereignis.pageY;

	// Wurde die Maus über einem Element gedrück, erfolgt eine Bewegung
	if (objDrag != null)
	{
		objDrag.style.cursor = "move";

		// Element neue Koordinaten zuweisen
		objDrag.style.left = (mouseX - offX) + "px";
		objDrag.style.top = (mouseY - offY) + "px";

		// Position in Statusleiste ausgeben
		window.status = "Box-Position: " + objDrag.style.left + ", " + objDrag.style.top;
	}
}

// Wird ausgeführt, wenn die Maustaste losgelassen wird
function stopDrag(ereignis)
{
	// Objekt löschen -> beim Bewegen der Maus wird Element nicht mehr verschoben
	if (objDrag != null)
	{
		objDrag.style.cursor  = "pointer";
	}
	objDrag = null;
}

function send_contact()
{
	if (!isEmpty(document.handyaufladen.contact_date) &&
        isDate(document.handyaufladen.contact_date) &&
        !isEmpty(document.handyaufladen.contact_time) &&
        !isEmpty(document.handyaufladen.contact_product) &&
        !isEmpty(document.handyaufladen.contact_amount,1,6,1) &&
        isNumber(document.handyaufladen.contact_amount,5,100) &&
        !isEmpty(document.handyaufladen.contact_txid,6,7) &&
        isNumber(document.handyaufladen.contact_txid) &&
        !isEmpty(document.handyaufladen.contact_phone) &&
        isPhoneNumber(document.handyaufladen.contact_phone) &&
        !isEmpty(document.handyaufladen.contact_email) &&
        isEmail(document.handyaufladen.contact_email) &&
        !isEmpty(document.handyaufladen.contact_name) &&
        !isEmpty(document.handyaufladen.contact_more))
	{
		document.handyaufladen.action.value = 'contact';
		document.handyaufladen.submit();
	}
}

