/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(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;
}

function showInfo(target, text, wait){
    if(wait){
        wait = 2000;
    }
    target.set('text', text);
    target.fade(1);
    (function(){
        target.fade(0);
    }).delay(wait);
}

//mootools style - add function on domready, and pass event.code
function isNumberKey(charCode){
    if ((charCode == 8) || (charCode == 9) || (charCode == 46) || (charCode >= 37 && charCode <= 40) || (charCode >= 48 && charCode <= 57) || (charCode >= 96 && charCode <= 105)){
        return true;
    }else{
        return false;
    }
}
/*
function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;

    return true;
}
*/
function capField(el){
    el.value = el.value.substr(0, 1).toUpperCase() + el.value.substr(1);
}

var ajaxGet = function(url, data, el, fnOnSuccess){
    new Request.HTML({
        headers: {'If-Modified-Since': 'Thu, 1 Jan 1970 00:00:00 GMT','Content-type':'text/html; charset=utf-8'},
        url: url,
        onRequest:function(){$(document.body).setStyle('cursor','progress');},
        method: 'get',
        evalScripts: true,
        evalResponse: true,
        data: data,
        update: el,
        onSuccess:function(tree, el, html){
            $(document.body).setStyle('cursor','auto');
            if(fnOnSuccess){
                var fn = eval(fnOnSuccess);
                fn(html);
            }
        }
    }).send();
};

var ajaxPost = function(url, form, el, fnOnSuccess){
    new Request.HTML({
        url: url,
        onRequest:function(){$(document.body).setStyle('cursor','progress');},
        data: form,
        evalScripts: true,
        evalResponse: true,
        update: el,
        onSuccess:function(tree, el, html){
            $(document.body).setStyle('cursor','auto');
            if(fnOnSuccess){
                var fn = eval(fnOnSuccess);
                fn(html);
            }
        }
    }).post();
};

var ajaxRequest;

var ajaxFast = function(url, data, fnOnSuccess, multipleRequests){
    if(ajaxRequest && !(multipleRequests)){
        console.log("cancel request");
        ajaxRequest.cancel();
    }
    ajaxRequest = new Request({
        url: url,
        onRequest: function(){
            $(document.body).setStyle('cursor', 'progress');
        },
        data: data,
        method: 'post',
        evalScripts: true,
        evalResponse: true,
        onSuccess: function(text, xml){
            $(document.body).setStyle('cursor', 'auto');
            if(fnOnSuccess){
                var fn = eval(fnOnSuccess);
                fn(text, xml);
            }
        }
    }).send();
    //ajaxRequest = null;
};

function buildAccordion(){
    //IE6 bullshit
    var heightValue = '';
    //if(window.ie6) heightValue='100%';

    var togglerName='dt.accordion_toggler_';
    var contentName='dd.accordion_content_';

    var counter=1;
    var toggler=$$(togglerName+counter);
    var content=$$(contentName+counter);

    while(toggler.length>0)
    {
        // Accordion
        new Accordion(toggler, content, {
            opacity: false,
            display: -1,
            alwaysHide: true,
            onComplete: function() {
                var element=$(this.elements[this.previous]);
                if(element && element.offsetHeight>0) element.setStyle('height', heightValue);
            },
            onActive: function(toggler) {
                toggler.addClass('open');
            },
            onBackground: function(toggler) {
                toggler.removeClass('open');
            }
        });

        // Selectors for the next level
        counter++;
        toggler=$$(togglerName+counter);
        content=$$(contentName+counter);
    }
}

var countImages = new Array();

