/**
 * @author bri
 */

 function emoProductRegistration() {
 	this.red = "#FF0000";
	this.grey = "#20A5C5";
	this.green = "#99cc99";
	this.GuaranteeStatus = false;
	this.noGuarantee = "";
	this.wrongPurchasedZipCode = "";
	this.snExist = "";
	this.PurchaseDate=false;
	
	this.submit = function() {
		if(this.checkForm()) {
			document.emoProductRegistration.submit();
		}
		else {
			alert("check the form");
		}
	}
	
	this.submitPromo = function() {
		if(this.checkFormPromo()) {
			document.emoProductRegistration.submit();
		}
		else {
			alert("check the form");
		}
	}
	
	this.getModelList = function(strSearch) {
		this.GuaranteeStatus = false;
//		document.getElementById("emoModelSearchForm").style.backgroundColor = this.grey;
		document.getElementById("emoModelSearchForm").style.color = "#20A5C5";
		if(strSearch=="") {
			document.getElementById("emoStatusMessage").innerHTML = "";
			document.getElementById("emoStatusMessage").style.display = "none";
		}
		if(strSearch.length >= 3) {
			document.getElementById("emoModelList").style.display = "block"
			emoProdReg.stopLoading("emoLoadingElementModel");
			emoProdReg.startLoading("emoLoadingElementModel");
			new Ajax.Request("/index.php", {
				method: "get",
				parameters: {
					eID: 'emo_productregistration_pi1',
					model: strSearch,
					action: 'searchModel'
				},
				onSuccess: function(xhr){
					objJSON = eval('(' + xhr.responseText + ')');
					resElement = document.getElementById("emoModelList");
					resElement.innerHTML = "";
					newHeight = objJSON.length*18;
					if(newHeight<=150) {
						resElement.style.height = newHeight+"px";
					}
					else {
						resElement.style.height = "150px";
					}
					if(objJSON) {
						for(row=0;row<objJSON.length;row++) {
							newEntry = document.createElement("div");
							newEntry.innerHTML = objJSON[row]['productname'];
							newEntry.className = "emoModelRow";
							newEntry.onmouseover = function() {
								this.className = "emoModelRowOver";
							}
							newEntry.onmouseout = function() {
								this.className = "emoModelRow";
							}
							
							newEntry.id = "emoModelListRow"+row;
							newEntry.onclick = function() {
								document.getElementById("emoModelSearchForm").value = this.title;
								document.getElementById("emoModelList").style.display = "none"
								emoProdReg.checkGuarantee(this.id);
							}
							
							newEntry.title = objJSON[row]['productname'];
							
							resElement.appendChild(newEntry);
						}
						emoProdReg.jsonModelList = objJSON;
						emoProdReg.stopLoading("emoLoadingElementModel");
					}
				},
				onFailure: function(){
					alert('Something went wrong...');
				}
			});
		}
		else {
			document.getElementById("emoModelList").style.display = "none"
			document.getElementById("emoModelList").innerHTML = "";
		}
	}
	
	this.hiddePruchasedList = function() {
		if(document.getElementById('emoPurchasedList').childNodes.length<1) {
			document.getElementById('emoPurchasedList').style.display='none';
		}
	}

	this.getPurchasedList = function(strSearch) {
		this.GuaranteeStatus = false;
//		document.getElementById("emoPurchasedSearchForm").style.backgroundColor = this.grey;
		document.getElementById("emoPurchasedSearchForm").style.color = "#20A5C5";
		if(strSearch=="") {
			document.getElementById("emoStatusMessage").innerHTML = "";
		}
		if(strSearch.length >= 1) {
			document.getElementById("emoPurchasedList").style.display = "block"
			emoProdReg.stopLoading("emoLoadingElementPurchased");
			emoProdReg.startLoading("emoLoadingElementPurchased");
			new Ajax.Request("/index.php", {
				method: "get",
				parameters: {
					eID: 'emo_productregistration_pi1',
					search: strSearch,
					action: 'searchPurchased'
				},
				onSuccess: function(xhr){
					objJSON = eval('(' + xhr.responseText + ')');
					resElement = document.getElementById("emoPurchasedList");
					resElement.innerHTML = "";
					newHeight = objJSON.length*18;
					
					if(newHeight<=150) {
						resElement.style.height = newHeight+"px";
					}
					else {
						resElement.style.height = "150px";
					}
					if(objJSON) {
						for(row=0;row<objJSON.length;row++) {
							newEntry = document.createElement("div");
							newEntry.innerHTML = objJSON[row]['company'];
							newEntry.className = "emoModelRow";
							newEntry.onmouseover = function() {
								this.className = "emoModelRowOver";
							}
							newEntry.onmouseout = function() {
								this.className = "emoModelRow";
							}
							
							newEntry.id = "emoPurchasedListRow"+row;
							newEntry.onclick = function() {
								document.getElementById("emoPurchasedSearchForm").value = this.title;
								document.getElementById("emoPurchasedList").style.display = "none"
								//emoProdReg.checkGuarantee(this.id);
							}
							
							newEntry.title = objJSON[row]['company'];
							
							resElement.appendChild(newEntry);
						}
						emoProdReg.jsonModelList = objJSON;
						emoProdReg.stopLoading("emoLoadingElementPurchased");
					}
				},
				onFailure: function(){
					alert('Something went wrong...');
				}
			});
		}
		else {
			document.getElementById("emoPurchasedList").style.display = "none"
			document.getElementById("emoPurchasedList").innerHTML = "";
		}
	}
		
	this.checkSN = function(sn) {
		if(sn!="") {
//			document.getElementById("emoModelSNForm").style.backgroundColor = this.grey;
			document.getElementById("emoModelSNForm").style.color = "#20A5C5";
			emoProdReg.startLoading("emoLoadingElementSN");
			new Ajax.Request("/index.php", {
				method: "get",
				parameters: {
					eID: 'emo_productregistration_pi1',
					sn: sn,
					action: 'checkSN',
					pid: emoProdReg.regPid
				},
				onSuccess: function(xhr){
					objJSON = eval('(' + xhr.responseText + ')');
					if(objJSON.snCheck=="false") {
						emoProdReg.snCheck = false;
//						document.getElementById("emoModelSNForm").style.backgroundColor = emoProdReg.green;
						document.getElementById("emoModelSNForm").style.color = emoProdReg.green;
						document.getElementById("emoStatusMessageSN").innerHTML = "";
						document.getElementById("emoStatusMessageSN").style.display = "none";
					}
					else if(objJSON.snCheck=="true") {
	//					document.getElementById("emoModelSNForm").style.backgroundColor = emoProdReg.red;
						document.getElementById("emoModelSNForm").style.color = emoProdReg.green;
						document.getElementById("emoStatusMessageSN").innerHTML = emoProdReg.snExist;
						document.getElementById("emoStatusMessageSN").style.display = "block";
						emoProdReg.snCheck = true;
					}
					emoProdReg.stopLoading("emoLoadingElementSN");
				},
				onFailure: function(){
					alert('Something went wrong...');
				}
			});
		}
		else {
			document.getElementById("emoModelSNForm").style.backgroundColor = this.grey;
		}
	}
	
	this.getPurchasedZipCode = function(zipCode) {
//			alert(document.getElementById("emoPurchasedSearchForm").value);
			new Ajax.Request("/index.php", {
				method: "get",
				parameters: {
					eID: 'emo_productregistration_pi1',
					zip: zipCode,
					purchased: document.getElementById("emoPurchasedSearchForm").options[document.getElementById("emoPurchasedSearchForm").options.selectedIndex].value,
					action: 'searchPurchasedZipCode'
				},
				onSuccess: function(xhr){
					if(xhr.responseText=="true") {
//						document.getElementById("emoPurchasedSearchForm").style.backgroundColor = emoProdReg.green;
						document.getElementById("emoPurchasedSearchForm").style.color = emoProdReg.green;
						document.getElementById("emoStatusMessage").innerHTML = "";
						document.getElementById("emoStatusMessage").style.display = "none";
						this.GuaranteeStatus = true;
					}
					else {
						this.GuaranteeStatus = false;
//						document.getElementById("emoPurchasedSearchForm").style.backgroundColor = emoProdReg.red;
						document.getElementById("emoPurchasedSearchForm").style.color = emoProdReg.red;
						document.getElementById("emoStatusMessage").innerHTML = emoProdReg.wrongPurchasedZipCode;
						document.getElementById("emoStatusMessage").style.display = "block";
					}
				},
				onFailure: function(){
					alert('Something went wrong...');
				}
			});
	}
	

	this.checkGuarantee = function(emoModelListRowId,checkType) {
		if(checkType) {
			selectObject = emoModelListRowId;
			tmpGuarantee = selectObject.options[selectObject.options.selectedIndex].id.split("_");
			guarantee = tmpGuarantee[1];
		}
		else {
			guarantee = this.jsonModelList[emoModelListRowId]["guarantee"]
			emoModelListRowId = emoModelListRowId.replace(/emoModelListRow/g,"");
		}
		if(guarantee>0) {
//			document.getElementById("emoModelSearchForm").style.backgroundColor = this.green;
			document.getElementById("emoModelSearchForm").style.color = this.green;
			document.getElementById("emoStatusMessage").innerHTML = "";
			document.getElementById("emoStatusMessage").style.display = "none";
			this.GuaranteeStatus = true;
		}
		else {
			this.GuaranteeStatus = false;
//			document.getElementById("emoModelSearchForm").style.backgroundColor = this.red;
			document.getElementById("emoModelSearchForm").style.color = this.red;
			document.getElementById("emoStatusMessage").innerHTML = this.noGuarantee;
			document.getElementById("emoStatusMessage").style.display = "block";
		}
	}
	
	this.startLoading = function(emoLoadingElementName) {
		//document.getElementById(emoLoadingElementName).appendChild(this.loadingElement.cloneNode(true));
		
//		document.getElementById(emoLoadingElementName).innerHTML = "Loading ...";
	}
	
	this.stopLoading = function(emoLoadingElementName) {
		//document.getElementById(emoLoadingElementName).innerHTML = "";
	}
	
	this.checkField = function(fieldElement) {
//		fieldElement.style.backgroundColor = this.grey;
		
		fieldElement.style.color = "#20A5C5";
		fieldElement.style.borderColor = "#082931";
	}
	
	this.checkDate = function() {
		//	alert("test");
			month = document.getElementById("purchasedate_month").value
			day = document.getElementById("purchasedate_day").value
			year = document.getElementById("purchasedate_year").value

			document.getElementById("purchasedate_month").style.backgroundColor = emoProdReg.grey;
			document.getElementById("purchasedate_day").style.backgroundColor = emoProdReg.grey;
			document.getElementById("purchasedate_year").style.backgroundColor = emoProdReg.grey;

			document.getElementById("purchasedate_month").style.color = "#20A5C5";
			document.getElementById("purchasedate_day").style.color = "#20A5C5";
			document.getElementById("purchasedate_year").style.color = "#20A5C5";
			
			guarantee = document.getElementById("emoModelSearchForm").options[document.getElementById("emoModelSearchForm").options.selectedIndex].id

			document.getElementById("emoStatusMessagePurchasedate").innerHTML = "";
			emoProdReg.PurchaseDate = false;

			if(day!="" && year!="" && month!="") {
				new Ajax.Request("/index.php", {
					method: "get",
					parameters: {
						eID: 'emo_productregistration_pi1',
						guarantee: guarantee,
						month: month,
						day: day,
						year: year,
						action: 'checkDate'
					},
					onSuccess: function(xhr){
						if(xhr.responseText=="true") {
//							document.getElementById("purchasedate_month").style.backgroundColor = emoProdReg.green;
//							document.getElementById("purchasedate_day").style.backgroundColor = emoProdReg.green;
//							document.getElementById("purchasedate_year").style.backgroundColor = emoProdReg.green;

							document.getElementById("purchasedate_month").style.color = emoProdReg.green;
							document.getElementById("purchasedate_day").style.color = emoProdReg.green;
							document.getElementById("purchasedate_year").style.color = emoProdReg.green;

							document.getElementById("emoStatusMessagePurchasedate").innerHTML = "";
							document.getElementById("emoStatusMessagePurchasedate").style.display = "none";
							emoProdReg.PurchaseDate = true;
						}
						else {
							emoProdReg.PurchaseDate = false;
//							document.getElementById("purchasedate_month").style.backgroundColor = emoProdReg.red;
//							document.getElementById("purchasedate_day").style.backgroundColor = emoProdReg.red;
//							document.getElementById("purchasedate_year").style.backgroundColor = emoProdReg.red;

							document.getElementById("purchasedate_month").style.color = emoProdReg.red;
							document.getElementById("purchasedate_day").style.color = emoProdReg.red;
							document.getElementById("purchasedate_year").style.color = emoProdReg.red;

							document.getElementById("emoStatusMessagePurchasedate").innerHTML = emoProdReg.wrongPurchaseDate;
							document.getElementById("emoStatusMessagePurchasedate").style.display = "block";
						}
					},
					onFailure: function(){
						alert('Something went wrong...');
					}
				});
			}
		}
		
	this.checkPromoDate = function() {
		//	alert("test");
		
			month = document.getElementById("purchasedate_month").value
			day = document.getElementById("purchasedate_day").value
			year = document.getElementById("purchasedate_year").value

			document.getElementById("purchasedate_month").style.backgroundColor = emoProdReg.grey;
			document.getElementById("purchasedate_day").style.backgroundColor = emoProdReg.grey;
			document.getElementById("purchasedate_year").style.backgroundColor = emoProdReg.grey;

			document.getElementById("purchasedate_month").style.color = "#20A5C5";
			document.getElementById("purchasedate_day").style.color = "#20A5C5";
			document.getElementById("purchasedate_year").style.color = "#20A5C5";
			
			guarantee = document.getElementById("emoModelSearchForm").options[document.getElementById("emoModelSearchForm").options.selectedIndex].id

			document.getElementById("emoStatusMessagePurchasedate").innerHTML = "";
			emoProdReg.PurchaseDate = false;

			if(day!="" && year!="" && month!="") {
				startDate = new Date("2009","12","01"),
				startDateStamp = startDate.getTime();
				stopDate = new Date("2010","02","15"),
				stopDateStamp = stopDate.getTime();
				actDateStamp = new Date(year,month,day).getTime();
				if(stopDateStamp>=actDateStamp && startDateStamp<=actDateStamp) {
					document.getElementById("purchasedate_month").style.color = emoProdReg.green;
					document.getElementById("purchasedate_day").style.color = emoProdReg.green;
					document.getElementById("purchasedate_year").style.color = emoProdReg.green;

					document.getElementById("emoStatusMessagePurchasedate").innerHTML = "";
					document.getElementById("emoStatusMessagePurchasedate").style.display = "none";
					emoProdReg.PurchaseDate = true;
				}
				else {
					document.getElementById("purchasedate_month").style.color = emoProdReg.red;
					document.getElementById("purchasedate_day").style.color = emoProdReg.red;
					document.getElementById("purchasedate_year").style.color = emoProdReg.red;

					document.getElementById("emoStatusMessagePurchasedate").innerHTML = emoProdReg.wrongPromoPurchaseDate;
					document.getElementById("emoStatusMessagePurchasedate").style.display = "block";
				}
			}
		}
		
	this.createLoadingElement = function() {
		this.loadingElement = document.createElement("img");
		this.loadingElement.src = "/typo3conf/ext/emoproductregistration/images/loading.gif";
	}
	
	this.checkForm = function() {
		requiredFields = new Array("firstname", "lastname", "street", "zipcode","city","mail","purchased","purchasedate","purchasedate_day","purchasedate_month","purchasedate_year","sn","model");
		fieldStatus = 0;

		for(fieldnameid in requiredFields) {
			if(document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0]) {
				if(document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0].value!="") {
					//document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0].style.backgroundColor = this.grey;
				}
				else {
//					document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0].style.backgroundColor = this.red;
					document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0].style.color = this.red;
					fieldStatus++;
				}
			}
		}
		if(fieldStatus>0 || this.GuaranteeStatus==false || this.snCheck==true || this.PurchaseDate==false) {
			if(this.GuaranteeStatus==false) {
//				document.getElementById("emoModelSearchForm").style.backgroundColor = this.red;
				document.getElementById("emoModelSearchForm").style.color = this.red;
			}
			return false;
		}
		else {
			return true;
		}
	}

	this.checkFormPromo = function() {
		requiredFields = new Array("firstname", "lastname", "street", "zipcode","city","mail","purchased","emoPurchasedPlzForm","purchasedate","purchasedate_day","purchasedate_month","purchasedate_year","sn","model","bill");
		fieldStatus = 0;

		for(fieldnameid in requiredFields) {
			if(document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0]) {
				if(document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0].value!="") {
					//document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0].style.backgroundColor = this.grey;
				}
				else {
					document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0].style.borderColor = this.red;
					document.getElementsByName("tx_emoproductregistration_pi1["+requiredFields[fieldnameid]+"]")[0].style.color = this.red;
					fieldStatus++;
				}
			}
		}
		if(fieldStatus>0 || this.snCheck==true || this.PurchaseDate==false) {
			return false;
		}
		else {
			return true;
		}
	}
}
 
 var emoProdReg = new emoProductRegistration();
emoProdReg.createLoadingElement();
