//Home page Javascript - moved to external file based on Tidal Research's recommendations
// for Mozilla browsers
function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;
};

if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

function preloader() {
     // counter
     var i = 0;
     // create object
     imageObj = new Image();
     // set image list
     var images = new Array("/images/home/craig-clark.jpg", "/images/home/peter-connors.jpg", "/images/home/stu-maloff.jpg", "/images/home/scott-merlo.jpg", "/images/home/robert-brown.jpg", "/images/home/robin-hensley.jpg", "/images/home/jean-manasian.jpg");

     // start preloading
     for(i = 0; i < images.length; i++)
     {
          imageObj.src=images[i];
     }
}

// rotate testimonials on home page right rail
function randomImage()	{
quotes = new Array(7);
authors = new Array(7);
bgImages = new Array(7);
quotes[0] = "It is absolutely critical that you are conscious about your brand when you first start a business. Acquiring a premium domain name that conveys an appropriate brand message will pay huge dividends down the road.";
authors[0] =  "Craig Clark, DownPillows.com";
bgImages[0] = "/images/home/craig-clark.jpg";
quotes[1] = "The smartest investment we have made is purchasing www.Hugz.com from BuyDomains. And we hear it all the time from our customers from around the world.";
authors[1] =  "Peter Connors, Hugz.com";
bgImages[1] = "/images/home/peter-connors.jpg";
quotes[2] = "I would advise any business owner who wants to maximize their exposure, to get a generic domain name for their web site and supplement it with other related domain names.";
authors[2] =  "Stu Maloff, OrlandoBasketball.com";
bgImages[2] = "/images/home/stu-maloff.jpg";
quotes[3] = "A proper domain name is everything in online marketing. You can have a great business idea but without that perfect domain name...you have an uphill battle trying to get your site well known.";
authors[3] =  "Scott Merlo, GolfPartners.com";
bgImages[3] = "/images/home/scott-merlo.jpg";
quotes[4] = "A good domain name is crucial in ongoing success, some people think that can take any domain name and make their site great, but truthfully that is rare, it must be catchy and easy to remember.";
authors[4] =  "Robert Brown, REALationship.com";
bgImages[4] = "/images/home/robert-brown.jpg";
quotes[5] = "Raising the Bar is easy to remember so most have no trouble finding my company whether they met at a speaking engagement, heard about us via word of mouth or other marketing venues.";
authors[5] =  "Robin Hensley, RaisingtheBar.com";
bgImages[5] = "/images/home/robin-hensley.jpg";
quotes[6] = "My agency used BuyDomains.com ...they made it incredibly easy to negotiate and purchase the domain name ...my client was thrilled with name we found for them";
authors[6] =  "Jean Manasian, HDVideoConferencing.com";
bgImages[6] = "/images/home/jean-manasian.jpg";
    //calculate a random index
    var index = Math.floor(Math.random() * quotes.length);
    $("homePremium").style.backgroundImage = "url('" + bgImages[index] + "')";
	 $("homePremium").style.backgroundRepeat = "no-repeat"; 
    $("quote").innerHTML = quotes[index];
    $("author").innerHTML = authors[index];
}
var main_mdg = false;
var main_on = false;
	function fill_main_search() {
	if (!main_on) {
			$('keywordsMainSearch').value = "ENTER BUSINESS NAME OR KEYWORD(S)";
			$('keywordsMainSearch').style.color = "#999999";
			$('keywordsMainSearch').focus();
			main_on = true;
		} else {
			$('keywordsMainSearch').value = "";
			$('keywordsMainSearch').style.color = "#333333";
			main_on = false;
		}
		if (main_mdg) {
			$('keywordsMainSearch').value = "Enter keywords or domain name e.g. CarsNow.com, FrenchLife.com";
		}	
}

var clearboxM = 0;
// global variable
function clearitM() {
	var keywordInput = document.getElementById('keywordsMainSearch');
    if (clearboxM == 0) {
        keywordInput.value = "";
        keywordInput.style.color = "#333333";
		keywordInput.focus();
		clearboxM = 1;
    }
}

