// 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.sellform;
var popup="ERROR :\n\n";
var submitOK="True"

var client_name=theForm.client_name.value;
var email=theForm.email.value;
var email_checkchar1=theForm.email.value.indexOf("@");
var email_checkchar2=theForm.email.value.lastIndexOf(".");
var llfone=replaceSubstring(theForm.landline.value, " ", "");
var cfone=replaceSubstring(theForm.cellphone.value, " ", "");
var address=theForm.address.value;
var specs=theForm.specs.value;
var price=theForm.price.value;

	llfone=replaceSubstring(llfone, "(", "");
	llfone=replaceSubstring(llfone, ")", "");
	cfone=replaceSubstring(cfone, "(", "");
	cfone=replaceSubstring(cfone, ")", "");

if (client_name.length<4){
	popup = (popup + "Please enter your FULL name in the block provided.\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 (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 (address.length<10) {
	popup = (popup + "The address is insufficient - please enter the COMPLETE address for the property.\n\n")
	submitOK="False"
}

if (specs.length<1) {
	popup = (popup + "You can\'t expect people to be interested in your property without telling them anything about it!\nPlease enter a few features that your property has.\n\n")
	submitOK="False"
}

if ( isNaN(price) || !(price>0) ){
	popup = (popup + "You have not entered a valid price - please enter a number\n\n");
	submitOK="False";
}

if (submitOK=="False"){
	alert(popup);
	return false;
}
}