/*
+----------------------------------------------------------------------+
| Copyright (c) 2007 Vertical Insights					               |
+----------------------------------------------------------------------+
| Authors: Scott Rogers <scott@verticalinsights.com>                   |
+----------------------------------------------------------------------+
 Created:      January 9, 2008
 Last Updated: January 9, 2008
*/

/* STAY TUNED
*/
stay_tuned_submit = function () {
	
	var pars = '';
	
	for (var i = 0; i < arguments.length; i++) {
		if (arguments[i] == ' Your First Name' ||
			arguments[i] == ' Your Last Name' ||
			arguments[i] == ' Your E-mail Address' ||
			arguments[i] == ' Your Phone Number'
			) {
			arguments[i] = 'NULL';
		}

		if (i > 0) { pars += '&pars[]=' + urlencode(arguments[i]); }
			else { pars += 'pars[]=' + urlencode(arguments[i]); }

	}
	
	new Ajax.Request (
		'/files_includes/processors/stay_tuned.inc.php',
		{
			parameters: pars,
			method: 'post',
			evalScripts: true,
			onSuccess: ajax_success,
			onFailure: ajax_failure
		}
	);
	
}

/* AJAX FEEDBACK USAGE:
Place this XHTML where ever you want the error to show up.
<div id="ajax_success" style="display: none;"><div class="feedback" id="success" style="font-size: 9px; padding: 2px;"></div><br /></div>
<div id="ajax_failure" style="display: none;"><div class="feedback" id="error" style="font-size: 9px; padding: 2px;"></div><br /></div>

This is the usage in the ajax statement
new Ajax.Request('./to/some/php/file.php', {parameters: pars, method: 'post', evalScripts: true, onSuccess: ajax_success, onFailure: ajax_failure});

SOMEWHERE INSIDE file.php
if ( conditional TRUE ) { header('HTTP/1.1 200 You successfully blah blah blah!'); }
	else { header('HTTP/1.1 400 Could not update blah blah blah.'); }
*/
ajax_success = function (feedback) {
	$('success').innerHTML = feedback.statusText;
	$('ajax_success').style.display = 'block';
	new Effect.Fade($('ajax_success'), {delay: 5, duration: 2.5});
	new Effect.BlindUp($('ajax_success'), {delay: 5, duration: 2.5});
}

ajax_failure = function (feedback) {
	$('error').innerHTML = feedback.statusText;
	$('ajax_failure').style.display = 'block';
	new Effect.Fade($('ajax_failure'), {delay: 7, duration: 2.5});
	new Effect.BlindUp($('ajax_failure'), {delay: 7, duration: 2.5});
}

/* AJAX STATUS:
Place this XHTML where ever you want the error to show up.
<div id="ajax_status" style="display: none;"><img src="/files_images/ajax_status/indicator_000000.gif" alt="Loading . . . Please Wait!" title="Loading . . . Please Wait!" name="ajax_status_img" border="0" id="ajax_status_img" /></div>

This is the usage in the ajax statement
new Ajax.Request('./to/some/php/file.php', {parameters: pars, method: 'post', evalScripts: true, onLoading: ajax_loading, onComplete: ajax_complete});
*/
ajax_loading = function () {
	$('ajax_status').style.display = 'block';
}

ajax_complete = function () {
	$('ajax_status').style.display = 'none';
}

// SUBMIT ON ENTER
submitOnEnter = function (myfield, e) {
	var keycode;
	if (window.event) { keycode = window.event.keyCode; }
		else if (e) { keycode = e.which; }
		else { return true; }
	if (keycode == 13) {
		myfield.form.submit();
		return false;
	}
	else { return true; }
}

// CONFIRM SUBMIT
confirmSubmit = function (msg) {
	var agree = confirm(msg);
	if (agree) { return true; }
		else { return false; }
}

// CONFIRM FORM SUBMIT
confirmFormSubmit = function (form_id, msg) {
	if (document.getElementById) { var	myForm	= document.getElementById(form_id); }
	var agree = confirm(msg);
	if (agree) {
		myForm.submit();
		return false;
	} else { return false; }
}

// UPDATE PARENT
NL_updateParent = function (URL) { //v1.0
  opener.document.location = URL;
}

