// JavaScript Document
// Javacript for "find" page
courseType = "";
courseID = "";
basePrice = 0;	
accommodPrice = 0;
seminarPrice = 0;
pickupPrice = 0;
insPrice = 0;
enrollFee = 52;

exchangeFactor = 1;
strg = "curr_c,curr_a,curr_s,curr_t,curr_p,curr_i,curr_f";
CurrencySymbolsCells = strg.split(",");

function VF_search(){ //v2.0
<!--start_of_saved_settings-->
<!--type,select,name,pickup,isDefault,errMsg,Please choose a pickup option.-->
<!--type,select,name,insurance,isDefault,errMsg,Please choose an insurance option.-->
<!--type,select,name,accommodation,isDefault,errMsg,Please choose your accommodation.-->
<!--type,select,name,duration,isDefault,errMsg,Please select course length.-->
<!--type,select,name,level,isDefault,errMsg,Please choose your German level.-->
<!--type,select,name,start,isDefault,errMsg,Please choose a start date.-->
<!--type,select,name,coursetype,isDefault,errMsg,Please choose a course.-->
<!--end_of_saved_settings-->
	var theForm = document.ANMELDUNG;
	var sFlg_pickup = false;
	var sFlg_insurance = false;
	var sFlg_accommodation = false;
	var sFlg_seminar = false;
	var sFlg_duration = false;
	var sFlg_level = false;
	var sFlg_start = false;
	var sFlg_coursetype = false;
	var errMsg = "";
	var setfocus = "";

		var theCourse = objCourse.selectedIndex - 1;
		if ( theCourse >= 0) {
			courseType = course[0+theCourse][2];
			courseID = course[0+theCourse][0];
		} else {
		    courseType = '';
			courseID = '';
		}

	for(var s6=0;s6<theForm['pickup'].length;s6++){if(theForm['pickup'].options[s6].selected){if(theForm['pickup'].options[s6].text==theForm['pickup'].options[0].text)sFlg_pickup=true;}}
	for(var s5=0;s5<theForm['insurance'].length;s5++){if(theForm['insurance'].options[s5].selected){if(theForm['insurance'].options[s5].text==theForm['insurance'].options[0].text)sFlg_insurance=true;}}
	for(var s4=0;s4<theForm['accommodation'].length;s4++){if(theForm['accommodation'].options[s4].selected){if(theForm['accommodation'].options[s4].text==theForm['accommodation'].options[0].text)sFlg_accommodation=true;}}
	for(var s7=0;s7<theForm['seminar'].length;s7++){if(theForm['seminar'].options[s7].selected){if(theForm['seminar'].options[s7].text==theForm['seminar'].options[0].text)sFlg_seminar=true;}}
    for(var s3=0;s3<theForm['duration'].length;s3++){if(theForm['duration'].options[s3].selected){if(theForm['duration'].options[s3].text==theForm['duration'].options[0].text)sFlg_duration=true;}}
	for(var s2=0;s2<theForm['level'].length;s2++){if(theForm['level'].options[s2].selected){if(theForm['level'].options[s2].text==theForm['level'].options[0].text)sFlg_level=true;}}
	for(var s1=0;s1<theForm['start'].length;s1++){if(theForm['start'].options[s1].selected){if(theForm['start'].options[s1].text==theForm['start'].options[0].text)sFlg_start=true;}}
	for(var s0=0;s0<theForm['coursetype'].length;s0++){if(theForm['coursetype'].options[s0].selected){if(theForm['coursetype'].options[s0].text==theForm['coursetype'].options[0].text)sFlg_coursetype=true;}}

	if (sFlg_pickup && courseType != 'I'){
		errMsg = "Please choose a pickup option\.";
		setfocus = "['pickup']";
	}
	if (sFlg_insurance && courseType != 'I'){
		errMsg = "Please choose an insurance option\.";
		setfocus = "['insurance']";
	}
	
	if (sFlg_seminar && courseType != 'I') {
		errMsg = "Please choose a seminar\.";
		setfocus = "['seminar']";
	}
	
	if (sFlg_accommodation && courseType != 'I'){
		errMsg = "Please choose your accommodation\.";
		setfocus = "['accommodation']";
	}
	if (sFlg_duration && courseType != 'I'){
		errMsg = "Please select course length\.";
		setfocus = "['duration']";
	}
	if (sFlg_level){
		errMsg = "Please choose your German level\.";
		setfocus = "['level']";
	}
	if (sFlg_start){
		errMsg = "Please choose a start date\.";
		setfocus = "['start']";
	}
	if (sFlg_coursetype){
		errMsg = "Please choose a course\.";
		setfocus = "['coursetype']";
	}
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	else theForm.submit();
}