var uploadImage = function(el, indx) {
    //if it exists...remove it...
    if($("ajax-temp")){
        $("ajax-temp").dispose();
    }
    //$(document.body).setStyle("cursor", "progress");
    el.getNext('img.loading').fade('in');

    var newInput = el.clone().inject($("frm_upload_images"));
    newInput.set("name", "txt_uploader");
    var divImage = el.getParent("div.add_images").getNext("div.div_image");

    var iFrame = new Element("iframe", {
        id: "ajax-temp",
        name: "ajax-temp",
        width: 0,
        height: 0,
        border: 0,
        styles: {
            'width': 0,
            'height': 0,
            'border': "none",
            'display': "none"
        }
    }).inject($("frm_upload_images"));

    iFrame.addEvent("load", function() {
        iFrame.removeEvent("load");
        var data = iFrame.contentWindow.document.body.innerHTML;
        var thumb = data.split('@');
        if(thumb.length < 2){
            alert(data);
            //$(document.body).setStyle("cursor", "auto");
            el.getNext('img.loading').fade('hide');
            return false;
        }
        //store image id
        var imageStore = divImage.getNext("input.txt_image_"+countImages[indx]);
        imageStore.value = thumb[0];

        countImages[indx]++;
        if(countImages[indx] > 2){
            el.setStyle("display", "none");
            el.value = '';
        }
        //thumb link
        var imgThumb = thumb[1] + '_thumb.' + thumb[2];
        var img = new Element("img", {
            'src': 'userimages/' + imgThumb,
            title: "Remove!",
            'events': {
                'click': function() {
                    imageStore.value = "";
                    countImages[indx]--;
                    this.dispose();
                    el.setStyle("display", "");
                }
            }
        }).inject(divImage);
        //$(document.body).setStyle("cursor", "auto");
        el.getNext('img.loading').fade('hide');
        newInput.dispose();
        el.value = '';
        //$("ajax-temp").dispose();
        //iFrame.dispose();
    });
    $("frm_upload_images").set("target", "ajax-temp");
    $("frm_upload_images").set("action", "uploadimage.php");
    $("frm_upload_images").set("method", "post");
    $("frm_upload_images").set("enctype", "multipart/form-data");
    $("frm_upload_images").set("encoding", "multipart/form-data");
    $("frm_upload_images").submit();
};


var createBookmark = function(title, url) {

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 };
 
 
 function highlightSmallThis(id){
 	id.style.zIndex = 10;
 	switch(id.id){
		case 'book_extra_s1':
 			document.getElementById('book_extra_s2').style.zIndex = 1
 			document.getElementById('book_extra_s3').style.zIndex = 1
		  break;
		case 'book_extra_s2':
 			document.getElementById('book_extra_s1').style.zIndex = 1
 			document.getElementById('book_extra_s3').style.zIndex = 1
		  break;
		case 'book_extra_s3':
 			document.getElementById('book_extra_s1').style.zIndex = 1
 			document.getElementById('book_extra_s2').style.zIndex = 1
		  break;
		}
 	document.getElementById('grayArea').style.display = ''
 	document.getElementById('grayArea').style.opacity = 0.5
 	document.getElementById('grayArea').style.filter='alpha(opacity=50)';
 	//var myEffect = new Fx.Morph('grayArea', {duration: 50, transition: Fx.Transitions.Sine.easeIn});
	//myEffect.start({'opacity': [0.5]});
 	}
 	
 function highlightThis(id){
 	id.style.zIndex = 10;
 	if(id.id=='grey_t1'){
 			document.getElementById('grey_t2').style.zIndex = 1
 		}else{
 			document.getElementById('grey_t1').style.zIndex = 1
 		}
 	document.getElementById('grayArea').style.display = 'block'
 	document.getElementById('grayArea').style.opacity = 0.5
 	document.getElementById('grayArea').style.filter='alpha(opacity=50)';
 	//var myEffect = new Fx.Morph('grayArea', {duration: 50, transition: Fx.Transitions.Sine.easeIn});
	//myEffect.start({'opacity': [0.5]});
 	}
 function unHightlightThis(){
 	document.getElementById('grayArea').style.display = 'none'
 	document.getElementById('grayArea').style.opacity = 0
 	document.getElementById('grayArea').style.filter='alpha(opacity=0)';
 	//var myEffect = new Fx.Morph('grayArea', {duration: 10, transition: Fx.Transitions.Sine.easeIn});
	//myEffect.start({'opacity': [0]});
 	
 	}
 
/*
 if (window.external) {
  document.write('<a href = "javascript:CreateBookmarkLink()");">Add to Favorites</a>');
  } else  if (window.sidebar) {
  document.write('<a href =  "javascript:CreateBookmarkLink()");">Bookmark Page</a>');
 } else if (window.opera && window.print) {
   document.write('<a href = "javascript:CreateBookmarkLink()");">Add Bookmark</a>');

 }
        */
