var errorMessage = '';
function showValidationStatus(p_ElementID, p_Result) {
	if(!p_Result) {
		alert(errorMessage);
		$(p_ElementID).style.backgroundColor = '#fcc';
		$(p_ElementID).focus();
		$(p_ElementID).select();
		return false;
	}
	else {
		$(p_ElementID).style.backgroundColor = '#fff';
	}
	return true;
}
function showValidationStatus_Select(p_ElementID, p_Result) {
	if(!p_Result) {
		alert(errorMessage);
		$(p_ElementID).style.backgroundColor = '#fcc';
		$(p_ElementID).focus();
		return false;
	}
	else {
		$(p_ElementID).style.backgroundColor = '#fff';
	}
	return true;
}
function showValidationStatus_Op(p_ElementID, p_Result) {
	if(!p_Result) {
		alert(errorMessage);
		$(p_ElementID).style.backgroundColor = '#fcc';
		$(p_ElementID).focus();
		$(p_ElementID).select();
		return false;
	}
	else {
		$(p_ElementID).style.backgroundColor = '#fff';
	}
	return true;
}
function validateForm() {
	// required fields
	if(	!showValidationStatus('domain_name', _validateDomain()) ||
		!showValidationStatus('earning_per_month', _validateCurrency($('earning_per_month').value, 'Gross earnings per month')) ||
		!showValidationStatus('adv_expense', _validateCurrency($('adv_expense').value, 'Advertising expense')) ||
		!showValidationStatus('current_level_months', _validateInt($('current_level_months').value, 'Months at current level', 1, -1)) ) {
		return false;
	}
	if(	!showValidationStatus_Select('sitetype', _validateSiteType($('sitetype').value, 'Site type')) ) {
		return false;
	}
	// optional fields (validate only if filled)
	if(!($('hours_per_month').value.blank())) {
		if(!showValidationStatus('hours_per_month', _validateInt($('hours_per_month').value, 'Hours per month', 0, -1))) return false;
	}
	if(!($('monthly_visitors').value.blank())) {
		if(!showValidationStatus('monthly_visitors', _validateInt($('monthly_visitors').value, 'Unique monthly visitors', 0, -1))) return false;
	}
	if(!($('monthly_pageviews').value.blank())) {
		if(!showValidationStatus('monthly_pageviews', _validateInt($('monthly_pageviews').value, 'Monthly pageviews', 0, -1))) return false;
	}
	if(!($('signedup_members').value.blank())) {
		if(!showValidationStatus('signedup_members', _validateInt($('signedup_members').value, 'Number of signed up members', 0, -1))) return false;
	}
	if(!($('optin_subscribers').value.blank())) {
		if(!showValidationStatus('optin_subscribers', _validateInt($('optin_subscribers').value, 'Number of opt-in subscribers', 0, -1))) return false;
	}
	if(!($('skill_level').value.blank())) {
		if(!showValidationStatus('skill_level', _validateInt($('skill_level').value, 'Skill level required', 1, 10))) return false;
	}
	if(!($('content_volume').value.blank())) {
		if(!showValidationStatus('content_volume', _validateInt($('content_volume').value, 'Content volume and uniqueness', 1, 10))) return false;
	}
	if(!($('user_email').value.blank())) {
		if(!showValidationStatus('user_email', _validateEmail($('user_email').value, 'Contact email'))) return false;
	}
	if($('cap_enabled')) return capCheck();
	return true;
}
function capCheck() {
	var xmlhttp;
	var sReply;
	DataToSend = "action=process&captext=" + $('captext').value;
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
		xmlhttp.open("POST",'captcha/process.php',false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(DataToSend);
		sReply = xmlhttp.responseText;
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("POST",'captcha/process.php',false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(DataToSend);
		sReply = xmlhttp.responseText;
	}
	//alert(sReply);
	if (sReply == "good") {
		$('captext').style.backgroundColor = '#fff';
		return true;
	} else {
		alert("Anti-bot code is in-correct!");
		$('captext').style.backgroundColor = '#fcc';
		$('captext').focus();
		$('captext').select();
		return false;
	}
}
function _validateDomain() {
	var val = $('domain_name').value;
	// check if empty
	if(val.blank()) {
		errorMessage = 'Domain name cannot be empty';
		return false;
	}
	// check if sub-domain
	var dotCount = 0;
	var pos = 0;
	var firstPart = '';
	var parts = new Array();
	var arrIn = 0;
	var oldPos = 0;
	while( (pos=val.indexOf('.', oldPos)) !=-1) {
		if(dotCount==0) {
			firstPart = val.substring(oldPos, pos);
		}
		parts[dotCount] = val.substring(oldPos, pos);
		pos=pos+1;
		oldPos = pos;
		dotCount++;
	}
	if(dotCount==0) { // not a valid domain name
		errorMessage = 'Invalid domain name';
		return false;
	}
	if(dotCount>3) { // maximum is 3, i.e. www.domain.ext1.ext2
		errorMessage = 'Please enter a valid top level domain name. Not a sub-domain name.';
		return false;
	}
	if(dotCount==2) {
		// could be www.domain.ext
		if(firstPart.toLowerCase()!='www' && firstPart.toLowerCase()!='http://www' && firstPart.toLowerCase()!='https://www') {
			// or it could be domain.ext1.ext2
			switch(parts[parts.length-1]) {
				// add all possible values for ext1 here, e.g. co, com etc..
				case 'co':
				case 'com':
					break;
				default:
					errorMessage = 'Please enter a valid top level domain name. Not a sub-domain name.';
					return false;
			}
		}
	}
	if(dotCount==3) { // can only be www.domain.ext1.ext2
			// has to start with www.
			if(firstPart.toLowerCase()!='www' && firstPart.toLowerCase()!='http://www' && firstPart.toLowerCase()!='https://www') {
				errorMessage = 'Please enter a top valid level dmoain name. Not a sub-domain name.';
				return false;
			}
			// also ext1 should be supported
			switch(parts[parts.length-1]) {
				// add all possible values for ext1 here, e.g. co, com etc..
				case 'co':
				case 'com':
					break;
				default:
					errorMessage = 'Please enter a valid top level domain name. Not a sub-domain name.';
					return false;
			}
	}
	// strip dir if specified
	var sIndex = 0; 
	if(val.startsWith('http://')) sIndex = 7;
	else if(val.startsWith('https://')) sIndex = 8;
	var sIndex = val.indexOf('/', sIndex);
	if(sIndex!=-1) { // okay, dir specified, so strip it
		val = val.substr(0, sIndex);
		$('domain_name').value = val;
	}
	
	return true;
}
function _validateCurrency(p_Value, p_FieldName) {
	var val = p_Value;
	// check if empty
	if(val.blank()) {
		errorMessage = p_FieldName + ' cannot be empty.';
		if(p_FieldName=='Gross earnings per month' || p_FieldName=='Advertising expense') {
			errorMessage += ' A value of zero is acceptable.'; 	
		}
		return false;
	}
	// check if a valid amount
	if(!RegExp(/^\$?\d+(\.\d{2})?$/).test(String(val).replace(/^\s+|\s+$/g, ""))) {
		errorMessage = p_FieldName + ' is invalid.';
		if(p_FieldName=='Gross earnings per month' || p_FieldName=='Advertising expense') {
			errorMessage += ' A value of zero is acceptable.'; 	
		}
		return false;
	}
	return true;
}
function _validateInt(p_Value, p_FieldName, p_Min, p_Max) {
	var val = p_Value;
	// check if empty
	if(val.blank()) {
		errorMessage = p_FieldName + ' cannot be empty.';
		return false;
	}
	// check if a valid integer
	if(!RegExp(/^-?\d+$/).test(String(val))) {
		errorMessage = p_FieldName + ' is invalid.';
		return false;
	}
	// check if within range
	if(val<p_Min || (p_Max!=-1 && val>p_Max) ) {
		
		if(p_Max!=-1)
			errorMessage = p_FieldName + ' has to be between '+p_Min+' and '+p_Max;
		else
			errorMessage = p_FieldName + ' cannot be lesser than '+p_Min;
		
		return false;
	}
	return true;
}
function _validateEmail(p_Value, p_FieldName) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
	if (filter.test(p_Value))
		return true;
	errorMessage = p_FieldName + ' is invalid';
	return false;
}
function _validateSiteType(p_Value, p_FieldName) {
	if(p_Value!='-') return true;
	errorMessage = 'Please choose a valid Site type';
	return false;
}
function gotoReport(id) {
	window.location.href = 'http://ebizvaluations.com/archive/report/'+id;
}
function doChecks() {
	
	// age
	if($('age_created').innerHTML.blank() || $('age_days').innerHTML.blank()) {
		$('age_created').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		$('age_days').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/whois.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										//alert(result);
										if(result[0]!='OK') {
											//$('age_created').update(result[0]);
											//$('age_days').update(result[0]);
											// redirect to error page is whoiis info is unavailable
											$('noreg_domain').value = $('hDomain').value;
											$('noreg_form').submit();
										}
										else {
											$('age_created').update(result[1]);
											$('age_days').update(result[2]);
											
											document.title = $('valuation_domain').value + 
											', First registered on ' + result[1] + 
											', Valued on '+ $('valuation_date').value +
											', Currently valued at USD. ' + $('valuation_result').value;
										}
									}
								});
	}
	// alexa rank
	if($('alexa_rank').innerHTML.blank()) {
		$('alexa_rank').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/alexa.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('alexa_rank').update(result[0]);
										}
										else {
											$('alexa_rank').update(result[1]);
										}
									}
								});
	}
	
	// compete
	if($('compete_info').innerHTML.blank()) {
		$('compete_info').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/compete.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('compete_info').update(result[0]);
										}
										else {
											$('compete_info').update(result[1]);
										}
									}
								});
	}
	// inlinks (linkdomain)
	if($('yahoo_inlinks').innerHTML.blank() || $('yahoo_homelinks').innerHTML.blank() || $('yahoo_interlinks').innerHTML.blank()) {
		$('yahoo_inlinks').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		$('yahoo_homelinks').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		$('yahoo_interlinks').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/linkdomain.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('yahoo_inlinks').update(result[0]);
											$('yahoo_homelinks').update(result[0]);
											$('yahoo_interlinks').update(result[0]);
										}
										else {
											$('yahoo_inlinks').update(result[1]);
											$('yahoo_homelinks').update(result[2]);
											$('yahoo_interlinks').update(result[3]);
										}
									}
								});
	}
	// edu inlinks (linkdomain)
	if($('yahoo_edu_inlinks').innerHTML.blank()) {
		$('yahoo_edu_inlinks').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/edulinkdomain.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('yahoo_edu_inlinks').update(result[0]);
										}
										else {
											$('yahoo_edu_inlinks').update(result[1]);
										}
									}
								});
	}
	// homepage pr
	if($('google_pr').innerHTML.blank()) {
		$('google_pr').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/googlepr.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('google_pr').update(result[0]);
										}
										else {
											$('google_pr').update(result[1] + ' / 10');
											if($('hSiteType').value=='blog') {
												var pr = parseInt(result[1]);
												if(pr==5 || pr==6) {
													$('pr_56').style.display = '';
												}
												else if(pr>=7) {
													$('pr_7').style.display = '';
												}
											}
										}
									}
								});
	}
	// top 10 pr links
	if($('google_pr_list').innerHTML.blank()) {
		$('google_pr_list').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/googlepr.php?h=' + $('hDomain').value + '&list' + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('google_pr_list').update(result[0]);
										}
										else {
											$('google_pr_list').update(result[1]);
										}
									}
								});
	}
	
	// top 10 pr links
	if($('indexed_pages').innerHTML.blank()) {
		$('indexed_pages').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/googlepages.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('indexed_pages').update(result[0]);
										}
										else {
											$('indexed_pages').update(result[1]);
										}
									}
								});
	}
	
	// technorati reactions
	if($('technorati_rank').innerHTML.blank()) {
		$('technorati_rank').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/technorati.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('technorati_rank').update(result[0]);
										}
										else {
											$('technorati_rank').update(result[1]);
										}
									}
								});
	}
	// dmoz listings
	if($('dmoz_listings').innerHTML.blank()) {
		$('dmoz_listings').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/dmoz.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('dmoz_listings').update(result[0]);
										}
										else {
											$('dmoz_listings').update(result[1]);
										}
									}
								});
	}
	// wikipedia
	if($('wiki_entries').innerHTML.blank()) {
		$('wiki_entries').innerHTML = '<img src="http://ebizvaluations.com/img/loading.gif" />';
		new Ajax.Request('http://ebizvaluations.com/classes/checks/wikipedia.php?h=' + $('hDomain').value + '&r='+$('hReportID').value,	{
									method:'get',
									onSuccess: function(transport){
										var result = transport.responseText.split('|||');
										if(result[0]!='OK') {
											$('wiki_entries').update(result[0]);
										}
										else {
											$('wiki_entries').update(result[1]);
										}
									}
								});
	}
}
function loadHintTips() {
	addTooltip('theLink1', '', 
		'Content site is one with extensive text, image or video content. They are primarily monetized via contextual ad programs, CPM ads and affiliate links. Incentivized sites pay their visitors in tokens, cash gifts to complete tasks like clicking on links &amp; filling surveys.  Product sale can be from dropshipping to ebay businesses - a physical product, software or ebook etc is sold. Service ranges from hosting to image hosting to graphic design. Directories typically charge for listing or review and are focused on a particular industry. The last category includes facebook applications, non-site based online businesses, widgets and mash-ups)', 
		null, 500, false);
	addTooltip('theLink2', '', 
		'These are not "hits" or page views', 
		null, 500, false);
	addTooltip('theLink3', '', 
		'1 is a site that requires zero technical knowledge or specialist interest. 10 is a site that requires someone with a very special skill set. A content site that needs no maintenance ranks as a 1. A standard blog without too many add-ons and customizations would rank as a 2. A standard forum, again without too many customizations or subject knowledge, ranks as a 3. A site reviewing CSS layouts and designs requiring a high level of familiarity with CSS would be a 4. A custom built ecommerce site with databases to manage, several scripts and requiring much familiarity with not just databases and PHP but also JS, AJAX etc would rank as a 9 or 10', 
		null, 500, false);
	addTooltip('theLink4', '', 
		'1 is an affiliate site with zero unique content. 3 is a site with about 20,000 words or 5000 useful forum threads or the equivalent content in images/videos. 6 is a site with about 500 pages of unique content or 100,000 forum threads. 10 is Wikipedia', 
		null, 500, false);
	addTooltip('theLink5', '', 
		'Instead of joebloggs@hotmail.com use format of "joebloggs at hotmail dot com" if worried about SPAM', 
		null, 500, false);
	addTooltip('theLink6', '', 
		'This includes active forum members, daily active users of a widget, signed up members of a directory or incentivized site', 
		null, 500, false);
	addTooltip('theLink7', '', 
		'This relates to subscribers to an email database only', 
		null, 500, false);
}
function loadHintTips_Checks() {
	addTooltip('vLink1', '', 
		'Alexa is a website ranking service. <a target="_blank" href="http://ebizvaluations.com/alexa/">More</a>', 
		null, 500, false);
	addTooltip('vLink2', '', 
		'Technorati is an internet search engine for blogs. <a target="_blank" href="http://ebizvaluations.com/technorati/">More</a>', 
		null, 500, false);
	addTooltip('vLink3', '', 
		'Compete is a web traffic analysis service. <a target="_blank" href="http://ebizvaluations.com/compete/">More</a>', 
		null, 500, false);
	addTooltip('vLink4', '', 
		'PR is a Google method of assigning value to pages based on the number and quality of incoming links. <a target="_blank" href="http://ebizvaluations.com/page-rank/">More</a>', 
		null, 500, false);
	addTooltip('vLink5', '', 
		'DMOZ or the Open Directory Project is a human sorted directory of websites that have passed a manual check. Many new sites - and low quality sites - may not be found there. <a target="_blank" href="http://ebizvaluations.com/dmoz/">More</a>', 
		null, 500, false);
	addTooltip('vLink6', '', 
		'Wikipedia is an "encyclopedia" open to anyone to edit. A site with many links here over a long period of time is likely to be more respected. <a target="_blank" href="http://ebizvaluations.com/wikipedia/">More</a>', 
		null, 500, false);
	addTooltip('vLink7', '', 
		'Links back to this site from other sites. <a target="_blank" href="http://ebizvaluations.com/backlinks/">More</a>', 
		null, 500, false);
	if($('vLink8')) {
		addTooltip('vLink8', '', 
			'Based on our unique calculation.. <a target="_blank" href="http://ebizvaluations.com/what-makes-for-an-accurate-valaution.php">How?</a>', 
			null, 500, false);
	}
	if($('vLink9')) {
		addTooltip('vLink9', '', 
			'Please read our privacy policy <a target="_blank" href="http://ebizvaluations.com/about/">here</a>', 
			null, 500, false);
	}
}
function readFeed() {
	if(true) return;
	var feed = encodeURIComponent("http://marketplace.sitepoint.com/categories/premium-sites-for-sale/feed");
	new Ajax.Request('http://ebizvaluations.com/classes/feedreader.php?f='+feed,	{
									method:'get',
									onSuccess: function(transport){
										$('feedBox1').update(transport.responseText);
									}
								});
	feed = encodeURIComponent("http://marketplace.sitepoint.com/categories/established-sites-for-sale/feed");
	new Ajax.Request('http://ebizvaluations.com/classes/feedreader.php?f='+feed,	{
									method:'get',
									onSuccess: function(transport){
										$('feedBox2').update(transport.responseText);
									}
								});
	/*feed = encodeURIComponent("http://feeds.feedburner.com/WebAuctionsDailySites?format=rss");
	new Ajax.Request('http://ebizvaluations.com/classes/feedreader.php?f='+feed,	{
									method:'get',
									onSuccess: function(transport){
										$('feedBox3').update(transport.responseText);
									}
								});*/
	feed = encodeURIComponent("http://feeds.feedburner.com/MostlistedRecent");
	new Ajax.Request('http://ebizvaluations.com/classes/feedreader.php?f='+feed,	{
									method:'get',
									onSuccess: function(transport){
										$('feedBox4').update(transport.responseText);
									}
								});
}
function makeRounded(){
	if(!NiftyCheck()) return;
	RoundedTop("div#container","#FFF","#e7e7e7");
	RoundedBottom("div#container","#FFF","#8395CB");
	RoundedTop("ul#nav li","transparent","#FFC");
	Rounded("div#box","#C0CDF2","#E4E7F2");
	Rounded("div#box2","#C0CDF2","#2266AA");
	Rounded("div#box7","#C0CDF2","#2266AA");
	Rounded("div#box3","#C0CDF2","#FFFFCC");
	Rounded("div#box3a","#C0CDF2","#FFFFCC");
	Rounded("div#box4","#C0CDF2","#FFFFCC");
	Rounded("div#boxleft","#C0CDF2","#FFFFCC");
	Rounded("div#boxleft2","#C0CDF2","#C0CDF2");
	Rounded("div#boxright","#C0CDF2","#FFFFCC");
	Rounded("div#minipics li","#C0CDF2","#FFF");
	RoundedTop("div.gradient","#C0CDF2","#B8B8B8");
	RoundedBottom("div.gradient","#C0CDF2","#ECECF2");
}