// JavaScript Document

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function validate()
{
var theForm=document.affiliate_signup;
var popup="ERROR :\n\n";
var submitOK="True";

var submitter = theForm.submit_or_add.value;

if (submitter == "submit"){
		var affiliate_name=theForm.affiliate_name.value;
		var passwd = theForm.passwd.value;
		var passwd2 = theForm.passwd2.value;
		var email=theForm.email.value;
		var email_checkchar1=theForm.email.value.indexOf("@");
		var email_checkchar2=theForm.email.value.lastIndexOf(".");
		var llfone=replaceSubstring(theForm.tel_ll.value, " ", "");
		var cfone=replaceSubstring(theForm.tel_cell.value, " ", "");
		var tanc=theForm.elements.tanc.checked;
		
			llfone=replaceSubstring(llfone, "(", "");
			llfone=replaceSubstring(llfone, ")", "");
			cfone=replaceSubstring(cfone, "(", "");
			cfone=replaceSubstring(cfone, ")", "");
		
		if (affiliate_name.length<4){
			popup = (popup + "Please enter the FULL name of the person or company signing up as an affiliate.\n\n");
			submitOK="False";
		}
		
		if ((passwd.length < 1) || (passwd2.length < 1)) {
			popup = (popup + "Please enter a password and repeat it in the Confirm Password box.\n\n");
			submitOK="False";
		} else	if (passwd != passwd2) {
			popup = (popup + "The confirm password does not match the initial password again. Please retry.\n\n");
			submitOK="False";
		}
		
		if (email_checkchar1<1 || email_checkchar2<3 || email_checkchar2 < (email_checkchar1 + 2) || email_checkchar2 >= (email.length - 1)) {
			popup = (popup + "You have not entered a valid e-mail. Please try again.\n\n");
			submitOK="False";
		}
		
		if (llfone.length<10 || isNaN(llfone) ) {
			popup = (popup + "You appear to have not entered a valid landline phone number. Please enter the number only, starting with the code, eg 021 123 4567 (for local numbers) or +46 11 123 1234 (for international numbers)\n\n");
			submitOK="False";
		}
		
		if (cfone.length<10 || isNaN(cfone) ){
			popup = (popup + "You appear to have not entered a valid cellphone number. Please enter the number only, starting with the code, eg 021 123 4567 (for local numbers) or +46 11 123 1234 (for international numbers)\n\n");
			submitOK="False";
		}
		
		if (tanc==false) {
				popup = popup + "You have to read, understand and agree to the terms and conditions before you can be signed up.\n\n";
				submitOK = "False";
		}
		
		if (submitOK=="False"){
			alert(popup);
			return false;
		}
} else {
		return true;
}
}