/**
 * 
 * standard.js
 *
 * Copyright (C) 2007, Continuum Creative Company
 * All rights reserved
 * 
 * http://www.continuum-cc.de
 * 
 */
 
/*------------------------- image swap -----------------------*/
function swapImage(imgName,newImg) {
        if ((navigator.appName == 'Netscape' && parseFloat(navigator.appVersion) >= 3) || (parseFloat(navigator.appVersion) >= 4)){
                eval('document.' + imgName + '.src = "img/' + newImg + '"');
        }
}

/*------------------------- print email ----------------------*/
function printEmail(alias, style) {

	var prefix = '&#064;mika-personal&#046;de';
	var full   = alias + prefix;

	document.write('<a href=\"mailto:' + full + '\"');
	if(style != '') {
		document.write(' class=\"' + style + '\"');
	}
	document.write('>');
	document.write(full);
	document.write('</a>');
}

/*------------------------- ajax frame and calls -------------*/
function getRegion() {

	// the vars
	var debug   = false;
	var bereich = document.getElementById('bereich').value;
	var newRegion = document.getElementById('region');
	var newLine;
	var newEntry;
	var newAtt;
	
	// removing old list
	theSelectCleaner('region');

	// debug
	if(debug == true) alert(bereich);
	
	// call the server
	var http = null;
	if (window.XMLHttpRequest) {
    	http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
   		http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (http != null) {
   		http.open("GET", "facade/ajaxframe.php?_action=getActiveRegion&_id=" + bereich, true);
   		http.onreadystatechange = ausgeben;
   		http.send(null);
	}
	
	function ausgeben() {
	   	if (http.readyState == 4) {
	   		if(debug == true) alert(http.responseText)
	   	    var data   = http.responseXML;
	   	    var result = data.getElementsByTagName("set");

			var newHTML = "";
	   	    if(debug == true) alert("length: " + result.length);
	   	    
	   	    for (var i = 0; i < result.length; i++) {
	   	    	var counter, theId, description;
	   	    	
	   	    	var thisResult = result[i];
	   	    	
	   	    	for (var j = 0; j < thisResult.childNodes.length; j++) {
            		with (thisResult.childNodes[j]) {
               			if (nodeName == "count") {
                  			counter = firstChild.nodeValue;
               			} else if (nodeName == "id") {
                  			theId = firstChild.nodeValue;
               			} else if (nodeName == "text") {
                  			description = firstChild.nodeValue;
               			}
            		}
            		                
         		}
         		newAtt = document.createAttribute('value')
         		newAtt.nodeValue = theId;
         		newLine = document.createElement("option");
         		newEntry = document.createTextNode(description + ' (' + counter + ')');
         		newLine.appendChild(newEntry);
         		newLine.setAttributeNode(newAtt);
         		newRegion.appendChild(newLine);
			}
	   	}
	}
}

/*------------------------- clean any select box -------------*/
function theSelectCleaner(theId) {
	var master;
	try {
		while(master = document.getElementById(theId).firstChild) {
			document.getElementById(theId).removeChild(master);
		}
	} catch (e) {
		// any error
	}
}

/**
 * Get the job list overview
 **/
function getPostingOverviewList(strBereich, strRegion, boolManual) {

	// the vars
	var debug = false;
	if(boolManual == true) {
		region = strRegion;
		bereich = strBereich; } 
	else {
		var bereich = document.getElementById("bereich").value;
		var region  = document.getElementById("region").value;
	}
	var theCounter, theId, theTitle, theRegion, theText;
	
	
	if(debug==true) alert('Bereich: ' + bereich + '\nRegion: ' + region);
	
	// call the server
	var http = null;
	if (window.XMLHttpRequest) {
    	http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
   		http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (http != null) {
   		http.open('GET', 'facade/ajaxframe.php?_action=getMyList&_region=' + region + '&_bereich=' + bereich, true);
   		http.onreadystatechange = ausgeben;
   		http.send(null);
	}
	
	function ausgeben() {
	   	if (http.readyState == 4) {
	   		var data    = http.responseXML;	   		
	   		
	   		if(debug == true) alert(http.responseText);
	   		
	   		var result  = data.getElementsByTagName("set");
	   		var newHTML = "";
	   	    
	   	    for (var i = 0; i < result.length; i++) {
	   	    	var counter, theId, description;
	   	    	
	   	    	var thisResult = result[i];
	   	    	
	   	    	for (var j = 0; j < thisResult.childNodes.length; j++) {
            		with (thisResult.childNodes[j]) {
               			if (nodeName == "count") {
                  			theCounter = firstChild.nodeValue;
               			} else if (nodeName == "id") {
                  			theId = firstChild.nodeValue;
               			} else if (nodeName == "title") {
                  			theTitle = firstChild.nodeValue;
               			} else if (nodeName == "region") {
                  			theRegion = firstChild.nodeValue;
               			}
            		}
            		                
         		}
         		newHTML += '<div style=\"position: relative; margin-top: 3px; margin-top: 3px; border-bottom: 1px solid #505050; float: left; width: 510px;\">';
         		newHTML += '<div style=\"float: left; width: 30px;\">&gt;&gt;</div>' ;
         		newHTML += '<div style=\"float: left; width: 320px; \">';
         		newHTML += '<a href=\"jobs.php?_sub=2&_posting=' + theId + '&_bereich=' + bereich + '&_region=' + region +'\">' + theTitle + '</a></div>' ;
         		newHTML += '<div style=\"float: left; text-align: right; width: 160px;\">' + theRegion + '</div>' ;
         		newHTML += '</div>';
			}
			document.getElementById('joblist').innerHTML = newHTML;
			if(debug == true) alert(newHTML);
	   	}
	}
}

function addExperienceBlock(thisArea) {
	
	if(thisArea == 'initiativ') {
	newStyle = 'initiativbutton'; } else {
	newStyle = 'jobbutton'; }
	
	var newHTML;
	newHTML  = '<a onclick=\"removeExperienceBlock(this);\" class=\"' + newStyle + '\">Eintrag l&ouml;schen</a>';
	newHTML += '<!-- 1st line --><div class=\"bewerbung-experience-block\">';
	newHTML += '<div class=\"bewerbung-experience-block1\">Monat/Jahr<br/><input type=\"text\" name=\"exp_date_from[]\" id=\"exp_date_from\" value=\"\" class=\"bewerbung-input-experience-block1\" /></div>';
	newHTML += '<div class=\"bewerbung-experience-block2\">bis</div>';
	newHTML += '<div class=\"bewerbung-experience-block3\">Monat/Jahr<br/><input type=\"text\" name=\"exp_date_top[]\" id=\"exp_date_to\" value=\"\" class=\"bewerbung-input-experience-block1\" /></div>';
	newHTML += '<div class=\"bewerbung-experience-block4\">Firma<br/><input type=\"text\" name=\"exp_firma[]\" id=\"exp_firma\" value=\"\" class=\"bewerbung-input-experience-block4\" /></div>';
	newHTML += '<div class=\"bewerbung-experience-block5\">Ort<br/><input type=\"text\" name=\"exp_ort[]\" id=\"exp_ort\" value=\"\" class=\"bewerbung-input-experience-block5\" /></div>';
	newHTML += '</div>';
	newHTML += '<!-- 2nd line --><div class=\"bewerbung-experience-block\">';
	newHTML += '<div class=\"bewerbung-mm-m\">T&auml;tigkeit<br/><input type=\"text\" name=\"exp_description[]\" id=\"exp_description\" value=\"\" class=\"bewerbung-input-m\" /></div>';
	newHTML += '<div class=\"bewerbung-mm-s\"> </div>';
	newHTML += '<div class=\"bewerbung-mm-m\">Wechselgrund (z.B. Insolvenz)<br/><input type=\"text\" name=\"exp_reason[]\" id=\"exp_reason\" value=\"\" class=\"bewerbung-input-m\" /></div>';
	newHTML += '</div>';
	
	var div = document.createElement("div");
	div.id = 'item_experience';
	div.innerHTML = "<div class=\"bewerbung-cnt\">"+newHTML+"</div>";
	document.getElementById('experience').appendChild(div);
}

function removeExperienceBlock(el) {
	var d = el.parentNode.parentNode;
	document.getElementById('experience').removeChild(d);
}

function addFortbildungBlock(thisArea1) {
	if(thisArea1 == 'initiativ') {
	newStyle = 'initiativbutton'; } else {
	newStyle = 'jobbutton'; }

	var newHTML;
	newHTML  = '<a onclick=\"removeFortbildungBlock(this);\" class=\"' + newStyle + '\">Eintrag l&ouml;schen</a>';
	newHTML += '<div class=\"bewerbung-msl-m\">Institut<br/><input type=\"text\" name=\"fortbildung_institut[]\" id=\"fortbildung_institut\" value=\"\" class=\"bewerbung-input-msl-m\" /></div>';
	newHTML += '<div class=\"bewerbung-msl-s\">Ort<br/><input type=\"text\" name=\"fortbildung_ort[]\" id=\"fortbildung_ort\" value=\"\" class=\"bewerbung-input-msl-s\" /></div>';
	newHTML += '<div class=\"bewerbung-msl-l\">Abschluss<br/><input type=\"text\" name=\"fortbildung_abschluss[]\" id=\"fortbildung_abschluss\" value=\"\" class=\"bewerbung-input-m\" /></div>';

	var div = document.createElement('div');
	div.id = 'item_training';
	div.innerHTML = '<div class=\"bewerbung-cnt\">'+newHTML+'</div>';
	document.getElementById('training').appendChild(div);
}

function removeFortbildungBlock(eb) {
	var e = eb.parentNode.parentNode;
	document.getElementById('training').removeChild(e);
}

/**
 * Deaktivate Stellenbeschreibung
 **/
function deaktivateJob(id) {
	window.location.href = '?_id=' + id + '&_a=save&_s=true';
}

/**
 * Aktivate Stellenbeschreibung
 **/
function aktivateJob(id) {
	window.location.href = '?_id=' + id + '&_a=save&_s=false';
}

/**
 * Delete Stellenbeschreibung
 **/
function deleteJob(id) {
	window.location.href = '?_id=' + id + '&_a=save&_d=true';
}

/**
 * Validate the posted form
 **/
function postApplication(thisArea) {
	var strError = '';
	var newHTML = '';
	
	// remove the children first
	try {
		var me = document.getElementById('errormessage').firstChild;
		document.getElementById('errormessage').removeChild(me);
	} catch (e) {
		// nothing
	}
	if(thisArea=='initiativ') {
		  thisStyle = 'bewerbung'; 
	} else {
			thisStyle = 'jobs'; 
	}
	
	if(thisArea=='initiativ') {
		if(document.frmApp.vorname.value == '') {
			strError += '<li class=\"' + thisStyle + '\">gewünschte T&auml;tigkeit</li>';
		}
	}
	if(document.frmApp.vorname.value == '') {
		strError += '<li class=\"' + thisStyle + '\">Vorname</li>';
	}
	if(document.frmApp.nachname.value == '') {
		strError += '<li class=\"' + thisStyle + '\">Nachname</li>';
	}
	if(document.frmApp.strasse.value == '') {
		strError += '<li class=\"' + thisStyle + '\">Strasse</li>';
	}
	if(document.frmApp.hausnummer.value == '') {
		strError += '<li class=\"' + thisStyle + '\">Hausnummer</li>';
	}
	if(document.frmApp.plz.value == '') {
		strError += '<li class=\"' + thisStyle + '\">Postleitzahl</li>';
	}
	if(document.frmApp.ort.value == '') {
		strError += '<li class=\"' + thisStyle + '\">Ort</li>';
	}
	if(document.frmApp.schulbildung.value == '_false') {
		strError += '<li class=\"' + thisStyle + '\">Schulabschluss</li>';
	}
	if(document.frmApp.telefon.value == '') {
		strError += '<li class=\"' + thisStyle + '\">Telefon</li>';
	}
	if(document.frmApp.currentjob.value == '') {
		strError += '<li class=\"' + thisStyle + '\">derzeitige T&auml;tigkeit</li>';
	}
	
	if(strError.length > 0) {
	  newHTML = '<p>Bitte &uuml;berpr&uuml;fen Sie folgende Eintr&auml;ge';
		newHTML += '<ul class=\"incopy\" style=\"top: 0px;\">';
		newHTML += strError;
		newHTML += '</ul></p>'; 	

		var div = document.createElement('div');
		div.id = 'item_error';
		div.innerHTML = '<div class=\"bewerbung-cnt\"'+newHTML+'</div>';
		document.getElementById('errormessage').appendChild(div);
		window.location.href = "#top";
		return false;
	}
	return true;
}

/**
 * Validate E-Mail Address
 **/
function validEmail(email) {
  var strReg = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$";
  var regex = new RegExp(strReg);
  return(regex.test(email));
}
