function create_request_object()
{
	NewAjaxObject = navigator.appName == 'Microsoft Internet Explorer' ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
	return NewAjaxObject;
}

// Current Page Reference (Stephen Chapman)
function get_url()
{
	var uri = new Object();

	uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
	uri.dom = uri.dir; // javascript.about.com
	if (uri.dom.substr(0,7) == 'http:\/\/') {
		uri.dom = uri.dom.substr(7);
	} else if (uri.dom.substr(0,8) == 'https:\/\/') {
		uri.dom = uri.dom.substr(8);
	}
	uri.path = ''; // library
	var pos = uri.dom.indexOf('\/');
	if (pos > -1) {
		uri.path = uri.dom.substr(pos+1);
		uri.dom = uri.dom.substr(0,pos);
	}
	uri.page = location.href.substring(uri.dir.length+1, location.href.length+1); // blurl1
	pos = uri.page.indexOf('?');
	if (pos > -1) {
		uri.page = uri.page.substring(0, pos);
	}
	pos = uri.page.indexOf('#');
	if (pos > -1) {
		uri.page = uri.page.substring(0, pos);
	}
	uri.ext = ''; // htm
	pos = uri.page.indexOf('.');
	if (pos > -1) {
		uri.ext =uri.page.substring(pos+1);
		uri.page = uri.page.substr(0,pos);
	}
	uri.file = uri.page; // blurl1.htm
	if (uri.ext != '') {
		uri.file += '.' + uri.ext;
	}
	if (uri.file == '') {
		uri.page = 'index';
	}
	uri.args = location.search.substr(1).split('?'); //

	return uri;
}

var uri = get_url();

// global vars
var gvar = new Object();
// cookie_domain
gvar.cookie_domain = uri.dom;
if (gvar.cookie_domain.substr(0,4) == 'www.') {
	gvar.cookie_domain = gvar.cookie_domain.substr(4);
}
gvar.cookie_domain = '.'+ gvar.cookie_domain;
// sub_dir
if (typeof(sub_dir) == 'undefined') {
	gvar.sub_dir = '';
} else {
	gvar.sub_dir = String(sub_dir);
}


// cookie functions
function set_cookie(name, value, expires, path, domain)
{
 	if (!expires) {
 		expires = 1000 * 86400 * 30;
 	} else {
 		expires = 1000 * expires;
 	}
	var cookieExpires = new Date();
	cookieExpires.setTime(cookieExpires.getTime() + expires);
 	if (!path) {
  		path = '; path=/';
 	}
 	if (!domain) {
 		domain = gvar.cookie_domain;
 	}

 	document.cookie = name +'='+ escape(value) +'; expires='+ cookieExpires.toGMTString() + path +'; domain='+ domain +';';
}
function fetch_cookie(name)
{
	cookie_name = name +'=';
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length) {
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name) {
			var value_end = document.cookie.indexOf(';', value_begin);
			if (value_end == -1) {
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(' ', cookie_begin) + 1;
		if (cookie_begin == 0) {
			break;
		}
	}
	return null;
}
function delete_cookie(name, expires, path, domain)
{
	expires = new Date();
 	expires.setTime(expires.getTime() - (1000 * 86400));
 	if (!path) {
 		path = '; path=/';
 	}
 	if (!domain) {
 		domain = gvar.cookie_domain;
 	}

 	document.cookie = name +'=; expires='+ expires.toGMTString() + path +'; domain='+ domain +';';
}

function sort_2d_array(ar, key, direction)
{
	if (!ar.length) {
		return ar;
	}
	// asc or desc
	var asc = direction == 'desc' ? false : true;
	var jc1, tmp_ar;
	// run sort length*(length-1) times
	for (var ic = 0; ic < ar.length; ic++) {
		// no need to sort the last element
		for (var jc = 0; jc < ar.length - 1; jc++) {
			jc1 = jc + 1;
			// if current element is bigger than the next one and we need increasing order
			// or current element is smaller than the next one and we need decreasing order
			if ((ar[jc][key] > ar[jc1][key] && asc) || (ar[jc][key] < ar[jc1][key] && !asc)) {
				// interchange sub-arrays places
				tmp_ar = ar[jc1];
				ar[jc1] = ar[jc];
				ar[jc] = tmp_ar;
			}
		}
	}
	// return array
	return ar;
}


function getCaptcha()
{
	var d = new Date();
	document.getElementById('captchaImage').setAttribute('src', 'captcha.php?r='+ d.getTime());
}

