//main js functions for the site
var elem = false;
var encKey = "jaslkj34lkjals98098asdjlkjlkasd7987987";
var dmcSiteHost = "http://paulmoreno.org";

function new_window(url) {
	link = window.open(url,"Link","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=450,left=80,top=180");
}

function clearMessResp() {
	$("#mess_resp").html("&nbsp;");
}

function updateMessResp(msg) {
	$("#mess_resp").html(msg);
}

function submitValForm_dyn(formName,subPath,retPath,required) {
/*
Use this function to validate a form and then submit it to a destination

formName string : name of form you are submitting
subPath string : URL to submit form data to (ie: dynFormSub.php)
retPath string : URL to return form response to
required string : comma seperated list of required field names (ie: name,phone,email)
*/
	var data = new Array();
	var required_arr = new Array();
	var myForm;
	var qstring = '';
	var error = 0;
	required_arr = required.split(",");
	switch(formName) {
		case 'dmc_login_form':
			myForm = document.dmc_login_form.elements;
		break;
	}
	for(var i=0; i<myForm.length; i++) {
		data[myForm[i].name] = myForm[i].value;
		qstring += '&'+myForm[i].name+'='+myForm[i].value;
	}
	qstring += '&form_name='+formName;
	qstring += '&rURL='+retPath;
	
	if(required_arr.length > 0) {
		for(i=0;i<required_arr.length;i++) {
			if(data[required_arr[i]] == '') {
				error++;
			}	
		}
		if(error == 0) {
			document.location.href = subPath+'?'+qstring;
		} else {
			alert('Please include all required fields');
		}
	} else {
		document.location.href = subPath+'?'+qstring;
	}
}

//---------------------encryption------------------

/*
js/encrypt.js must be included to function.
*/

function dmcEncrypt(encValue)
{
	var resp;
	resp = textToBase64(rc4(encKey,encValue));
	return resp;
}

function dmcDecrypt(encValue)
{
	var resp;
	resp = rc4(encKey,base64ToText(encValue));
	return resp;
}

/*
Add bookmark script
*/

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Philip Myers :: http://virtualipod.tripod.com/bookmark.html */

function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}

/*
Add bookmark script
*/

//---------------------utility---------------------

