var clickthrough = false;
var pop_count = 0;

function popOnce(url, name, w, h, s) {
	if (pop_count == 0) {
		var options = 'toolbar=no,status=no,location=no,menubar=no,directories=no,copyhistory=no,resizable=no,width='+w+',height='+h+',scrollbars='+s;
		window.open(url, name, options);
	}
	pop_count++;
}

function popUp(url, name, w, h, s) {
	var options = 'toolbar=no,status=no,location=no,menubar=no,directories=no,copyhistory=no,resizable=no,width='+w+',height='+h+',scrollbars='+s;
	window.open(url, name, options);
}

function toggleImage(id, img) {
	document.getElementById(id).src = img;
}

function swap_image(obj, newimage) {
	obj.src = newimage;
}

function confirm_update() {
	if (confirm('Are you sure?')) {
		return true;
	}
	return false;
}

function exitpop(subid) {
	if (pop_img && pop_url) {
		var url = 'http://www.todayswebspecial.com/exitpop.php?img='+escape(pop_img)+'&url='+escape(pop_url)+'&SUBID=';
		if (subid) {
			url += escape(subid);
		}
		window.open(url,'exitPop','toolbar=no,status=no,location=no,menubar=no,resizable=no,copyhistory=no,scrollbars=no,directories=no,width=425,height=600,top=0,left=0');
	}
}

function add_mbox_session(obj) {
	var mboxSession = '';
	var mbox_cookie = readCookie('mbox');
	var tmp = mbox_cookie.split('|');
	for (i = 0; i < tmp.length; i++) {
		if (tmp[i].indexOf('session#') != -1) {
			var tmp2 = tmp[i].split('#');
			mboxSession = tmp2[1];
		}
	}
	if (mboxSession) {
		obj.href += '&mboxSession='+mboxSession;
	}
}
function open_window2(url, noclickthrough) {
	if (!noclickthrough) {
		clickthrough = true;
		if (typeof(properClickThrough) != 'undefined') {
			properClickThrough = true;
			USIdone = true;
		}
	}
	var mboxSession = '';
	var mbox_cookie = readCookie('mbox');
	var tmp = mbox_cookie.split('|');
	for (i = 0; i < tmp.length; i++) {
		if (tmp[i].indexOf('session#') != -1) {
			var tmp2 = tmp[i].split('#');
			mboxSession = tmp2[1];
		}
	}
	if (mboxSession) {
		url += '&mboxSession='+mboxSession;
	}
	window.open(url);
}

function open_window(url, noclickthrough) {
	if (!noclickthrough) {
		clickthrough = true;
		if (typeof(properClickThrough) != 'undefined') {
			properClickThrough = true;
			USIdone = true;
		}
	}
	var mboxSession = '';
	var mbox_cookie = readCookie('mbox');
	var tmp = mbox_cookie.split('|');
	for (i = 0; i < tmp.length; i++) {
		if (tmp[i].indexOf('session#') != -1) {
			var tmp2 = tmp[i].split('#');
			mboxSession = tmp2[1];
		}
	}
	if (mboxSession) {
		url += '&mboxSession='+mboxSession;
	}
	window.open(url, 'cart_window', 'toolbar=no,status=no,location=no,menubar=no,resizable=no,width=980,height=870,scrollbars=yes');
}

function open_cart(url) {
	window.open(url, 'cart_window', 'toolbar=no,status=no,location=no,menubar=no,resizable=no,width=920,height=650,scrollbars=yes');
}

function scrollpop(delay) {
	if (!document.all || document.all && window.XMLHttpRequest) {
		setTimeout("$('#scroller').animate({'right':'0'}, 2000)", delay);
	}
	else {
		setTimeout("$('#scroller').fadeIn(2000)", delay);
	}
}

function createCookie(name, value, days) {
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	}
	document.cookie = name+'='+value+expires+'; path=/';
}

function readCookie(name) {
	var value = '';
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			value = c.substring(nameEQ.length,c.length);
			break;
		}
	}
	return value;
}

function emailCheck(emailStr) {
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];
	if (user.match(userPat) == null) {
		alert("The username doesn't seem to be valid.");
		return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	var domainArray = domain.match(domainPat);
	if (domainArray == null) {
		alert("The domain name doesn't seem to be valid.");
		return false;
	}
	var atomPat = new RegExp(atom,"g");
	var domArr = domain.match(atomPat);
	var len = domArr.length;
	if ((domArr[domArr.length-1] != "info") && (domArr[domArr.length-1] != "name") && (domArr[domArr.length-1] != "arpa") && (domArr[domArr.length-1] != "coop") && (domArr[domArr.length-1] != "aero")) {
		if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3) {
			alert("The address must end in a three-letter domain, or two letter country.");
			return false;
		}
	}
	if (len < 2) {
	   var errStr = "This address is missing a hostname!";
	   alert(errStr);
	   return false;
	}
	return true;
}

function UPTvalidateform(thisform) {
	if (emailCheck(thisform.email.value)) {
		return false;
	}
	else {
		$('#email').focus();
		return true;
	}
}