function SetList(objList, theValue) {
	for (var ii = 0; ii < objList.options.length; ii++) {
		if (objList.options[ii].value == theValue) {
			return ii;
		}
	}
	return 0;
}

function ClearSession() {
     for(var i=0; i<9; ++i) {
	 	sess[i] = "";
	 }
}

function SetItUp() {
	objDuration = document.getElementById('duration')
	objCourse = document.getElementById('coursetype')
	objStart = document.getElementById('start');
	objLevel = document.getElementById('level');
	objAccomm = document.getElementById('accommodation');
	objSeminar = document.getElementById('seminar');
	objPickup = document.getElementById('pickup');
	objCurrency = document.getElementById('currency');
	objIns = document.getElementById('insurance');


	cellCourse = document.getElementById('a_course');
	cellAccomm = document.getElementById('a_accomm');
	cellSeminar = document.getElementById('a_seminar');
	cellPickup = document.getElementById('a_pickup');
	cellIns = document.getElementById('a_ins');
	cellFee = document.getElementById('a_fee');
    cellTotal = document.getElementById('a_total')

	cellCourse.innerHTML = "";
	cellAccomm.innerHTML = "";
	if (cellSeminar) cellSeminar.innerHTML = "";
	cellPickup.innerHTML = "";
	cellIns.innerHTML = "";
	cellFee.innerHTML = "";
    cellTotal.innerHTML = "";

	if (sess[0] != "") {
		objCourse.selectedIndex =  SetList(objCourse, sess[0]);

		var theCourse = objCourse.selectedIndex - 1;
		if ( theCourse >= 0) {
			courseType = course[0+theCourse][2];
		} else {
		    courseType = '';
		}

		PopulateDates();
		PopulateDuration();
		PopulateLevel();
		PopulateAccomm();
		PopulateIns();
		if (objSeminar) PopulateSeminar();
		PopulatePickup();
		PopulateCurrency();
		
		objDuration.selectedIndex = SetList(objDuration, sess[3]);
		objIns.selectedIndex = SetList(objIns, sess[5]);
		objCurrency.selectedIndex =  SetList(objCurrency, sess[7]);
		objPickup.selectedIndex =  SetList(objPickup, sess[6]);
		objStart.selectedIndex = SetList(objStart, sess[1]);
		objAccomm.selectedIndex = SetList(objAccomm, sess[4]);
		objLevel.selectedIndex = SetList(objLevel, sess[2]);
		if (objSeminar) objSeminar.selectedIndex = SetList(objSeminar, sess[8]);
		
		ConvertCurrency();
		CalcBase();

	} else {
		objCourse.disabled = false;
		objCourse.selectedIndex =  0;
		objDuration.selectedIndex = 0;
		objIns.selectedIndex = 0;
		objCurrency.selectedIndex =  0;
		objPickup.selectedIndex =  0;
		objStart.selectedIndex = 0;
		objAccomm.selectedIndex = 0;
		if (objSeminar) objSeminar.selectedIndex = 0;
		objLevel.selectedIndex = 0;
	
		objDuration.disabled = true ;
		objStart.disabled =  true;
		objLevel.disabled = true;
		objAccomm.disabled = true;
		if (objSeminar) objSeminar.disabled =  true ;
		objPickup.disabled =  true ;
		objCurrency.disabled =  true ;
		objIns.disabled = true;
		
		objCourse.className = "";
		objDuration.className = "";
		objStart.className = "";
		objLevel.className = "";
		objAccomm.className = "";
	    if (objSeminar) objSeminar.className = "";
		objPickup.className = "";
		objCurrency.className = "";
		objIns.className = "";
	}
}

