﻿// JScript File

/*  Ex, version 1.1.0
 *  (c) 2007 Basil Goldman <basil@mantis.co.il>
 *
 *  Ex is freely distributable with Prototype 1.4.0 under the terms of an MIT-style license.
 *  For details:
 *
/*--------------------------------------------------------------------------*/

var Ex = {

    _resultElementPrefixId:'',
    _expandTitle:'',
    _closeTitle:'',

    toggleList: function(obj, id, rows) {
	    if (rows > 3) {
		    for (var x = 4; x < rows + 1; x++)
		    if (document.getElementById(Ex._resultElementPrefixId + id + '_' + x).style.display == 'block') {
			    document.getElementById(Ex._resultElementPrefixId + id + '_' + x).style.display = 'none';
			    obj.innerHTML = Ex._expandTitle + " <img src=../Resources/"+veidan._culture+"/Images/Arrows/arrow_black.gif  />";
			    obj.title = Ex._expandTitle;
		    }
		    else { document.getElementById(Ex._resultElementPrefixId + id + '_' + x).style.display = 'block'; obj.innerHTML = Ex._closeTitle; obj.title=Ex._closeTitle; }
	    }
    },

    _total:0,
    _showPerItem:0,
    _itemElementId:'image_',

    showNextItem: function() {
        var y = Ex._showPerItem;
        for(var x = 1; x <= Ex._total; x++) {
            if(x > y) { /*$('image_'+x).show();*/ }
        }
    },

    ReplaceFolderNameOrFileName: function(obj) {
        var e = obj.value.toLowerCase();
        if (e.length==2){
		    alert ("2 charcters name of the file or folder are reserved for system file struction - please change");
		    obj.focus();
        }
        obj.value=Ex.IsAmpersand(e.replace(/ /g,"_").toLowerCase());
    },

    IsAmpersand: function(s) {
        return s.replace(/&/g,"and").toLowerCase();
    },

    OpenPopUp: function(Title, Path, Width, Height) {
        window.open('','_blank','width='+Width+',height='+Height+',top=100,left=100,resizable=yes,scrollbars=no');
	    //image.document.write("<html>\n<head>\n<title>"+Title+"</title>\n</head>");
	    //image.document.write("<body style=\"margin:0;padding:0;\">\n");
	    //image.document.write("<object type=\"application/x-shockwave-flash\" data=\""+Path+"\" width=\""+Width+"\" height=\""+Height+"\">");
	    //image.document.write("<param name=\"movie\" value=\""+Path+"\">");
	    //image.document.write("</object>");
	    //image.document.write("</body>\n</html>\n");
	    return true;
    }
}

/*--------------------------------------------------------------------------*/

var Record = {

    _last:null,
    _visible:0,
    _total:0,
    _temp:1,
    _prevElementId:'',
    _nextElementId:'',
    _recordElementPrefixId:'',

    load: function() {
        var y = Record._visible;
        if(Record._total > y) {
            Record._visible++;
            $(Record._nextElementId).show();
            $(Record._nextElementId).onclick = function() { Record.next(); }


        }

        for(var x = 1; x <= Record._total; x++) {
            if(x > y) { if($(Record._recordElementPrefixId + x)) $(Record._recordElementPrefixId + x).hide(); }
        }
    },

    prev: function() {
        Record._temp--;
        Record._visible--;

        $(Record._recordElementPrefixId + Record._temp).show();
        $(Record._recordElementPrefixId + Record._visible).hide();

        if(Record._temp == 1) { $(Record._prevElementId).hide(); }
        if(Record._visible == Record._total) { $(Record._nextElementId).show(); };
    },

    next: function() {
        if(Record._visible <= Record._total) {
            $(Record._recordElementPrefixId + (Record._visible)).show(); $(Record._recordElementPrefixId + Record._temp).hide(); $(Record._nextElementId).onclick = function() { Record.next(); }
        }
        if(Record._visible == Record._total) { $(Record._nextElementId).hide(); }
        if($(Record._prevElementId).visible != false) { $(Record._prevElementId).show(); $(Record._prevElementId).onclick = function () { Record.prev(); } }

        Record._temp++;
        Record._visible++;
    },

    init: function() { Record.load(); }
}

/*--------------------------------------------------------------------------*/

