//
//	Modul     : Standardvalidation mit Email und Passwort
//	Version   : 1.0
//	Datei     : validate.js
//	Autor     : René Jossen
//	Erstellt  : 03.11.2006
//	
//	Aenderung : 
//	WER -------- WANN ------ WAS --------------------------------------------------------
//
//
//

falseColor = "#FFAAAA";
trueColor = "#fafbf5";
var usrField = "";
var usrForm = "";

function initValidate(theForm){
	var f = theForm;
	for(i = 0;i<f.length;i++){
		//f[i].style.backgroundColor = trueColor;
	}
}

function validate(theForm){
	var fromDayFields = new Array();
	var fromMonthFields = new Array();
	var fromYearFields = new Array();
	var toDayFields = new Array();
	var toMonthFields = new Array();
	var toYearFields = new Array();
	var iDropDateFrom = 0;
	var iDropDateTo = 0;
	
	usrField = "";
	usrForm = "";
	var f = theForm;
	returnor = true;
	prevPass = "";
	for(i = 0;i<f.length;i++){
		if(f[i].getAttribute("needed") != null){
			if(f[i].value == "" && f[i].getAttribute("needed") != "dropDate"){
				f[i].style.backgroundColor = falseColor;
				if(returnor){
					f[i].focus();
				}
				returnor = false;
			}else{
				//gnkcf Datumsabfrage
				if(f[i].getAttribute("needed") == "dropDate"){
					
					switch(f[i].getAttribute("needState")){
						case "fromDay":
							fromDayFields[iDropDateFrom] = f[i];
							f[i].style.backgroundColor = trueColor;
							break;
						case "fromMonth":
							fromMonthFields[iDropDateFrom] = f[i];
							f[i].style.backgroundColor = trueColor;
							break;
						case "fromYear":
							fromYearFields[iDropDateFrom] = f[i];
							f[i].style.backgroundColor = trueColor;
							iDropDateFrom++;
							break;
						case "toDay":
							toDayFields[iDropDateTo] = f[i];
							f[i].style.backgroundColor = trueColor;
							break;
						case "toMonth":
							toMonthFields[iDropDateTo] = f[i];
							f[i].style.backgroundColor = trueColor;
							break;
						case "toYear":
							toYearFields[iDropDateTo] = f[i];
							f[i].style.backgroundColor = trueColor;
							
							//gnkcf Gesamtabfrage
							myDate = new Date();
							
							dateFrom = 
								(fromDayFields[iDropDateTo].selectedIndex+1) + ". " 
								+ (fromMonthFields[iDropDateTo].selectedIndex+1) + ". " 
								+ (myDate.getYear() - fromYearFields[iDropDateTo].selectedIndex);
							if(!ValidateDate(dateFrom)){
								fromDayFields[iDropDateTo].style.backgroundColor = falseColor;
								fromMonthFields[iDropDateTo].style.backgroundColor = falseColor;
								fromYearFields[iDropDateTo].style.backgroundColor = falseColor;
								returnor = false;
							}
							
							
							dateTo = 
								(toDayFields[iDropDateTo].selectedIndex+1)+ ". " 
								+ (toMonthFields[iDropDateTo].selectedIndex+1) + ". " 
								+ (myDate.getYear() - toYearFields[iDropDateTo].selectedIndex);
							if(!ValidateDate(dateTo)){
								returnor = false;
								toDayFields[iDropDateTo].style.backgroundColor = falseColor;
								toMonthFields[iDropDateTo].style.backgroundColor = falseColor;
								toYearFields[iDropDateTo].style.backgroundColor = falseColor;
							}
							
							//gnkcf from < to!
							xreturnor = true;
							if(fromYearFields[iDropDateTo].selectedIndex < toYearFields[iDropDateTo].selectedIndex){
								xreturnor = false;
							}else if(fromYearFields[iDropDateTo].selectedIndex == toYearFields[iDropDateTo].selectedIndex){
								if(fromMonthFields[iDropDateTo].selectedIndex > toMonthFields[iDropDateTo].selectedIndex){
									xreturnor = false;	
								}else if(fromMonthFields[iDropDateTo].selectedIndex == toMonthFields[iDropDateTo].selectedIndex){
									if(fromDayFields[iDropDateTo].selectedIndex > toDayFields[iDropDateTo].selectedIndex){
										xreturnor = false;
									}
								}
							}
							
							
							if(!xreturnor){
								returnor = false;
								toDayFields[iDropDateTo].style.backgroundColor = falseColor;
								toMonthFields[iDropDateTo].style.backgroundColor = falseColor;
								toYearFields[iDropDateTo].style.backgroundColor = falseColor;
							}
							
							
							iDropDateTo++;
							break;
					}
					
				}else if(f[i].getAttribute("needed") == "password" && prevPass == ""){
					prevPass = f[i].value;
					f[i].style.backgroundColor = trueColor;
				}else if(f[i].getAttribute("needed") == "password" && prevPass != ""){
					if(prevPass == f[i].value){
						f[i].style.backgroundColor = trueColor;
					}else{
						f[i].style.backgroundColor = falseColor;
						if(returnor){
							f[i].focus();
						}
						returnor = false;
					}
				}else if(f[i].getAttribute("needed") == "email"){
					if(ValidateEmail(f[i].value)){
						f[i].style.backgroundColor = trueColor;
					}else{
						f[i].style.backgroundColor = falseColor;
						if(returnor){
							f[i].focus();
						}
						returnor = false;
					}
				}else if(f[i].getAttribute("needed") == "select"){
					if(f[i].selectedIndex != 0){
						f[i].style.backgroundColor = trueColor;
					}else{
						f[i].style.backgroundColor = falseColor;
						if(returnor){
							f[i].focus();
						}
						returnor = false;
					}
				}else if(f[i].getAttribute("needed") == "telephone"){
					if(f[i].value.search("00") == 0 || f[i].value.search(/\+/) == 0){
						f[i].style.backgroundColor = trueColor;
					}else{
						f[i].style.backgroundColor = falseColor;
						if(returnor){
							alert("Please make sure to include your international country code");
							f[i].focus();
						}
						returnor = false;
					}
				}else if(f[i].getAttribute("needed") == "username"){
					if(ValidateEmail(f[i].value)){
						usrField = f[i];
						f[i].style.backgroundColor = trueColor;
					}else{
						f[i].style.backgroundColor = falseColor;
						if(returnor){
							f[i].focus();
						}
						returnor = false;
					}
				}else if(f[i].getAttribute("needed") == "date"){
					if(ValidateDate(f[i].value)){
						f[i].style.backgroundColor = trueColor;
					}else{
						f[i].style.backgroundColor = falseColor;
						if(returnor){
							f[i].focus();
						}
						returnor = false;
					}
				}else if(f[i].getAttribute("needed") == "dmnDate"){
					
					if(f.dmnDateMonth.options[f.dmnDateMonth.selectedIndex].innerText){
						xActYear = 1900 + (f.dmnDateYear.options[f.dmnDateYear.selectedIndex].innerText*1);
						
					}else{
						xActYear = 1900 + (f.dmnDateYear.options[f.dmnDateYear.selectedIndex].value*1);
					}
					if(xActYear < 1920){
						xActYear += 100;	
					}
					
					f[i].value = (f.dmnDateDay.selectedIndex+1) + "." + (f.dmnDateMonth.selectedIndex+1) + "." + xActYear;
					if(ValidateDate(f[i].value)){
						f.dmnDateDay.style.backgroundColor = trueColor;
						f.dmnDateMonth.style.backgroundColor = trueColor;
						f.dmnDateYear.style.backgroundColor = trueColor;
						if(f.dmnDateMonth.options[f.dmnDateMonth.selectedIndex].innerText){
							f[i].value = (f.dmnDateDay.selectedIndex+1) + ". " + f.dmnDateMonth.options[f.dmnDateMonth.selectedIndex].innerText.split(".")[0] +". "+ xActYear;
						}else{
							f[i].value = (f.dmnDateDay.selectedIndex+1) + ". " + f.dmnDateMonth.options[f.dmnDateMonth.selectedIndex].value +" "+ xActYear;
						}
					}else{
						f.dmnDateDay.style.backgroundColor = falseColor;
						f.dmnDateMonth.style.backgroundColor = falseColor;
						f.dmnDateYear.style.backgroundColor = falseColor;
						if(returnor){
							f.dmnDateDay.focus();
						}
						returnor = false;
					}
				}else{
					f[i].style.backgroundColor = trueColor;
				}
			}
		}
	}
	if(usrField!="" && returnor){
		document.getElementById("chckUsrname").style.visibility = "visible";
		capusrid = document.getElementById("capusrid").value;
		creusrid = document.getElementById("creusrid").value;
		getUserExists(usrField.value,capusrid,creusrid);
		usrForm = f;
		returnor = false;	
	}else{
		return returnor;
	}
}

function getXMLHTTPRequest() {
	try {
	req = new XMLHttpRequest();
	} catch(err1) {
		try {
		req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (err2) {
			try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err3) {
				req = false;
			}
		}
	}
	return req;
} 

var http = getXMLHTTPRequest();

function getUserExists(usrName,capusrid,creusrid) {
    var myurl = '/pasAviation/usrExists.cfm';
    myRand = parseInt(Math.random()*999999999999999);
    // add random number to URL to avoid cache problems
    var modurl = myurl+"?rand="+myRand+"&usrname="+usrName+"&capusrid="+capusrid+"&creusrid="+creusrid;
	//alert(modurl);
    http.open("GET", modurl, true);
    // set up the callback function
    http.onreadystatechange = useHttpResponse;
    http.send(null);
} 


function useHttpResponse() {
    if (http.readyState == 4) {
        if(http.status == 200) {
            var xValue = http.responseText.split("-")[1]
			document.getElementById("chckUsrname").style.visibility = "hidden";
            if(xValue=="NO"){
				usrForm.submit();
				gotoStepForced("iStep"+wantedStep);
			}else{
				usrField.style.backgroundColor = falseColor;
				usrField.focus();
			}
        }
    } else {

    }
} 	