function submitSearchHeaderForm(){
			if(validateKeywordMain(document.forms.searchKeywordsMainForm))
				document.forms.searchKeywordsMainForm.submit();
		}

function validateKeywordMain(form){
		pageTracker._trackPageview('tracking=homepage-main-search');
		var kw = document.searchKeywordsMainForm.keywordsMainSearch.value;
		var diverr = document.getElementById('errorMainSearch');
		diverr.innerHTML = '';
		diverr.style.display = 'none';
		kw = kw.replace(/^\s+|\s+$/, '');
		if(kw == '' || kw == 'ENTER KEYWORD(S) OR PHRASE'){
			diverr.innerHTML = 'Keywords field is required';
			diverr.style.display = 'block';
			clearboxM = 0;
			clearitM();
			return false;
		}
		var regexp = /^[0-9a-zA-Z\-]+([.][a-zA-Z]+){0,2}$/;
		var pattern = /,|\s/;
		var keywords = kw.split(pattern);
		for(var i=0; i < keywords.length;i++){
			var s = keywords[i].replace(/^\s+|\	s+$/, '');
			if(s != '' && !regexp.exec(s)){
				diverr.innerHTML = 'Please use correct search format.';
				diverr.style.display = 'block';
				clearboxM = 0;
				clearitM();
				return false;
			}
		}
		return true;
	}
	
function submitSearchHeaderForm2(){
	if(validateKeywordMain2(document.forms.searchKeywordsMainForm))
	document.forms.searchKeywordsMainForm.submit();
}

function validateKeywordMain2(form){
		pageTracker._trackPageview('tracking=simple-main-search');
		var kw = document.searchKeywordsMainForm.keywordsMainSearch.value;
		var diverr = document.getElementById('errorMainSearch');
		diverr.innerHTML = '';
		diverr.style.display = 'none';
		kw = kw.replace(/^\s+|\s+$/, '');
		if(kw == '' || kw == 'ENTER KEYWORD(S) OR PHRASE'){
			diverr.innerHTML = 'Keywords field is required';
			diverr.style.display = 'block';
			clearboxM = 0;
			clearitM();
			return false;
		}
		var regexp = /^[0-9a-zA-Z\-]+([.][a-zA-Z]+){0,2}$/;
		var pattern = /,|\s/;
		var keywords = kw.split(pattern);
		for(var i=0; i < keywords.length;i++){
			var s = keywords[i].replace(/^\s+|\	s+$/, '');
			if(s != '' && !regexp.exec(s)){
				diverr.innerHTML = 'Please use correct search format.';
				diverr.style.display = 'block';
				clearboxM = 0;
				clearitM();
				return false;
			}
		}
		return true;
	}
	
function submitSearchHeaderForm3(){
	if(validateKeywordMain3(document.forms.searchKeywordsMainForm))
	document.forms.searchKeywordsMainForm.submit();
}

function validateKeywordMain3(form){
	pageTracker._trackPageview('tracking=homeTest1-main-search');
	var kw = document.searchKeywordsMainForm.keywordsMainSearch.value;
	var diverr = document.getElementById('errorMainSearch');
	diverr.innerHTML = '';
	diverr.style.display = 'none';
	kw = kw.replace(/^\s+|\s+$/, '');
	if(kw == '' || kw == 'ENTER KEYWORD(S) OR PHRASE'){
		diverr.innerHTML = 'Keywords field is required';
		diverr.style.display = 'block';
		clearboxM = 0;
		clearitM();
		return false;
	}
	var regexp = /^[0-9a-zA-Z\-]+([.][a-zA-Z]+){0,2}$/;
	var pattern = /,|\s/;
	var keywords = kw.split(pattern);
	for(var i=0; i < keywords.length;i++){
		var s = keywords[i].replace(/^\s+|\	s+$/, '');
		if(s != '' && !regexp.exec(s)){
			diverr.innerHTML = 'Please use correct search format.';
			diverr.style.display = 'block';
			clearboxM = 0;
			clearitM();
			return false;
		}
	}
return true;
}