var ElementFinder = {

    GetElementsByClassName: function(oElm, strTagName, strClassName, url) {
        var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
            var arrReturnElements = new Array();
            strClassName = strClassName.replace(/\-/g, "\\-");
            var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
            var oElement;
            for(var i=0; i<arrElements.length; i++){
                oElement = arrElements[i];
                if(oRegExp.test(oElement.className)){
                    arrReturnElements.push(oElement);
                    oElement.title = oElement.innerHTML;
                    oElement.innerHTML = oElement.innerHTML + " <img src="+url+" />";

                }
            }
    }
}

/*--------------------------------------------------------------------------*/

var Question = {

    ElementPrefixId:'',
    ValidatorId:'_Validator',

    SetQuestionAnswer: function(QuestionCategoryId) {
        Question.ElementPrefixId = QuestionCategoryId;
        Catalog = [];
        for(var x = 0; x < 10; x++) {
            if($(Question.ElementPrefixId + "_TextAnswer_" + x)) {
                    Catalog[x] = $(Question.ElementPrefixId + "_TextAnswer_" + x).value;
            }
        }

	//if(Question.ValidId(Question.ElementPrefixId + "_TextFirstName")) { }
	//if(Question.ValidId(Question.ElementPrefixId + "_TextLastName")) { }
	//if(Question.Echeck(Question.ElementPrefixId + "_TextEmail")) { result = true; } else { $(Question.ElementPrefixId + "_TextEmail" + Question.ValidatorId).show(); }
	//if(Question.ValidId(Question.ElementPrefixId + "_TextPhone")) {  }
	//if(Question.ValidId(Question.ElementPrefixId + "_TextJobTitle")) {  }
	//if(Question.ValidId(Question.ElementPrefixId + "_TextCompany")) {  }

	if(Question.ValidId(Question.ElementPrefixId + "_TextFirstName") && Question.ValidId(Question.ElementPrefixId + "_TextLastName") && Question.ValidId(Question.ElementPrefixId + "_TextEmail") && Question.ValidId(Question.ElementPrefixId + "_TextPhone") && Question.ValidId(Question.ElementPrefixId + "_TextJobTitle") && Question.ValidId(Question.ElementPrefixId + "_TextCompany")) {
		if(Question.Echeck(Question.ElementPrefixId + "_TextEmail")) {
		
		    //alert(Catalog.length + "___________" +QuestionCategoryId);
		    
			Mantis.Veidan.Pages.Content.Custom_Controls_QuestionForm.SetQuestionAnswer(
			$(Question.ElementPrefixId + "_TextFirstName").value,
			$(Question.ElementPrefixId + "_TextLastName").value,
			$(Question.ElementPrefixId + "_TextEmail").value,
			$(Question.ElementPrefixId + "_TextPhone").value,
			$(Question.ElementPrefixId + "_TextJobTitle").value,
			$(Question.ElementPrefixId + "_TextCompany").value,

			QuestionCategoryId,
			Catalog,

			function(response) {
				if(response.value) {
				    $('Message_' + QuestionCategoryId).show();
				    document.getElementById('PanelForm_'+QuestionCategoryId).style.display = "none";
				} else { alert("Exception: Error"); }
			});
		} else { $(Question.ElementPrefixId + "_TextEmail" + Question.ValidatorId).show(); }
	}
    },


	ValidId: function(id) {
		var o = document.getElementById(id);
		if(o.value == "" || o.value == null) {
			$(o.id + Question.ValidatorId).show();
			//o.focus()
			return false
		  } else {
			$(o.id + Question.ValidatorId).hide();
			return true
		  }
	},

    Valid: function(o) {
//        if(o.value == "" || o.value == null) {
//            $(o.id + Question.ValidatorId).show();
//            o.focus()
//            return false
//        } else {
//            $(o.id + Question.ValidatorId).hide();
//        }
    },

    Echeck: function(id) {
	var o = document.getElementById(id);
	var str = o.value;

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		var lastDot=str.lastIndexOf(dot);

		if (str.indexOf(at)==-1){

		   return false
		}

		if ((lastDot+1)==lstr){

		   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
    }
}

var Toogle = {

    Next: function(id) {
        
        if($(id).visible() != true) { $(id).show(); } else { $(id).hide(); }
    },
    
    Prev: function(id) {
        if($(id).visible() != true) { $(id).show(); } else { $(id).hide(); }
    }
}