/* SHOW/HIDE

TODO:
1) MAKE 'id' parameters dynamic (for() loop?)
*/
showhide_l10 = function (id1, id2, id3, id4) {
	if (document.getElementById) {

		if (id1 != 'NULL') { obj1 = document.getElementById(id1); } else { obj1 = ''; }
		if (id2 != 'NULL') { obj2 = document.getElementById(id2); } else { obj2 = ''; }
		if (id3 != 'NULL') { obj3 = document.getElementById(id3); } else { obj3 = ''; }
		if (id4 != 'NULL') { obj4 = document.getElementById(id4); } else { obj4 = ''; }

		if (obj1 != '' && obj1.style.display == "none") { obj1.style.display = ""; }
			else if (obj1 != '' && obj1.style.display == "") { obj1.style.display = "none"; }
		if (obj2 != '' && obj2.style.display == "none") { obj2.style.display = ""; }
			else if (obj2 != '' && obj2.style.display == "") { obj2.style.display = "none"; }
		if (obj3 != '' && obj3.style.display == "none") { obj3.style.display = ""; }
			else if (obj3 != '' && obj3.style.display == "") { obj3.style.display = "none"; }
		if (obj4 != '' && obj4.style.display == "none") { obj4.style.display = ""; }
			else if (obj4 != '' && obj4.style.display == "") { obj4.style.display = "none"; }
	}
}


/**
 * Emulates insertAdjacentHTML(), insertAdjacentText() and 
 * insertAdjacentElement() three functions so they work with Netscape 6/Mozilla
 * by Thor Larholm me@jscript.dk
 */
if (typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement) {
	HTMLElement.prototype.insertAdjacentElement = function (where,parsedNode) {
	  switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this); break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild); break;
		case 'beforeEnd':
			this.appendChild(parsedNode); break;
		case 'afterEnd':
			if (this.nextSibling) { this.parentNode.insertBefore(parsedNode,this.nextSibling); }
				else { this.parentNode.appendChild(parsedNode); }
			break;
	  }
	};

	HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) {
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML);
	};

	HTMLElement.prototype.insertAdjacentText = function (where,txtStr) {
		var parsedText = document.createTextNode(txtStr);
		this.insertAdjacentElement(where,parsedText);
	};
}

var toQueryComponent = function (input) {
    if (!input.name || input.disabled)
        return "";

    var n = urlencode(input.name);

    switch (input.type) {
    case "text":
    case "password":
    case "submit":
    case "hidden":
        return n + "=" + urlencode(input.value);
    case "textarea":
        // normalize line breaks as CR LF pairs as per RFC 1866
        var v = input.value.split(/\r\n|\r|\n/).join("\r\n");
        return n + "=" + urlencode(v);
    case "checkbox":
    case "radio":
        if (!input.checked)
            return "";
        var v = getRealValue(input);
        if (v === null) v = "on";
        return n + "=" + urlencode(v);
    case "select-one":
    case "select-multiple":
        var nvp = [];
        var opt, i = 0;
        while ((opt = input.options[i++]) != null) {
            if (opt.selected) {
                var v = getRealValue(opt);
                if (v === null) v = opt.text;
                // older versions of IE do not support Array.push
                nvp[nvp.length] = n + "=" + urlencode(v);
            }
        }
        return nvp.join("&");
    default:
        // input types reset, button, image, and file not implemented
        return "";
    }
}

var urlencode = function (str) {
    var v;
    try { v = encodeURIComponent(str); } catch (e) { v = escape(str); }
    //return v.replace(/%20/g,"+");
    return v;
}

var getRealValue = function (input) {
    var attr = input.getAttributeNode("value");
    return (attr && attr.specified) ? input.getAttribute("value") : null;
}

var buildQueryString = function (form) {
    var str = "";
    var element, i = 0;
    while ((element = form.elements[i++]) != null) {
        var qc = toQueryComponent(element);
        if (qc != "") str += "&" + qc;
    }
    return str.substring(1);
}

//--------------------------------------------------------------------//
//                        MACROMEDIA DEFAULT JS                       //
//--------------------------------------------------------------------//
MM_goToURL = function () { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

MM_preloadImages = function () { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

MM_swapImgRestore = function () { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

MM_findObj = function (n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

MM_swapImage = function () { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

MM_displayStatusMsg = function (msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

MM_openBrWindow = function (theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

MM_popupMsg = function (msg) { //v1.0
  alert(msg);
}

MM_jumpMenu = function (targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

MM_jumpMenuGo = function (selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}

MM_changeProp = function (objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}
//--------------------------------------------------------------------//