function Dispatch(objSelect, code) {
	objSelect.className = "chosen";
	
	switch(code) {
	case 1:
	//course type
		var theCourse = objCourse.selectedIndex - 1;
		if ( theCourse >= 0) {
			courseType = course[0+theCourse][2];
			courseID = course[0+theCourse][0];
		} else {
		    courseType = '';
			courseID = '';
		}
		PopulateDates();
		PopulateDuration();
		PopulateLevel();
		PopulateAccomm();
		if (objSeminar) PopulateSeminar();
		PopulatePickup();
		PopulateIns();
		CalcBase();
		
		objStart.disabled = false;
		break;
	case 2:
	// start
		objLevel.disabled = false;
		break;
	case 3:
	//level
		if (courseType == "S" || courseType == "H" || courseType.substr(0,1) == "B") {
			objDuration.disabled = false;
		} else if (courseType == 'I') {
			//skype
			objCurrency.disabled = false;		
		} else {
			PopulateAccomm();
			CalcBase();
			objAccomm.disabled = false;
		}
		
		break;
	case 4:
	//duration
		CalcBase();
	
		if( courseType != 'I') {
			PopulateAccomm();
			objAccomm.disabled = false;
		}
		break;
	case 5:
	//accommodation
		CalcBase();
		
		if (courseType != 'I') {
			objIns.disabled = false;
			objPickup.disabled = false;
			if (objSeminar) objSeminar.disabled = false;	
		}
	
		objCurrency.disabled = false;
		break;
	case 6:
	//Seminar
		CalcBase();
		break;
	case 7:
	//ins
		CalcBase();
		objPickup.disabled = false;
		break;
	case 8:
	//pickup
		CalcBase();
		objCurrency.disabled = false;
		break;
	case 9:
	//currency
		ConvertCurrency();
		CalcBase();
		break;
	}
}

