﻿
var letter_array = Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')
var active_flag=false
var deceleration_flag=false
var actual_position=-4004
var direction
var deceleration
var inner_str=''


function $(div){return document.getElementById(div)}
	
function initScroll()
{
	$('scroller-default').style.display='none'
	$('scroller').style.display='block'
	
	for (i=0;i<26;i++)
	{
		action="'get-letter'"
		letter="'"+letter_array[i]+"'"
		sort="'alphabetical'"
		inner_str+='<div class="section"><div class="placeholder">'		
		inner_str+='<a href="search.php?action=get-letter&letter='+letter_array[i]+'&category=null&sort=alphabetical" class="letters letter-'+letter_array[i]+'" title="click to see all domains starting with the letter '+letter_array[i]+'"><span>'+letter_array[i]+'</span></a>'
		inner_str+='</div></div>'
	}
	
	inner_str+=inner_str+inner_str
	$('scroller-inner').innerHTML=inner_str
	
	$('button-left').onmouseover=function(){if(!active_flag){move(1,12)}}
	$('button-right').onmouseover=function(){if(!active_flag){move(0,12)}}
	$('button-left').onmouseout=function(){if(active_flag){clearInterval(timer);deceleration_flag=true;move(1,1)}}
	$('button-right').onmouseout=function(){if(active_flag){clearInterval(timer);deceleration_flag=true;move(0,1)}}
}

function move(direction, increment)
{	
	scroller_images=$('scroller-inner').style
	active_flag=true
	actual_scroll=0
	deceleration=0

	timer=setInterval(scroll, 20)

	function scroll()
	{
		actual_scroll+=increment
		direction==1?actual_position+=increment:actual_position-=increment
		
		actual_position>=0?actual_position=-4004:''
		actual_position<=-8008?actual_position=-4004:''
		
		scroller_images.left=actual_position+'px'
		
		deceleration++
		
		if (deceleration_flag && deceleration==15)
		{
			active_flag=false
			deceleration_flag=false
			clearInterval(timer)
		}
	}	
}

function initWishList()
{
	$('wish-list').href="javascript:initPopup(true,false,'')"
	$('login').href="javascript:initPopup(true,false,'')"
	$('register').href="javascript:initPopup(true,false,'')"
}

function initPopup(status,referrer,domain)
{
	if (status)
	{
		var height=$('container').offsetHeight
		var window_height=windowHeight()
		var scroll_position=scrollPositionCheck()
		y=(window_height-480)/2+scroll_position	
		
		bg_mask=$('mask').style
		window_height>height?bg_mask.height=window_height+'px':bg_mask.height=height+'px'
		bg_mask.display='block'
		
		popup_outer=$('popup-outer').style
		popup_outer.top=y+'px'
		
		popup_inner=$('popup-inner')
		popup_inner.style.top=y+15+'px'
		
		$('button-close').onclick=function(){initPopup(false)}

		if (login!='true')
		{
			$('button-login').onclick=function()
			{
				if (!active_flag)
				{
					var login_email=$('login-email').value
					var login_password=$('login-password').value
					if (!emailCheck(login_email) || login_password.length<3)
					{
						login_password.length<3?alert('sorry, passwords must consist of at least 3 characters'):alert('sorry, you seem to have entered an invalid email address')
					}
					else
					{
						active_flag=true
						popup_inner.className='active'
						hash=sha1Hash(sha1Hash(login_email+login_password)+login_key) // ie; hash the email and password, then add the key and hash again
						ajaxGet('cms.php?action=login&hash='+hash+'&referrer='+referrer+'&domain='+domain+'&rand='+Math.random()*999, 'wish-list')
					}
				}
			}
			$('button-register').onclick=function()
			{
				if (!active_flag)
				{
					var register_name=$('register-name').value
					var register_email=$('register-email').value
					var register_password=$('register-password').value
					if (register_name.length<2){alert('sorry, your name must consist of at least 2 characters')}
					else if (!emailCheck(register_email)){alert('sorry, you seem to have entered an invalid email address')}
					else if (register_password.length<3){alert('sorry, passwords must consist of at least 3 characters')}
					else
					{
						active_flag=true
						popup_inner.className='active'
						hash=sha1Hash(register_email+register_password)
						ajaxGet('cms.php?action=register&name='+register_name+'&email='+register_email+'&hash='+hash+'&referrer='+referrer+'&domain='+domain+'&rand='+Math.random()*999, 'wish-list')
					}
				}
			}
		}
		else
		{
			active_flag=true
			popup_inner.className='active'
			ajaxGet('cms.php?action=get-wish-list&referrer='+referrer+'&domain='+domain+'&rand='+Math.random()*999, 'wish-list')
		}
	}
	else
	{
		bg_mask.display='none'
		popup_outer.top='-5000px'
		popup_inner.style.top='-5000px'
		popup_inner.className=''
	}
}

function wishListContact(domain, name, email)
{
	var bid='null'
	
	if ($('bid-'+domain)){bid=$('bid-'+domain).value}
	
	popup_inner.className='active'
	active_flag=true
	ajaxGet('cms.php?action=wish-list-contact&domain='+domain+'&bid='+bid+'&name='+escape(name)+'&email='+escape(email)+'&rand='+Math.random()*999, 'wish-list-contact')
}

function XHR(method)
{
	http_request=false
	if (window.XMLHttpRequest)
	{
		http_request=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		try {http_request=new ActiveXObject('Msxml2.XMLHTTP')}
		catch (e)
		{
			try {http_request=new ActiveXObject('Microsoft.XMLHTTP')}
			catch (e){}
		}
	}
	else return false
}

