// Client-side validation for the grounding/erdung pages
/**
 * Check to see if a form field element is blank
 * 
 * @param e		an array containing the name of the field to check and an error message to output
 * @param m		a string object that will receive the error message
 */
function assertIsNotBlank(e,m) {
	var field = document.getElementById(e[0]); // find the element
	if (field) {
		if (field.value === "") {
			return false;
		}
	}
	return true; // the field is non-empty, or is not defined
}

function assertIsValidEmail() {
	var phone = document.getElementById("00N60000001QWH5");
	var re = /.*@.*\..*/;
	if (phone) {
		if (re.test(phone.value)) {
			return true;
		}
	}
	return false;
}


function assertIsConfirmed() {
	var confirm = document.getElementById("00N60000001QW9f");
	if (confirm) {
		if (confirm.checked) {
			return true;
		}
	}
	return false;
}

function assertIsNum(field) {
	if (field.value && isNaN(field.value)) {
		alert("Please enter a numeric value in this field.");
		field.value = "";
		return false;
	}
	return true;
}

function assertIsValidDate(field, l) {
 	var re = null;
	re = /(0[1-9]|[1-2][0-9]|3[0-1])\.(0[1-9]|1[0-2])\.(19|20)[0-9][0-9]/;
	if (field.value && !re.test(field.value)) {
	    if (l === "de") {
	      alert("Bitte geben Sie eine g\u00fcltige Adresse nach dem Format tt.mm.jjjj ein.");
	    } else {
	      alert("Please enter a valid date in the format 'dd.mm.yyyy'");
	    }
		field.value = "";
		field.focus();
		return false;
	}
	return true;
}

function assertIsValidDateRange(field, l) {
	// make sure that the date is less than October 25, 2007
	var cutoff = new Date(2007,10,25);
	
	//first, make sure this is a valid date at all
	var isValid = assertIsValidDate(field, l);
	if (!isValid) { return false; } // date valdation failed, so we're done
	
	// Ok, now parse the date in the field into something we can use with Date
	var dateParts = field.value.split(".");
	var entered = new Date(dateParts[2], dateParts[1], dateParts[0]);
	if (entered > cutoff) { // if the entered date is greater than the cutoff, fail and alert the user
		if (l === "de") {
			alert("Um die Voraussetzung f\u00fcr die Kompensation zu erf\u00fcllen, geben Sie bitte ein Datum vor dem 26.10.2007 ein.");
		} else {
			alert("Please enter a date on or before 25.10.2007 to qualify for compensation.");
		}
		field.value = "";
		field.focus();
		return false;
	}
	return true;
}

function setDateField(hiddenField, dateField) {
	var h = document.getElementById(hiddenField);
	if(h) {
		// split the date field into parts
		var dateParts = dateField.value.split(".");
		h.value =  dateParts[1] + "/" + dateParts[0] + "/" + dateParts[2];
	}
}

var checkForBlank = [
	/* ["00N60000001QWq0","Name"], */
	["00N60000001QWqt","System Owner: Postcode"],
	["00N60000001QWqo","System Owner: Town/City "],
	["00N60000001QWqj","System Owner: Country"],
	["00N60000001QRm8","System Information: Size"],
	["instDateDisp","System Information: Installation date"],
	["00N60000001TId0","System Information: Commissioning date"],
	["00N60000001TId5","System Information: Actual output since commissioning"],
	["00N60000001TIdA","System Information: Expected output since commissioning"],
	["00N60000001TIdK","System Information: Panel type 1"],
	["00N60000001TIdZ","System Information: Panel quantity 1"],
	["00N60000001QQJt","System Information: Panel serial numbers"],
	["00N60000001TIeN","System Information: Panel supplier"],
	["00N60000001TIec","System Information: Inverter type 1"],
	["00N60000001TIf1","System Information: inverter quantity 1"],
	["00N60000001TIfQ","System Information: Grounding kit type 1"],
	["00N60000001TIfa","System Information: Grounding kit quantity 1"],
	["00N60000001TIfg","System Information: Date kit(s) installed"],
	["00N60000001TIfp","Your Details: Company"],
	["00N60000001QQK4","Your Details: Contact person"],
	["00N60000001TIfu","Your Details: Street"],
	["00N60000001TIg4","Your Details: Postcode"],
	["00N60000001TIg9","Your Details: Town/City"],
	["00N60000001TIgJ","Your Details: Country"],
	["00N60000001QQK5","Your Details: Telephone number"],
	["00N60000001QWH5","Your Details: Email address"]
];

