//length of the domain before the .
function domLen(sVal) {
	return sVal.length - domainType(sVal).length;
}

//returns the domain extension of the entered domain (everything after the .)
function domainType(sVal) {
	//only allow .com, .net, .org, .ca, and third and fourth level .ca domains
	var domExt, intTemp;

	intTemp = sVal.indexOf(".");
	if (intTemp >= 0) {
		domExt = sVal.substring(intTemp, sVal.length).toLowerCase();
		if (domExt.length > 4 && domExt.search(".ca") < 0)
			domExt = domExt.substring(domExt.length-4,domExt.length);
		if (domExt.length >= 6 && (domExt.search(".on.ca") >= 0 || domExt.search(".bc.ca") >= 0 || domExt.search(".ab.ca") >= 0 || domExt.search(".sk.ca") >= 0 || domExt.search(".mb.ca") >= 0 || domExt.search(".qc.ca") >= 0 || domExt.search(".nb.ca") >= 0 || domExt.search(".ns.ca") >= 0 || domExt.search(".pe.ca") >= 0 || domExt.search(".nf.ca") >= 0 || domExt.search(".yk.ca") >= 0 || domExt.search(".nt.ca") >= 0))
			domExt = domExt.substring(domExt.lastIndexOf(".", domExt.length-7), domExt.length);//domExt = domExt.substring(domExt.length-6,domExt.length);
		else if (domExt.length > 3 && domExt.search(".ca") >= 0)
			domExt = domExt.substring(domExt.length-3,domExt.length);
		if ((domExt.search(".ca") == domExt.length-3 && (domExt.length==3 || domExt.length>=6)) || domExt == ".com" || domExt == ".net" || domExt == ".org") {
			return sVal.substring(sVal.length-domExt.length, sVal.length);
		}
	}
	return "";
}

//regular expression replace, returns the string with replaced characters.
function rrep(sVal, rExp, rwThis) {
	if (sVal.search(rExp) >= 0) {
		sVal = sVal.replace(rExp, rwThis)
	}
	return sVal;
}

// Validate this form before submitting - prevent blanks and the entry of www.domainname
// as well as all kinds of illegal characters, and names
function validateBeforeSubmit(){
	var sVal, soVal, intError, strError, domExt;


	intError = 0;
	sVal = new String(document.searchForm.sdomain.value);

	//get rid of leading .s
	//while (sVal.substring(0,1) == ".") {
	//		sVal = sVal.replace(".", "");
	//}

	// Check to see if the field was left blank
	if (domLen(sVal) < 2) {
		intError += 2
	}
 	// Check to see if the user entered a domain beginning with www
	if ( (sVal.toLowerCase().substring(0,4))=="www." ) {
		sVal = sVal.substring(4, sVal.length);
		intError += 4;
	}
	if ((sVal.substring(0,1))=="-") {
		sVal = sVal.replace("-", "");   //replace the first occurence of - with nothing.
		intError += 8;
	}
	if ((sVal.substr(domLen(sVal)-1,1))=="-") {
		sVal = sVal.substring(0, domLen(sVal) -1) + domainType(sVal);
		if ((Math.floor(intError / 8)) != 1)
			intError += 8;
	}

	soVal=sVal;

	//check for accented characters
	sVal = rrep(sVal,/[ÀÂÃÄÅ]/g,"A");
	sVal = rrep(sVal,/[àáâãäå]/g,"a");
	sVal = rrep(sVal,/[ç]/g,"c");
	sVal = rrep(sVal,/[Ç]/g,"C");
	sVal = rrep(sVal,/[èéêë]/g,"e");
	sVal = rrep(sVal,/[ÈÉÊË]/g,"E");
	sVal = rrep(sVal,/[òóôõö]/g,"o");
	sVal = rrep(sVal,/[ÒÓÔÖ]/g,"O");
	sVal = rrep(sVal,/[ÙÚÛÜ]/g,"U");
	sVal = rrep(sVal,/[ùúûü]/g,"u");
	sVal = rrep(sVal,/[æ]/g,"ae");
	sVal = rrep(sVal,/[Ÿ]/g,"Y");
	sVal = rrep(sVal,/[ÿ]/g,"y");
	sVal = rrep(sVal,/[Ñ]/g,"N");
	sVal = rrep(sVal,/[ñ]/g,"n");
	sVal = rrep(sVal,/[œ]/g,"oe");
	sVal = rrep(sVal,/[Œ]/g,"OE");
	sVal = rrep(sVal,/[š]/g,"s");
	sVal = rrep(sVal,/[ÌÍÎÏ]/g,"I");
	sVal = rrep(sVal,/[ìíîï]/g,"i");

	if (sVal != soVal) {
		intError += 16;
	}
		soVal = sVal;

	//check for characters that could be replaced with a -
	sVal = rrep(sVal.substring(0,domLen(sVal)),/[_+=,<>*&.]/g,"-") + domainType(sVal);

	//check for other invalid characters, replace with nothing.
	sVal = rrep(sVal.substring(0,domLen(sVal)),/[^A-Za-z0-9-]/g,"") + domainType(sVal);

	if (sVal != soVal) {
		intError += 1;
	}
	//check for dashes again, in case the above replaced
	//a beginning character with a dash, or there was more than one
	//dash at the beginning or end of the original sVal
	while (((sVal.substring(0,1))=="-") || ((sVal.substr(domLen(sVal)-1,1))=="-")) {
		//check again in case the above put a dash at the beginning or end
		if ((sVal.substring(0,1))=="-") {
			sVal = sVal.replace("-", "");
		}
		if ((sVal.substr(domLen(sVal)-1,1))=="-") {
			sVal = sVal.substring(0, domLen(sVal) -1) + domainType(sVal);
		}
	}

	//dynamically create an error string, and pop it up.
	strError = "There were problems in your domain name selection:\n\n";

	document.searchForm.isError.value="";
	if ((Math.floor(intError / 1) % 2) == 1)
		strError = strError + "* The only characters allowed in a domain name are non accented letters, digits and a dash (-).\n";
	if ((Math.floor(intError / 2) % 2) == 1)
		strError = strError + "* Domain Names must have a length of at least 1 or at least 2 for .ca domains.\n";
	if ((Math.floor(intError / 4) % 2) == 1)
		strError = strError + "* When searching for a domain, do not add the 'www.' portion to the begining of the domain.\nFor example, search for domain.ca, not www.domain.ca.\n";
	if ((Math.floor(intError / 8) % 2) == 1)
		strError = strError + "* Domains cannot begin or end with a dash (-)\n";
	if ((Math.floor(intError / 16)) == 1 && ((Math.floor(intError / 1) % 2)!= 1)) {
		strError = strError + "* Accented characters are not allowed in domain names.\n";
		document.searchForm.isError.value="yes";
	}

	if (domLen(sVal) >= 2)
		strError = strError + "\nYour search string will be replaced with a valid domain name."
	else
		strError = strError + "\nPlease enter a valid domain name."

	if (intError % 16 != 0)
		alert(strError);
	document.searchForm.sdomain.value = sVal;

	document.searchForm.sdomain.focus();
	return (intError % 16 == 0);  //return true if intError is 0, false if not.
}