function CalcBase() {
	var howLong = parseInt(objDuration.options[objDuration.selectedIndex].value);
	var theCourse = objCourse.selectedIndex - 1;
	if (onlyskype)theCourse = objCourse.selectedIndex +31;
	if (onlybusiness)theCourse = objCourse.selectedIndex +19;
	basePrice = 0;	
	accommodPrice = 0;
	pickupPrice = 0;
	seminarPrice = 0;
	insPrice = 0;
	enrollFee = 52;

	if ( theCourse >= 0) {
		courseType = course[0+theCourse][2];
		if (courseType != 'S' && courseType != 'H') {
			howLong = 1;
			objDuration.disabled=true;
		}
		
		if (howLong > 0) {
			basePrice = course[0+theCourse][3+howLong];
		}

		if (courseType == 'I') {
			enrollFee = 0;
		}		
	}		

	var theAccommod = objAccomm.options[objAccomm.selectedIndex].value;

		if (courseType == 'I') {
			accommodPrice = 0;
		} else if (howLong > 0 && objAccomm.selectedIndex > 0) {
		if (courseType != 'S' && courseType != 'H' && courseType.substr(0,1) != 'B') {
			howLong = parseInt(courseType.substring(1,2))
	    }

		for (var i=0; i < accommod.length; i++) {
			if (accommod[i][0] == theAccommod) {
				if (courseType == 'T') {
				// test only -- 1 night
					accommodPrice = accommod[i][accommod[i].length-1];
				} else {
					accommodPrice = accommod[i][3+howLong];
				}
				
				break; 
			}
		}
	}
	
	if (objIns.selectedIndex > 0 ) {
		var thisIns = objIns.options[objIns.selectedIndex].value

		if (thisIns == "H" || thisIns == "HC") {
		// health portion Euro 15 per week
			insPrice += weeks[SearchArray(weeks, howLong)][1] * 15;
		}		

		var insBasis = basePrice + accommodPrice;
		
		if (thisIns == "C" || thisIns == "HC") {
		// cancelation portion
			if (insBasis <= 500) {
				insPrice += 22;
			} else if (insBasis <= 2000) {
				insPrice += 44;
			} else if (insBasis >= 2858) {
				insPrice += 60;
			} else {				
				insPrice += parseInt(insBasis * .021)
			}
		}
	}
	if( objSeminar && objSeminar.selectedIndex > 0 ) {
		var thisSeminar = objSeminar.options[objSeminar.selectedIndex].value;
		seminarPrice = seminar[SearchArray(seminar, thisSeminar)][2];
	}
	
	if (objPickup.selectedIndex > 0 ) {
		var thisPickup = objPickup.options[objPickup.selectedIndex].value;
		pickupPrice = pickup[SearchArray(pickup, thisPickup)][3];
	}
	
	if (basePrice != "") {
		cellCourse.innerHTML = parseInt(basePrice * exchangeFactor);
	}
	
	cellAccomm.innerHTML = parseInt(accommodPrice * exchangeFactor); 
	cellIns.innerHTML = parseInt(insPrice * exchangeFactor);	
	cellPickup.innerHTML = parseInt(pickupPrice * exchangeFactor); 
	if (cellSeminar) cellSeminar.innerHTML = parseInt(seminarPrice * exchangeFactor);
	cellFee.innerHTML = parseInt(enrollFee * exchangeFactor); 

	if (basePrice > 0 || accommodPrice > 0 || pickupPrice > 0) {
		cellTotal.innerHTML = parseInt( ( parseInt(accommodPrice) + parseInt(basePrice) + parseInt(pickupPrice) + parseInt(insPrice) + parseInt(enrollFee) + parseInt(seminarPrice) ) * exchangeFactor );
	}
}

function PopulateDates() {
	var dateCtr = 1;
	objStart.options.length = 1;
	if (courseType != "") {
		if (courseID.substring(0,1) == "P" || courseID.substring(0,2) == 'BP' || courseID.substr(0,2) == "ON") {
			objStart.options[dateCtr] = new Option("Individual Start Date", "0000-00-00");
			dateCtr += 1;
		} else {	
			for(i=0; i < dates.length; i++) {
				if (dates[i][1] == courseType || ( (courseType.substring(0,1) == "H" || courseType.substring(0,1) == "B")  && dates[i][1] == "S") ) {
					//note above is date[i][2] in english page
					objStart.options[dateCtr] = new Option(dates[i][3], dates[i][0]);
					dateCtr += 1;	
				}
			}
		}
	}
}

function PopulateLevel() {
	var levelCtr = 1;
	objLevel.options.length = 1;
	if (courseType != "") {
		for(i=0; i < level.length; i++) {
			if ( courseType == 'S' || courseType == 'H' ||  courseType == 'I' || level[i][2] >= 50) {
				objLevel.options[levelCtr] = new Option(level[i][1], level[i][0]);
				levelCtr += 1;	
			}
		}
	}
}

function PopulateSeminar() {
	var seminarCtr = 1;
	objSeminar.options.length = 1;
	objSeminar.selectedIndex = 0;
	
	if (courseType == 'I') {
	// skype, no seminar
		objSeminar.options[seminarCtr] = new Option(msgs[3], "NONE");
		objSeminar.selectedIndex = seminarCtr;
		objSeminar.disabled = true;
		seminarCtr += 1;
	} else {
		for (i=0; i < seminar.length; i++) {
				objSeminar.options[seminarCtr] = new Option(seminar[i][1], seminar[i][0]);
				seminarCtr += 1;
		}
	}
}