var checkForBlankDe = [
  /* ["00N60000001QWq0","Name"], */
  ["00N60000001QWqt","Eigent\u00fcmer des Systems: PLZ"],
  ["00N60000001QWqo","Eigent\u00fcmer des Systems: Ort"],
  ["00N60000001QWqj","Eigent\u00fcmer des Systems: Land"],
  ["00N60000001QRm8","Informationen zum System: Gr\u00f6\u00dfe"],
  ["instDateDisp","Informationen zum System: Installationsdatum"],
  ["00N60000001TId0","Informationen zum System: Datum der Inbetriebnahme"],
  ["00N60000001TId5","Informationen zum System: Tats\u00e4chliche Leistung seit der Inbetriebnahme"],
  ["00N60000001TIdA","Informationen zum System: Erwartete Leistung seit der Inbetriebnahme"],
  ["00N60000001TIdK","Informationen zum System: Modultyp 1"],
  ["00N60000001TIdZ","Informationen zum System: Modulmenge 1"],
  ["00N60000001QQJt","Informationen zum System: Modul-seriennummern"],
  ["00N60000001TIeN","Informationen zum System: Modullieferant"],
  ["00N60000001TIec","Informationen zum System: Wechselrichtertyp 1"],
  ["00N60000001TIf1","Informationen zum System: Menge der Wechselrichter 1"],
  ["00N60000001TIfQ","Informationen zum System: Erdungskit Typ 1"],
  ["00N60000001TIfa","Informationen zum System: Menge der Erdungskit 1"],
  ["00N60000001TIfg","Informationen zum System: Installationsdatum der Erdungskit"],
  ["00N60000001TIfp","Ihre Kontaktdaten: Firma"],
  ["00N60000001QQK4","Ihre Kontaktdaten: Ansprechpartner"],
  ["00N60000001TIfu","Ihre Kontaktdaten: Stra\u00dfe"],
  ["00N60000001TIg4","Ihre Kontaktdaten: PLZ"],
  ["00N60000001TIg9","Ihre Kontaktdaten: Ort"],
  ["00N60000001TIgJ","Ihre Kontaktdaten: Land"],
  ["00N60000001QQK5","Ihre Kontaktdaten: Telefonnummer"],
  ["00N60000001QWH5","Ihre Kontaktdaten: Mailadresse"]
];


/**
 * Check for the form to see if all of the required elements have values
 *
 * @param a		an 2d arrary of field ID strings and error messages
 */
function checkForm(a, l) {
	// check for blank required fields
	var theMessage="";
  if (l === "de") {
    theMessage = "Die folgenden Felder sind zu beantworten und d\u00fcrfen nicht unausgef\u00fcllt bleiben:";
  } else {
    theMessage = "The following fields are required and cannot be left blank:";
  }
	var submitOK=true;
	for (var i=0; i < a.length; i++) {
		if (!assertIsNotBlank(a[i])) {
			submitOK = false;
			theMessage += "\n " + a[i][1];
		}
	}
	// check to be sure the user has confirmed acceptance of the terms
	if (!assertIsConfirmed()) {
		submitOK=false;
    if (l === "de") {
      theMessage += "\n Ihre Kontaktdaten: Bitte best\u00e4tigen Sie, dass die angegebenen Informationen zutreffend sind"
    } else {
		  theMessage += "\n Your Details: Your must confirm that this information is correct"; 
    }
	}
	// check the format of the email to see if it's something like valid
	if (!assertIsValidEmail()) {
		submitOK=false;
    if (l ==="de") {
      theMessage += "\n Ihre Kontaktdaten: Bitte geben Sie eine g\u00fcltige Mailadresse ein"
    } else {
		  theMessage += "\n Your Details: Please enter a valid email address";
    }
	}
	if (submitOK) {
		return true;
	} else {
		alert(theMessage);
		return false;
	}
}

window.onload = function() {
	// place any other onload functions here

	// format phone number for specified input id
	doFormatPhone("00N60000001QQK5");
};



function doFormatPhone(phoneobj) {
	// set form field where phone format should take place
	var gotphone = document.getElementById(phoneobj);

	gotphone.onblur = function() {
		formatPhone(this);
	}
}

/* Format phone number function */
function formatPhone(curPhone) {
	curPhone.value = formatPhoneStr(curPhone.value);
}

