// change country
function showChangeCountry() {
	$("#flags").load("/ajax-change_country.cfm", function(data) {
		$("#flags").slideDown("fast");
	});
}
function loadSelectedSite(which) {
	$("#changecountrylink").load("/ajax-change_country.cfm?id="+which, function(data) {
		$("#flags").slideUp("fast");
		document.location.reload();
	});
}

// help
function showHelp(which) {
	winOpenCenter("/help.cfm?type="+which,"help_window","no","yes","no","no",450,300)
}

// decline request
function declineRequest(which,what) {
	if(confirm("Are you sure you want to decline this offer?")) {
		$.get("/ajax-decline_request.cfm?ID="+which,function(data) {
			$("#ri"+what).slideUp("slow");
		});
	}
}

// login stuff
function showLogin() {
	$("#login").load("/ajax-login_form.cfm");
}
function doLogin() {
	$.post("/ajax-login_form.cfm",{ Email: $("#loginEmail").val(), Password: $("#loginPassword").val() },function(data) {
		document.getElementById("login").innerHTML=data;
		tmp=setTimeout('$("#cpnav").load("/ajax-user_nav.cfm");',500);
	});
}

// forgot password
function forgotPassword() {
	$("#login").load("/ajax-forgot_password.cfm");
}
function forgotPasswordQuestion() {
	$.post("/ajax-forgot_password.cfm",{ Email: $("#loginEmail").val() },function(data) {
		document.getElementById("login").innerHTML=data;
	});
}

// post a comment to a lot
function postLotComment(lotid) {
	$.post("/ajax-post_comment.cfm",{ Comment: $("#newcomment").val(), LotID: lotid },function(data) {
		document.getElementById("commentsection").innerHTML=data;
		document.getElementById("newcomment").value="";
	});
}

// login alert
function loginAlert() {
	alert("You are not signed in, sign in or sign up to get rolling!");
	document.getElementById("loginEmail").focus();
}

// advanced search
function advancedSearch(r,pc,c,w,ws,we,prf,prt,so,q) {
	if(document.getElementById("advancedfilter").style.display=="none") {
		$("#advancedsearchbutton").removeClass("secondary");
		$("#advancedsearchbutton").addClass("secondary-sel");
	} else {
		$("#advancedsearchbutton").removeClass("secondary-sel");
		$("#advancedsearchbutton").addClass("secondary");
	}
	$("#advancedfilter").load("/ajax-advanced_search.cfm?r="+r+"&pc="+pc+"&c="+c+"&w="+w+"&ws="+ws+"&we="+we+"&prf="+prf+"&prt="+prt+"&so="+so+"&q="+q);
	$("#advancedfilter").toggle("slow");
}

// video
function openVideoWindow() {
	winOpenCenter("/video/video.cfm","video_window","no","no","no","no",720,480);
}

// image opening
function viewSpecifiedImage(which) {
	var x = window.open('','pictureViewer','width=800,height=800,resizable=1');
	if( !x ) { return true; }
	x.document.open();
	x.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>Picture Viewer<\/title>');
	x.document.write('<sc'+'ript type="text/javascript">');
	x.document.writeln('var isNN,isIE;');
	x.document.writeln('function reSizeToImage(){');
	if (navigator.appName.indexOf("Microsoft")==-1)
	{
		x.document.writeln('window.innerWidth=document.images["ex_popimage"].width;');
		x.document.writeln('window.innerHeight=document.images["ex_popimage"].height;');
		x.document.writeln('document.title="Picture Viewer";');
	}else if(window.opera){
		x.document.writeln('window.resizeTo(100,100);');
		x.document.writeln('width=130-(document.body.clientWidth-document.images[0].width);');
		x.document.writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		x.document.writeln('window.resizeTo(width,height);');
	}else{
		x.document.writeln('window.resizeTo(100,100);');
		x.document.writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		x.document.writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		x.document.writeln('window.resizeTo(width,height);');
	}
	x.document.writeln('}<\/sc'+'ript>');
	x.document.write('<\/head><body style="margin:0px;" id="ex_popbody" onload="reSizeToImage();self.focus();">');
	x.document.write('<img id="ex_popimage" name="ex_popimage" src="'+which+'" onclick="window.close();" alt="" \/><\/body><\/html>');
	x.document.close();
	if( x.focus ) { x.focus(); }
}
// Image switcher