function submitGeoKeywordForm(){
			if(validateGeo(document.forms.geoSearchForm)) {
			pageTracker._trackPageview('tracking=homepage-geo-tracking');
			document.forms.geoSearchForm.submit();
			}
		}

function validateGeo(form){
		var kw = document.geoSearchForm.geoKeywords.value;
		var diverr = document.getElementById('errorGeoKeywords');
		diverr.innerHTML = '';
		diverr.style.display = 'none';
		kw = kw.replace(/^\s+|\s+$/, '');
		if(kw == '' || kw == 'ENTER KEYWORD(S)...ie: finance, golf'){
			diverr.innerHTML = 'Keywords field is required';
			diverr.style.display = 'block';
			clearbox1 = 0;
			clearit1();
			document.getElementById('geoKeywords').focus();
			return false;
		}
		var regexp = /^[0-9a-zA-Z\-]+([.][a-zA-Z]+){0,2}$/;
		var pattern = /,|\s/;
		var keywords = kw.split(pattern);
		for(var i=0; i < keywords.length;i++){
			var s = keywords[i].replace(/^\s+|\	s+$/, '');
			if(s != '' && !regexp.exec(s)){
				diverr.innerHTML = 'Wrong input format.';
				diverr.style.display = 'block';
				clearbox1 = 0;
				clearit1();
				document.getElementById('geoKeywords').focus();
				return false;
			}
		}
		return true;
	}



var on = false;
function fill_text() {
    if (!on) {
		$('geoKeywords').value = "ENTER KEYWORD(S)...ie: finance, golf";
        if ($('signIn') != null) {
            $('signIn').value = "ENTER YOUR EMAIL";
        }
        if ($('keywordAlerts') != null) {
            $('keywordAlerts').value = "ENTER KEYWORD(S)";
        }
        on = true;
    } else {
		$('geoKeywords').value = "";
        if ($('signIn') != null) {
            $('signIn').value = "";
        }
        if ($('keywordAlerts') != null) {
            $('keywordAlerts').value = "";
        }
        on = false;
	}
}

var clearbox1 = 0;
// global variable
function clearit1() {
    if (clearbox1 == 0) {
        $('geoKeywords').value = "";
        $('geoKeywords').style.color = "#333333";
        clearbox1 = 1;
    }
}

var clearbox2 = 0;
// global variable
function clearit2() {
    if (clearbox2 == 0) {
        $('signIn').value = "";
        $('signIn').style.color = "#333333";
        clearbox2 = 1;
    }
}

var clearbox3 = 0;
// global variable
function clearit3() {
    if (clearbox3 == 0) {
        $('keywordAlerts').value = "";
        $('keywordAlerts').style.color = "#333333";
        clearbox3 = 1;
    }
}

var clearboxGeoCity = 0;
// global variable
function clearitGeoCity() {
	var geoInput = document.getElementById('city');
    if (clearboxGeoCity == 0) {
        geoInput.value = "";
        geoInput.style.color = "#333333";
		geoInput.focus();
		clearboxGeoCity = 1;
    }
}

var clearboxGeoArea = 0;
// global variable
function clearitGeoArea() {
	var geoInput = document.getElementById('area');
    if (clearboxGeoArea == 0) {
        geoInput.value = "";
        geoInput.style.color = "#333333";
		geoInput.focus();
		clearboxGeoArea = 1;
    }
}


function showGeoBox () {
	initializeGeoLocation();
	var geoBox = $('geoEditBox');
	if (geoBox.style.display == 'none') {
	geoBox.style.display = 'block';
	var geoInput = document.getElementById('city');
	geoInput.focus();
	}
}

function hideGeoBox () {
	var geoBox = $('geoEditBox');
	if (geoBox.style.display == 'block') {
	geoBox.style.display = 'none';
	}
}