/* Returns a formatted phone number */
function formatPhoneStr(phoneNumber) {
	var tempPhone = phoneNumber.replace(/[^0-9xX]/g,"");

	tempPhone = tempPhone.replace(/[xX]/g,"x");

	var extension = "";

	if(tempPhone.indexOf("x") > -1) {
		extension = " "+tempPhone.substr(tempPhone.indexOf("x"));

		tempPhone = tempPhone.substr(0,tempPhone.indexOf("x"));
	}

	switch(tempPhone.length) {
		case(10):
			return tempPhone.replace(/(...)(...)(....)/g,"($1) $2-$3")+extension;
			break;
		case(11):
			if(tempPhone.substr(0,1) == "1") {
				return tempPhone.substr(1).replace(/(...)(...)(....)/g,"($1) $2-$3")+extension;
			}

			break;
		default:
			break;
	}

	return phoneNumber;
}

 

function setPanelOther() {
	var selector = document.getElementById("00N60000001TIeN");
	var field = document.getElementById("00N60000001TIeX");
	if (selector) { 
		if (selector[selector.selectedIndex].value =="Other") {
			field.disabled=false;
		} else {
			field.disabled="disabled";
			field.value="";
		}
	}
}

function setPanelOther2() {
	var selectors = [
		"00N60000001TIec",
		"00N60000001TIeh",
		"00N60000001TIem",
		"00N60000001TIew"
	];
	var enableOther = false;
	var current = "";
	var field1 = document.getElementById("00N60000001QVSE");
	var field2 = document.getElementById("00N60000001QVRP");
	for (var i=-0; i < selectors.length; i++) {
		current = document.getElementById(selectors[i]);
		if (current[current.selectedIndex].value == "Other"){
			enableOther = true;
		}
	}
	
	if (enableOther) {
		field1.disabled = false;
		field2.disabled = false;
	} else {
		field1.disabled = "disabled";
		field1.value = "";
		field2.disabled = "disabled";
		field2.value = "";
	}
}

function setCompensation(l) {
	var size=document.getElementById("00N60000001QRm8");
	if (size) {
		if (!isNaN(size.value)) {
			var compensation = Math.round((15 * parseFloat(size.value)));
			if (compensation < 70) { compensation  = 70; }
			var field = document.getElementById("compensation");
			field.innerHTML = compensation;
		} else {
			size.value = "";
      if (l === "de") {
        alert("Gr\u00f6\u00dfe (in kW): Bitte geben Sie die Gr\u00f6\u00dfe in kW als eine Zahl ein.");
      } else {
        alert("Size (in Kw): You must enter the kW amount as a number");
      }
		}
	}
}

function setContact() {
  var n = document.getElementById("00N60000001TIeN");
  var v = '';
  var e = '';
  switch(n.value) {
    case 'Additive Energie Monning GmbH':
      v = 'Karl-Heinz Monning (Grounding)';
	  e = 'aem-monning@t-online.de';
      break;
    case 'Donauer Solartechnik Vertriebs GmbH':
      v = 'Stefan Luginger (Grounding)';
	  e = 'stefan.luginger@donauer.eu';
      break;
    case 'EWS GmbH & Co. KG':
      v = 'Roland Goss (Grounding)';
	  e = 'r.goss@ews-handewitt.de';
      break;
    case 'Goldbeck Solar GmbH':
      v = 'Andreas Argus (Grounding)';
	  e = 'andreas.argus@goldbeck.de';
      break;
    case 'Krannich Solartechnik':
      v = 'Thomas Retzer (Grounding)';
	  e = 't.retzer@de.krannich-solar.com';
      break;
    case 'PV5 Solarconcept GmbH':
      v = 'Bjorn Brosner (Grounding)';
	  e = 'b.broesner@pv5.de';
      break;
    case 'Ralos Vertriebs GmbH':
      v = 'Tim Siegert (Grounding)';
    e = 't.siegert@ralos.de';
      break;
    case 'S.A.G. Solarstrom Vertriebs GmbH':
      v = 'Simone Konstanzer (Grounding)';
    e = 's.konstanzer@solarstromag.com';
      break;
    case 'Sanisolar':
      v = 'Geert Vens (Grounding)';
	  e = 'geert.vens@sanisolar.be';
      break;
    case 'Wagner & Co Solartechnik GmbH':
      v = 'Boris Farnung (Grounding)';
	  e = 'Boris.Farnung@wagner-solar.com';
      break;
     default:
      v='Kristina Loge (Grounding)';
	  e = 'kloge@evergreensolar.com';
      break;
  }
  
  document.getElementById("name").value = v;
  document.getElementById("email").value = e;
  document.getElementById("company").value = n.value;
}