function ajaxGet(url, referrer)
{
	XHR('get')
	http_request.onreadystatechange=function() {loadPage(http_request, referrer)}
	http_request.open('GET', url, true)
	http_request.send(null)
}

function loadPage(http_request, referrer)
{
	if (http_request.readyState==4 && http_request.status==200)
	{
		returnValue = http_request.responseText
		active_flag=false
		
		switch (referrer)
		{
			case 'wish-list':
			popup_inner.className=''

			if (returnValue=='false') {alert('sorry, you seem to have entered the wrong email or password')}
			else if (returnValue=='duplicate') {alert('sorry, an account with this email or password already exists; please try again')}
			else	
			{
				$('registration').innerHTML=returnValue
				$('wish-list-header').className='wish-list'
				login='true'
			}
			break
			
			case 'wish-list-contact':
			alert('message sent; thanks')
			popup_inner.className=''
			break
			
			case 'add-remove-domains':
			$('throbber').style.visibility='hidden'
			break
			
			case 'search-page-contact':
			for(key in chosen_array)
			{
				if ($('td-'+key) && chosen_array[key]!='')
				{
					$('td-'+key).src='graphics/button-buy.png'	
					chosen_array[key]=''
				}
			}
			$('domain-info').innerHTML=''
			$('throbber').style.visibility='hidden'
			$('contact-name').value='first and last names:'
			$('contact-email').value='email address:'
			name=0; email=0
			initBuyButton()
			alert('message sent; thanks')
			break
		}
	}
}

function windowHeight()
{
	var height=0
	if (typeof window.innerHeight!='undefined') {height=window.innerHeight}
	else if (typeof document.documentElement!='undefined' && typeof document.documentElement.clientHeight!='undefined' && document.documentElement.clientHeight!= 0) {height=document.documentElement.clientHeight}
	else {height=document.getElementsByTagName('body')[0].clientHeight}
	return height
}

function scrollPositionCheck()
{
	var position=0
	if (typeof window.pageYoffset!='undefined') {position=window.pageYoffset}
	else if (typeof document.documentElement.scrollTop!='undefined' && document.documentElement.scrollTop>0) {position=document.documentElement.scrollTop}
	else if (typeof document.body.scrollTop!='undefined') {position=document.body.scrollTop} 
	return position
}

function emailCheck(email)
{
	var success=true
	var email=email.toLowerCase()
	
	// check for valid characters
	var validation_str="0123456789abcdefghijklmnopqrstuvwxyz-_@."	
	var check=0		
	for (var i=0; i<email.length; i++) {for (var j=0; j<validation_str.length; j++){if (email.charAt(i)==validation_str.charAt(j)){check++}}}		
	if (check!=email.length){success=false}

	// check for '@' character (and correct positioning of '@' character)
	var email_check=email.indexOf('@')
	var at_check=email.lastIndexOf('@')	
	if (email_check==-1 || email_check!=at_check){success=false}
	else
	{
		email_check=email.split("@")
		email_check[1].length<5?success=false:''
		email_check[0].length<2?success=false:''			
	}
	
	// check for '.' character
	email.indexOf('.')==-1?success=false:''
	
	return success	
}

function sha1Hash(msg)
{
	var K=[0x5a827999,0x6ed9eba1,0x8f1bbcdc,0xca62c1d6];msg+=String.fromCharCode(0x80);var l=Math.ceil(msg.length/4)+2;var N=Math.ceil(l/16);var M=new Array(N)
	for (var i=0; i<N; i++)
	{
		M[i]=new Array(16)
		for (var j=0; j<16; j++){M[i][j]=(msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) | (msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3))}
	}

	M[N-1][14]=((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14]);
	M[N-1][15]=((msg.length-1)*8) & 0xffffffff
	var H0=0x67452301;var H1=0xefcdab89;var H2=0x98badcfe;var H3=0x10325476;var H4=0xc3d2e1f0;var W=new Array(80); var a,b,c,d,e
	for (var i=0; i<N; i++)
	{
		for (var t=0;  t<16; t++) W[t]=M[i][t]
		for (var t=16; t<80; t++) W[t]=ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1)
		a=H0; b=H1; c=H2; d=H3; e=H4
		for (var t=0; t<80; t++){var s=Math.floor(t/20);var T=(ROTL(a,5)+f(s,b,c,d)+e+K[s] + W[t]) & 0xffffffff;e=d;d=c;c=ROTL(b, 30);b=a;a=T}
		H0=(H0+a) & 0xffffffff;H1=(H1+b) & 0xffffffff;H2=(H2+c) & 0xffffffff;H3=(H3+d) & 0xffffffff;H4=(H4+e) & 0xffffffff
	}
	return H0.toHexStr()+H1.toHexStr()+H2.toHexStr()+H3.toHexStr()+H4.toHexStr()
}

function f(s, x, y, z)
{
	switch (s)
	{
		case 0: return (x & y) ^ (~x & z)
		case 1: return x ^ y ^ z
		case 2: return (x & y) ^ (x & z) ^ (y & z)
		case 3: return x ^ y ^ z
	}
}

function ROTL(x, n)
{
	return (x<<n) | (x>>>(32-n))
}

Number.prototype.toHexStr = function()
{
	var s="", v
	for (var i=7; i>=0; i--){v=(this>>>(i*4)) & 0xf; s+=v.toString(16)}
	return s
}