function switchImages(which,what) {
	for(i=1;i<4;i++) {
		document.getElementById("img"+i+"a").className="image";
		document.getElementById("img"+i+"b").style.width="105px";
	}
	document.getElementById("img"+which+"b").style.width="90px";
	document.getElementById("img"+which+"a").className="image selected";
	document.getElementById("image").style.background="url('/images/bg-"+what+"-"+which+".jpg')";
}

// Window opening

function winOpenCenter(url,name,statbar,scroll,locate,resize,xWidth,yWidth)	{
var adjustedleft = 8//optional
var adjustedheight = 30//adjust height because of windows taskbar
var screenwidthremainder = screen.availWidth%2//really not needed, but won't hurt
var screenheightremainder = screen.availHeight%2
var screenwidth = screen.availWidth - screenwidthremainder
var screenheight = screen.availHeight - screenheightremainder
var winheight = yWidth //set new window height properties
var winwidth = xWidth //set new window width properties
var winleft = parseInt(screenwidth/2) - (winwidth/2) - adjustedleft//optional
var wintop = parseInt(screenheight/2) - (winheight/2) - adjustedheight

var win = window.open(url,name,'width=' +winwidth+ ',height=' +winheight+',status=' +statbar+',resizable='+resize+',scrollbars='+scroll+',location='+locate+',top='+wintop+',left='+winleft);
return win;
}

// Email Checker

function containsCharacter(str,testchar)
{
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) == testchar) return true;
	}
	return false;
}

function validCharacters(teststr,validstr)
{
	for (i = 0; i < teststr.length; i++) {
		if (validstr.indexOf(teststr.charAt(i)) < 0) return false;
	}
	return true;
}

function isEmailValid(str)
{
	var pos;
	var errorCount = 0;
	var email = str;
	
	var validStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.[]-_";

	if (email == "") {
		alert("Please enter an email address.");
		return false;
	}
	
	if (email.charAt(0) == "@" || email.charAt(0) == "." || email.charAt(0) == "[" || email.charAt(0) == "]" || email.charAt(0) == "-" || email.charAt(0) == "_") errorCount++;
	
	if (!validCharacters(email,validStr)) errorCount++;
		
	for (i = 1; i < email.length; i++) {
		if ((email.charAt(i-1) == "." && email.charAt(i) == ".") || (email.charAt(i-1) == "." && email.charAt(i) == "@") || (email.charAt(i-1) == "@" && email.charAt(i) == ".")) {
			errorCount++;
			break;
		}
	}

	if (!errorCount && email.indexOf("@") < 0 && email.length > 0) {
		alert("Your email address is missing the domain name. The format should be "+str+"@somedomain.\n\nFor example, "+str+"@aol.com, "+str+"@earthlink.net, or "+str+"@yahoo.com.");
		return false;
	}
	if (email.length < 6) errorCount++;
	pos = email.indexOf("@");
	email = email.substring(pos+1,email.length);
	if (email.indexOf("@") != -1) errorCount++;
	if ((email.charAt(0) == "[" && email.charAt(email.length-1) != "]") || (email.charAt(0) != "[" && email.charAt(email.length-1) == "]") || (email.charAt(0) == "]" || email.charAt(email.length-1) == "[")) errorCount++;
	if (email.indexOf(".") < 0) {
		errorCount++;
	}
	else {
		pos = email.lastIndexOf(".");
		email = email.substring(pos+1,email.length);
		if ((!isNaN(email) && email != "") || (email.charAt(email.length-1) == "]")) {
			pos = str.indexOf("@");
			if (email.charAt(email.length-1) == "]") {
				email = str.substring(pos+1,str.length-1);
				if (email.charAt(0) == "[") {
					email = email.substring(1,str.length);
				}				
			}
			else {
				email = str.substring(pos+1,str.length);
			}

			var ipAddress = "";
			var periods = 0;

			for (i = 0; i < email.length; i++) {
				if (email.charAt(i) == ".") {
					periods++;
					if ((isNaN(ipAddress)) || (ipAddress < 0 || ipAddress > 255)) {
						errorCount++
						break;
					}
					ipAddress = "";
				}
				else
					ipAddress += email.charAt(i).toString();
			}
			if ((isNaN(ipAddress)) || (ipAddress < 0 || ipAddress > 255)) errorCount++

			if (periods != 3) errorCount++;
		}
		else {
			if (email.length < 2 || email.length > 3) errorCount++;
		}
	}

	if (errorCount > 0) {
		alert(str+" is not a valid email address. Please check your typing.");
		return false;
	}
	return true;
}