function getUrlVars()
{
/**
Example for URL http://www.example.com/index.html?hello=bonjour&goodevening=bonsoir

var hash = getUrlVars();
alert(hash['hello']); // prints 'bonjour'
alert(hash['goodevening']); // prints 'bonsoir'
**/
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function chkObjectExists(inParent,theVal) {
/**
Written by Kumar S
http://www.guyfromchennai.com/?p=20

inParent bool - if true, looks in window.opener
theVal string - element id
**/
	if(inParent){
		if (window.opener.document.getElementById(theVal) != null) {
			return true;
		} else {
			return false;
		}
	} else {
		if (document.getElementById(theVal) != null) {
			return true;
		} else {
			return false;
		}
	}
}

function sanitizeServerAjaxCall(encText){
	var resp = encText;
	resp = resp.replace(/<div id="dmc_aui_cont">/,"");
	resp = resp.replace(/<\/div>/,"");
	return resp;
}

function whenLoading(){
	var e = document.getElementById('mess_resp'); 
	e.innerHTML = "Sending Data...";
}

function whenLoaded(){
	var e = document.getElementById('mess_resp'); 
	e.innerHTML = "Data Sent...";
}

function whenInteractive(){
	var e = document.getElementById('mess_resp'); 
	e.innerHTML = "Processing data...";
}

function openWindow(url,height,width,top,left,resize) {
	var myRand = parseInt(Math.random()*999999999);
	var name = "RandWindow_"+myRand;
	window.open(url,name,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable="+resize+",width="+width+",height="+height+",top="+top+",left="+left+"");
}

function pause(numberMillis) 
{
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
		return;
	}
} 

function openBrowseServer_fck(mode,path) {
/*
mode options: File Flash Image Media or none for all
*/
	var url;
	var name = 'ai_pop_browser';
	if(mode == 'none') {
		url = path+'cms/FCKeditor/editor/filemanager/browser/default/browser.html?Connector=connectors/asp/connector.asp';
	} else {
		url = path+'cms/FCKeditor/editor/filemanager/browser/default/browser.html?Type='+mode+'&Connector=connectors/asp/connector.asp';
	}
	window.open(url,name,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=700,height=450,top=80,left=80");
}

// --------------------AJAX METHODS VIA SACK ---------------------- //

function sackAjaxRequest(reqFile,method,respElement) {
	var ajax = new sack();
	ajax.requestFile = reqFile;
	ajax.method = method;
	ajax.element = respElement;
	ajax.onLoading = whenLoading;
	ajax.onLoaded = whenLoaded; 
	ajax.onInteractive = whenInteractive;
	//ajax.onCompletion = sackResponse;
	ajax.runAJAX();
}

function sackResponse() {
	return true;
}

// -----------------------jQuery--------------------------------//

function showElement(id) {
	$(id).show("slow");
}

function hideElement(id) {
	$(id).hide("slow");
}

function slideElementUp(id) {
	$(id).slideUp();
}

function slideElementDown(id) {
	$(id).slideDown();
}

function toggleElement(id) {
	$(id).toggle();
}

function fadeElementIn(id) {
	$(id).fadeIn("slow");
}

function fadeElementOut(id) {
	$(id).fadeOut("slow");
}

/** jqModal **/
function fireJqModal(dmcTrig, dmcModalElem, dmcTarget) {
	var triggers = $(dmcTrig)[0];
	
	$(dmcModalElem).jqm({
		trigger: triggers,
		/*ajax: 'server.aspx',*/
		target: dmcTarget,
		overlay: 50,
		onHide: function(h) { 
		  h.o.remove(); // remove overlay
		  h.w.fadeOut(888); // hide window 
		},
		onShow: function(h) { 
		  h.w.fadeIn(888); // hide window 
		}
	});
	
	if($.browser.msie) {
		$('div.jqmAlert .jqmClose')
		.hover(
		function(){ $(this).addClass('jqmCloseHover'); }, 
		function(){ $(this).removeClass('jqmCloseHover'); });
	}
}

function fireJqModalAjax(dmcTrig, dmcModalElem, dmcTarget) {
	var triggers = $(dmcTrig)[0];
	
	$(dmcModalElem).jqm({
		trigger: triggers,
		ajax:'@name',
		target: dmcTarget,
		overlay: 50,
		onHide: function(h) { 
		  h.o.remove(); // remove overlay
		  h.w.fadeOut(888); // hide window 
		},
		onShow: function(h) { 
		  h.w.fadeIn(888); // hide window 
		}
	});
	
	if($.browser.msie) {
		$('div.jqmAlert .jqmClose')
		.hover(
		function(){ $(this).addClass('jqmCloseHover'); }, 
		function(){ $(this).removeClass('jqmCloseHover'); });
	}
}

/*jq form method*/
function jqSubmitForm(mode) {
	var data = new Array();
	var myRand = parseInt(Math.random()*999999999);
	var filePath = dmcSiteHost+"/dmc_main/server.aspx";
	var myForm = document.form1;
	//var queryString = $("#form1 .jqFormField").fieldSerialize();
	var queryString = "rand="+myRand;
	for(var i=0; i<myForm.length; i++) {
		if(myForm[i].name != "__VIEWSTATE") {
			data[myForm[i].name] = myForm[i].value;
			queryString += '&'+myForm[i].name+'='+escape(myForm[i].value);
		}
	}
	if(queryString.length > 0) {
		$.get(filePath, queryString ,
		function(data){
			if(sanitizeServerAjaxCall(data) == "success") {
				$("#mess_resp").html("Form successfully submitted");
			} else {
				if(mode == "message") {
					$("#mess_resp").html("Failed to submit form");
				} else if(mode =="data") {
					$("#mess_resp").html(sanitizeServerAjaxCall(data));
				} else {
					$("#mess_resp").html("Failed to submit form");
				}
			}
			setTimeout("clearMessResp()",3000);
		});
	} else {
		$("#mess_resp").html("Please include all required fields");
		setTimeout("clearMessResp()",3000);
	}
}

function jqSubmitContributeForm(mode) {
	var data = new Array();
	var myRand = parseInt(Math.random()*999999999);
	var filePath = dmcSiteHost+"/dmc_main/server.aspx";
	var myForm = document.form1;
	var queryString = "rand="+myRand;
	for(var i=0; i<myForm.length; i++) {
		if(myForm[i].name != "__VIEWSTATE") {
			data[myForm[i].name] = myForm[i].value;
			queryString += '&'+myForm[i].name+'='+escape(myForm[i].value);
		}
	}
	if(queryString.length > 0) {
		$.get(filePath, queryString ,
		function(data){
			$("#mess_resp").html("Transferring to PayPal...");
			$("#PayPalRedirect").html(sanitizeServerAjaxCall(data));
		});
	} else {
		$("#mess_resp").html("Please include all required fields");
		setTimeout("clearMessResp()",3000);
	}
}