function PopulateIns() {
	var insCtr = 1;
	objIns.options.length = 1;
	objIns.selectedIndex = 0;
	
	if (courseType == 'I') {
	// skype, no ins
		objIns.options[insCtr] = new Option(msgs[1], "N");
		objIns.selectedIndex = insCtr;
		objIns.disabled = true;
		insCtr += 1;
	} else {
	
		for(i=0; i < ins.length; i++) {
			if ( courseType == 'S' || courseType == 'H' || ins[i][2] >= 50) {
				objIns.options[insCtr] = new Option(ins[i][1], ins[i][0]);
				insCtr += 1;	
			}
		}
	}
}


function PopulatePickup() {
	var pickupCtr = 1;
	objPickup.options.length = 1;
	objPickup.selectedIndex = 0;
	
	if (courseType == 'I') {
	// skype, no pickup
		objPickup.options[pickupCtr] = new Option(msgs[2], "NONE");
		objPickup.selectedIndex = pickupCtr;
		pickupCtr += 1;
		objPickup.disabled = true;
	} else {
	
		for(i=0; i < pickup.length; i++) {
				objPickup.options[pickupCtr] = new Option(pickup[i][1], pickup[i][0]);
				pickupCtr += 1;	
		}
	}
}


function PopulateDuration() {
	var weekCtr = 1;
	objDuration.options.length = 1;
	var theCourse = objCourse.selectedIndex - 1;

	if (theCourse >= 0) {
		for(i=0; i < weeks.length; i++) {
			if (course[theCourse][4+i] > 0) {
				objDuration.options[weekCtr] = new Option(weeks[i][2], weeks[i][0]);
				weekCtr += 1;	
			}
		}
	}
}

function PopulateAccomm() {
	var accommCtr = 1;
	
	if (objAccomm.selectedIndex > 0) {
		var orig = objAccomm.options[objAccomm.selectedIndex].value;
	} else {
		var orig = "";
	}
	
	objAccomm.options.length = 1;
	objAccomm.selectedIndex = 0;
	
	var theLength = objDuration.selectedIndex - 1;
	if (courseType == 'I') {
	// skype, no accommodation	
		objAccomm.options[accommCtr] = new Option(msgs[0], "N");
		objAccomm.selectedIndex = accommCtr;
		objAccomm.disabled = true;
		accommCtr += 1;
	} else if (courseType == "T" || courseType.substring(0,1) == "H" || courseType.substring(0,1) == "B" ) {
		// test, home or 2 Cities
	    if (courseType.substr(0,1) == 'B') {
			var matchIt = courseType;
		} else if (courseType == "T" ) {
		    var matchIt = "L";
		} else {
		    var matchIt = "HT";
		}
		
		for(i=0; i < accommod.length; i++) {
			if (accommod[i][2]== matchIt) {
				objAccomm.options[accommCtr] = new Option(accommod[i][1], accommod[i][0]);

				if (accommod[i][0] == orig && orig != "") {
					objAccomm.selectedIndex = accommCtr;
				}
								
				accommCtr += 1;	
			}
		}	
	} else {		
	// standard course
		for(i=0; i < accommod.length; i++) {
			if (accommod[i][4+theLength] >= 0 && accommod[i][2] != 'HT' && accommod[i][2].substring(0,1) != 'B' && accommod[i][2].substring(0,1) != "X") {
				objAccomm.options[accommCtr] = new Option(accommod[i][1], accommod[i][0]);

				if (accommod[i][0] == orig && orig != "") {
					objAccomm.selectedIndex = accommCtr;
				}
				
				accommCtr += 1;	
			}
		}
	}}

function PopulateCurrency() {
	var exchangeCtr = 1;
	objCurrency.options.length = 1;
		for(i=0; i < exchange.length; i++) {
				objCurrency.options[exchangeCtr] = new Option(exchange[i][4], exchange[i][0]);
				exchangeCtr += 1;	
		}
}