function checkpassword(formadi)
{
	if (formadi.password.value == '' || formadi.password2.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	if (formadi.password.value != formadi.password2.value) {
		alert('Passwords do not match.');
		return false;
	}
	return true;
}

var daily_digest_opt_displayed = false;
function daily_digest_opted_out()
{
	if (!daily_digest_opt_displayed) {
		var msg = 'To receive Daily Digest, please choose to receive emails in the "Edit Profile" section above.'+"\n('Email Me' checkbox)";
		alert(msg);
	}
	daily_digest_opt_displayed = true;
}

function checkquestion(formname)
{
	if (formname.question.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	return true;
}

function checklink(formname)
{
	if (formname.url.value == '' || formname.title.value == '' || formname.description.value == '') {
		alert('Please fill all required(*) fields.');
		return false;
	}
	return true;
}

function checktestimonial(formname)
{
	if (formname.name.value == '' || formname.testimonial.value == '') {
		alert('Please fill all required(*) fields.');
		return false;
	}
	return true;
}

function checkarticle(formname)
{
	if (formname.title.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	return true;
}

function checkevent(formname)
{
	var error_title = "Required field(s):\n\n"
	var error_msg = "";
	var i = 1;

	if (formname.title.value == '') {
		error_msg = error_msg + i + ". Enter field Title\n";
		i++;
	}

	if (formname.hosted_by.value == '') {
		error_msg = error_msg + i + ". Enter field Hosted By\n";
		i++;
	}

	if (error_msg) {
		alert(error_title+error_msg);
		return false;
	}
  return true;
}

function ShowHideElementByID(id)
{
	// admin@flashmx.us
	if (document.getElementById) {
		// DOM3 = IE5, NS6
		if (document.getElementById(id).style.display == 'none') {
			document.getElementById(id).style.display = 'block';
		} else {
			document.getElementById(id).style.display = 'none';
		}
	} else {
		if (document.layers) {
			if (document.id.display == 'none') {
				document.id.display = 'block';
			} else {
				document.id.display = 'none';
			}
		} else {
			if (document.all.id.style.visibility == 'none') {
				document.all.id.style.visibility = 'block';
			} else {
				document.all.id.style.visibility = 'none';
			}
		}
	}
}

function checksendtofriend(formname)
{
	if (formname.name1.value == '' || formname.email1.value == '' || formname.name2.value == '' || formname.email2.value == '' || formname.message.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	return true;
}

function checkcontactus(formname)
{
	if (formname.namesurname.value == '' || formname.email.value == '' || formname.subject.value == '' || formname.message.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	return true;
}

function checkcreditcard(formadi)
{
	if (formadi.paybycheck.checked == false && formadi.paybyphone.checked == false) {
		if (formadi.Ecom_Payment_Card_Type.value == '' || formadi.Ecom_Payment_Card_Name.value == ''
				|| formadi.Ecom_Payment_Card_Number.value == '' || formadi.Ecom_Payment_Card_ExpDate_Month.value == ''
				|| formadi.Ecom_Payment_Card_ExpDate_Year.value == '' || formadi.Ecom_Payment_Card_Verification.value == '') {
			alert('If you want to pay with credit card, please fill all fields.');
			return false;
		}
	}
	return true;
}

function checkcomment(formadi)
{
	if (formadi.comment.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	return true;
}

function checkcard(formadi)
{
	if (formadi.Ecom_Payment_Card_Type.value == '' || formadi.Ecom_Payment_Card_Name.value == ''
			|| formadi.Ecom_Payment_Card_Number.value == '' || formadi.Ecom_Payment_Card_ExpDate_Month.value == ''
			|| formadi.Ecom_Payment_Card_ExpDate_Year.value == '' || formadi.Ecom_Payment_Card_Verification.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	return true;
}

function checkbilling(formadi)
{
	if (formadi.Ecom_ShipTo_Postal_Name_First.value == '' || formadi.Ecom_ShipTo_Postal_Name_Last.value == ''
			|| formadi.Ecom_ShipTo_Postal_Street_Line1.value == '' || formadi.Ecom_ShipTo_Postal_City.value == ''
			|| formadi.Ecom_ShipTo_Postal_PostalCode.value == ''
			|| (formadi.Ecom_ShipTo_Postal_StateProv.value == '' && formadi.Ecom_ShipTo_Postal_StateProv_Typein.value == '')
			|| formadi.Ecom_ShipTo_Postal_CountryCode.value == '' || formadi.Ecom_ShipTo_Online_Email.value == ''
			|| formadi.Ecom_ShipTo_Telecom_Phone_Number.value == '') {
		alert('Please fill all required(*) fields.');
		return false;
	}
	if (formadi.copy_delivery.checked == false) {
		if (formadi.Ecom_BillTo_Postal_Name_First.value == '' || formadi.Ecom_BillTo_Postal_Name_Last.value == ''
				|| formadi.Ecom_BillTo_Postal_Street_Line1.value == '' || formadi.Ecom_BillTo_Postal_City.value == ''
				|| formadi.Ecom_BillTo_Postal_PostalCode.value == ''
				|| (formadi.Ecom_BillTo_Postal_StateProv.value == '' && formadi.Ecom_BillTo_Postal_StateProv_Typein.value == '')
				|| formadi.Ecom_BillTo_Postal_CountryCode.value == '') {
			alert('Please fill all required(*) fields.');
			return false;
		}
	}
	return true;
}
function billing_country_onchange(num)
{
	// assign and check
	// state drop-down
	var str = parseInt(num) == 2 ? 'Bill' : 'Ship';
	var p = 'Ecom_'+ str +'To_Postal_StateProv';
	var i_p = 'Input_'+ p;
	var t_p = 'Title_'+ p;
	if (!document.getElementById(p) || !document.getElementById(i_p) || !document.getElementById(t_p)) {
		return false;
	}
	// state input text
	var pr = 'Ecom_'+ str +'To_Postal_StateProv_Typein';
	var i_pr = 'Input_'+ pr;
	var t_pr = 'Title_'+ pr;
	if (!document.getElementById(pr) || !document.getElementById(i_pr) || !document.getElementById(t_pr)) {
		return false;
	}
	// country
	var ctry = 'Ecom_'+ str +'To_Postal_CountryCode';
	if (!document.getElementById(ctry)) {
		return false;
	}
	// attempt to change
	if (document.getElementById(ctry).value == '0') {
		// new country is usa
		document.getElementById(pr).value = '';
		document.getElementById(i_pr).style.display = 'none';
		document.getElementById(i_p).style.display = '';
		//document.getElementById(p).focus();
		document.getElementById(t_pr).style.display = 'none';
		document.getElementById(t_p).style.display = '';
	} else {
		// new country is not usa
		document.getElementById(p).value = '';
		document.getElementById(i_p).style.display = 'none';
		document.getElementById(i_pr).style.display = '';
		//document.getElementById(pr).focus();
		document.getElementById(t_p).style.display = 'none';
		document.getElementById(t_pr).style.display = '';
	}
}
function billing_address_onload()
{
	// call 'on user press back' in 0.1 sec
	setTimeout('billing_address_onback()', 100);
}
// act on browser auto-fill
function billing_address_onback()
{
	// switch second state/province
	billing_country_onchange(2);
	// hide billing details
	if (document.getElementById('copy_delivery') && document.getElementById('billing_details')) {
		if (document.getElementById('copy_delivery').checked) {
			document.getElementById('billing_details').style.display = 'none';
		}
	}
	// switch first state/province
	billing_country_onchange(1);
}

function checkfreecourse(formadi)
{
	if (formadi.email.value == '' || formadi.email2.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	if (formadi.email.value != formadi.email2.value) {
		alert('Emails do not match.');
		return false;
	}
	if (!_valid_email(formadi.email.value)) {
		alert('Enter a valid E-mail address.');
		formadi.email.focus();
		return false;
	}
	return true;
}

function checkregister(formadi)
{
	if (formadi.firstname.value == '' || formadi.lastname.value == '' || formadi.email.value == '' || formadi.username.value == '' || formadi.password.value == '' || formadi.password2.value == '') {
		alert('Please fill all fields.');
		return false;
	}
	if (formadi.password.value != formadi.password2.value) {
		alert('Passwords do not match.');
		return false;
	}
	if (formadi.agreement.checked == false) {
		alert('You must agree with the agreement');
		return false;
	}
	if (!_valid_email(formadi.email.value)) {
		alert('Enter a valid E-mail address.');
		formadi.email.focus();
		return false;
	}
	return true;
}

function popUp(URL)
{
	day = new Date();
	id = day.getTime();
	eval('page'+ id +" = window.open(URL, '"+ id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=600,left = 390,top = 100');");
}

function _valid_email(str)
{
	var at = '@';
	var dot = '.';
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	if (str.indexOf(at) == -1) {
		return false;
	}
	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
		return false;
	}
	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
		return false;
	}
	if (str.indexOf(at, (lat + 1)) != -1) {
		return false;
	}
	if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
		return false;
	}
	if (str.indexOf(dot, (lat + 2)) == -1) {
		return false;
	}
	if (str.indexOf(' ') != -1) {
		return false;
	}
	return true;
}