/* START: category L1 to L2 hover and onclick functions */
var objHover = "";
var objNew = ""; 
var obj = "";
var closeLink = "";
var objContainer = "";
var objNewContainer = "";
function togC(fn) {
	closeLink=$(fn+'_close');
	closeLink.style.display = 'block';
	if ((obj == "") && (objHover !== "")){
	objContainer=$(fn+'_container');	
	obj=$(fn+'_category');
	objContainer.style.position = 'relative';
	obj.style.display = 'block';
	}
	else if (obj !== ""){ 	
		objNewContainer=$(fn+'_container');	
		objNew = $(fn+'_category');	
		if (objNew == obj) { 
			objContainer.style.position = 'static';
			obj.style.display = 'none';
			obj = "";			
			objNew = "";
		} else {
		
			if (objNewContainer !== objContainer) {	
				objContainer.style.position = 'static';
				objNewContainer.style.position = 'relative';
				obj.style.display = 'none';	
			}
		obj.style.display = 'none';	
		obj = "";
		objNew = "";
		objContainer = objNewContainer;
		objNewContainer = "";
		togC(fn);
		}
	}
}

function hideC(fn) {
	objContainer.style.position = 'static';
	obj.style.display = 'none';	
	obj = "";
}

function hoverL1On(fn) {
	closeLink=$(fn+'_close');	
	if ((objHover == "") && (obj == "")){
	objContainer=$(fn+'_container');
	objContainer.style.position = 'relative';
	objHover=$(fn+'_category');		
	closeLink.style.display = 'none';
	objHover.style.display = 'block';
	}
}

function hoverL1Off(fn) {
	if ((objHover !== "") && (obj == "")){ 
		objContainer.style.position = 'static';
		objHover.style.display = 'none';
		objHover = "";		
	} 
}
/* END: category L1 to L2 hover and onclick functions */

	function initializeGeoLocation(){
		var parentCity = document.getElementById('geoLocation.city');
		var parentArea = document.getElementById('geoLocation.area');
		var parentCountry = document.getElementById('geoLocation.countryName');
		var parentCountryId = document.getElementById('geoLocation.countryId');
		var city = document.getElementById('city');
		var area = document.getElementById('area');
		var country = document.getElementById('country');
		city.value = parentCity.innerHTML;
		area.value = parentArea.innerHTML;
		var indx=0;
		for(var i=0; i < country.options.length; i++) {
			if (country.options[i].value==parentCountryId.value) {
				indx=i;
				break;
			}
		}
		country.selectedIndex = indx;
	}

	function updateGeoLocationFromFrontPage(isAjaxGeoSearch){
		var city = $('city').value;
		var area = $('area').value;
		var country = $('country');
		var diverr = $('errorGeo');
		diverr.style.display = 'none';
        var validRegexp = /^[a-zA-Z\-,. ]{1,}$/;
		if (city != '' && !validRegexp.exec(city)) {
			document.getElementById('city').focus();
			diverr.style.display = 'block';
			diverr.innerHTML = 'City/Town has wrong input format.';
			return false;
		}
		if (area != '' && !validRegexp.exec(area)) {
			document.getElementById('area').focus();
			diverr.style.display = 'block';
			diverr.innerHTML = 'Region/State has wrong input format.';
			return false;
		}
        $('geoLocation.city').innerHTML = city;
        $('geoLocation.area').innerHTML = area;
        $('geoLocation.countryName').innerHTML = country.options[country.selectedIndex].text;
		$('geoLocation.countryId').value = country.value;
        setCookie("geoParams", city + '@' + area + '@' + country.value, null, "/", ".buydomains.com", null);
        if(isAjaxGeoSearch) ajaxGeoSearch();
		hideGeoBox();
    }
	
	function goLite(button) {
		button.style.color = "#09346a";
		button.style.pointer = "cursor";
	}
	function goDim(button) {
		button.style.color = "#fff";
	}

    function isValidEmail(email) {
        var result = true;
        if (email != null && email.length > 0) {
            var emailRegxp = /^.+@.+\..{2,4}$/;
            result = emailRegxp.test(email);
        }
        return result;
    }

    function checkSubscribeMail(){        
        var emailControl = document.getElementById("signIn");
        if(emailControl!=null && (""==emailControl.value || !isValidEmail(emailControl.value))){
            emailControl.style.backgroundColor = "red";
            return false;
        }
        else if(emailControl!=null){
            emailControl.style.backgroundColor = "white";
            return true;
        }
        return false;
    }