function CurrencySymbol(currencyCode) {
	var theCH = "";
	
	switch(currencyCode) {
	case "EUR":
		theCH = "&#8364;";
		break;
	case "USD" :
		theCH = "&#36;";
		break;
	case "GBP" :
		theCH = " 	&#8356;";
		break;
	case "CHF" :
		theCH = "CHF";
		break;
	case "JPY" :
		theCH = "&yen;"
		break;
	case "ZAR" :
		theCH = "ZAR";
		break;
    case "BRL" :
	    theCH = "R$";
		break;
	case "CAD" :
		theCH = "C$";
		break;
	case "AUD" :
		theCH = "A$";
		break;
	case "MXN" :
		theCH = "M$";
		break;
	default:
		theCH = currencyCode;
	}

	return theCH;
}

function ConvertCurrency() {
	var theCurrency = objCurrency.options[objCurrency.selectedIndex].value

	for (var i=0; i < exchange.length; i++) {
		if (theCurrency == exchange[i][0]) {
			exchangeFactor = exchange[i][1];
			theCH = CurrencySymbol(theCurrency);
			
			for (var j=0; j < CurrencySymbolsCells.length; j++) {
				ptr = document.getElementById(CurrencySymbolsCells[j])
				ptr.innerHTML = "<div align=right>" + theCH + "</div>";
			}
		}
	}
}

function ChangeTime(byHowMuch) {
	var newIndex = objDuration.selectedIndex + byHowMuch;
	
	if (newIndex > 0 && newIndex < objDuration.options.length) {
		objDuration.selectedIndex = newIndex;
		CalcBase();
	}
}

function ChangeInst(UpOrDown) {
	var thisPtr = 0;
	var newPtr = 0;

	if (objCourse.selectedIndex > 0 ) {
		var thisCourse = objCourse.options[objCourse.selectedIndex].value;
		
		thisPtr = SearchArray(course, thisCourse);

		if (UpOrDown > 0 ) {
			var nextOne = course[thisPtr][26]
		} else {
			var nextOne = course[thisPtr][27]
		}

		if (nextOne != "" && nextOne != null) {		
			for (var ii = 0; ii < objCourse.options.length; ii++) {
				if (objCourse.options[ii].value == nextOne) {
					objCourse.selectedIndex = ii;
					break;
				}
			}
		}
		
		CalcBase();
	}
}
function SearchArray(theArray, theValue) {
	for (ii =0; ii < theArray.length; ii++) {
		if (theArray[ii][0] == theValue) {
			return ii;
		}
	}
	
	return -1
}
function submitForm(reallysubmit) {
      if( reallysubmit ) {
		if (objCurrency.selectedIndex == 0) objCurrency.selectedIndex = 1;
		
        document.forms.ANMELDUNG.send.value = 1;
		document.forms.ANMELDUNG.CoursePrice.value = basePrice;
		document.forms.ANMELDUNG.AccommPrice.value = accommodPrice;
		document.forms.ANMELDUNG.EnrollFee.value = enrollFee;
		
		sess[0] = (objCourse.selectedIndex > 0 ) ? objCourse.options[objCourse.selectedIndex].value : "";
		sess[1] = (objStart.selectedIndex > 0 ) ? objStart.options[objStart.selectedIndex].value : "";
		sess[2] = (objLevel.selectedIndex > 0 ) ? objLevel.options[objLevel.selectedIndex].value : "";
		sess[3] = (objDuration.selectedIndex > 0 ) ? objDuration.options[objDuration.selectedIndex].value : "";
		sess[4] = (objAccomm.selectedIndex > 0 ) ? objAccomm.options[objAccomm.selectedIndex].value : "";
		sess[5] = (objIns.selectedIndex > 0 ) ? objIns.options[objIns.selectedIndex].value : "";
		sess[6] = (objPickup.selectedIndex > 0 ) ? objPickup.options[objPickup.selectedIndex].value : "";
		sess[7] = (objCurrency.selectedIndex > 0 ) ? objCurrency.options[objCurrency.selectedIndex].value : "";
		sess[8] = (objSeminar.selectedIndex > 0 ) ? objSeminar.options[objSeminar.selectedIndex].value : "";
		
		document.forms.ANMELDUNG.SessionStuff.value = sess.join('|');
		//alert(document.forms.ANMELDUNG.SessionStuff.value);
	   	//VF_search();
	}
}
