function loadjscssfile(filename, filetype){
     if (filetype=="js"){
        var fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", filename);
     }
     else if (filetype=="css"){
        var fileref=document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", filename);
     }
     if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}

var DownloaderWidget = (function () {
    var BrowserInfo = {
        name: null,
        acrobat: null,
        acrobat_ver: null
    };

    function launchDownload(href) {
        info = detectReader();

        if (info.acrobat) {
            parent.$gJQ.fancybox({
                'hideOnOverlayClick': 'true',
                'centerOnScroll': 'true',
                'autoDimensions': 'true',
                'type': 'iframe',
                'href': href,
                'width': '100%',
                'height': '100%'
            });
        }
        else {
            var frame = parent.document.createElement("iframe");
            frame.style.display = "none";
            frame.src = href;
            parent.document.body.appendChild(frame);
            parent.$gJQ.fancybox.close();
        }
    }

    function detectReader() {
        if (navigator && (navigator.userAgent.toLowerCase()).indexOf("chrome") > -1) BrowserInfo.name = "chrome";
        else if (navigator && (navigator.userAgent.toLowerCase()).indexOf("msie") > -1) BrowserInfo.name = "ie";
        else if (navigator && (navigator.userAgent.toLowerCase()).indexOf("firefox") > -1) BrowserInfo.name = "firefox";
        else if (navigator && (navigator.userAgent.toLowerCase()).indexOf("safari") > -1) BrowserInfo.name = "safari";
        else BrowserInfo.name = "other";


        try {
            if (BrowserInfo.name == "ie") {
                var control = null;

                try {
                    control = new ActiveXObject('AcroPDF.PDF');
                }
                catch (e) { }

                if (!control) {
                    try {
                        control = new ActiveXObject('PDF.PdfCtrl');
                    }
                    catch (e) { }
                }

                if (!control) {
                    BrowserInfo.acrobat == null;
                    return browser_info;
                }

                version = control.GetVersions().split(',');
                version = version[0].split('=');
                BrowserInfo.acrobat = "installed";
                BrowserInfo.acrobat_ver = parseFloat(version[1]);
            }
            else if (BrowserInfo.name == "chrome") {
                for (key in navigator.plugins) {
                    if (navigator.plugins[key].name == "Chrome PDF Viewer" || navigator.plugins[key].name == "Adobe Acrobat") {
                        BrowserInfo.acrobat = "installed";
                        BrowserInfo.acrobat_ver = parseInt(navigator.plugins[key].version) || "Chome PDF Viewer";
                    }
                }
            }
            else if (navigator.plugins != null) {
                var acrobat = navigator.plugins['Adobe Acrobat'];
                if (acrobat == null) {
                    BrowserInfo.acrobat = null;
                    return browser_info;
                }
                BrowserInfo.acrobat = "installed";
                BrowserInfo.acrobat_ver = parseInt(acrobat.version[0]);
            }
        }
        catch (e) {
            BrowserInfo.acrobat_ver = null;
        }

        return BrowserInfo;
    }

    return {
        launchDownload: launchDownload
    }
})();

var GenericForm = (function() {
        var bindings = new Array();
        var _id;
        var _action;
        
        function initialize(id, action){
            if(typeof($gJQ) == "undefined"){$gJQ = $}
            
            $gJQ('#btnSubmit').click(form_submit);
            _id = id;
            _action = action;
            if (action){
                document.getElementById(_id).action = _action;
            }
            
            for (var element in bindings){
                if (document.getElementById(element).addEventListener){
			document.getElementById(element).addEventListener("blur", function(){field_blur(this);}, false);
		}
		else{
			document.getElementById(element).attachEvent("blur", function(){field_blur(this);});
		}
            }
        }
        
        function field_blur(field){
            var parts = bindings[field.id].split("[");
            var params = parts[0].split(";");
            var errorField = parts[1];
            
            for (var i in params){
                switch(params[i]){
                    case "mandatory":
                        if (validate_mandatory(field.id, errorField)){
                            continue;
                        }
                        return false;
                    case "email":
                        if (validate_mail(field.id, errorField)){
                            continue;
                        }
                        return false;
                    case "alpha":
                        if (validate_alpha(field.id, errorField)){
                            continue;
                        }
                        return false;
                    case "numeric":
                        if (validate_numeric(field.id, errorField)){
                            continue;
                        }
                        return false;
                }
            }
            
            return true;
        }
        
	function form_submit(){
		var formValid = true

		for (var element in bindings){
                    if (!field_blur(document.getElementById(element))){
                        formValid = false;
                    }
                }
                
		if (formValid){
                    $gJQ("#" + _id).trigger("submit");
                    //document.getElementById(_id).submit();
		}
	}
        
        function form_onSubmit(overrideFunction){
            $gJQ("#" + _id).submit(overrideFunction);
        }
        
        function validate_mail(strIdField, strIdLabelErr){
            if (!checkEmailAddressSyntax($gJQ("#" + strIdField).val())){
                    $gJQ("#" + strIdLabelErr).show()
                    return false
            }
            $gJQ("#" + strIdLabelErr).hide()
            return true
        }
        
        function validate_alpha(strIdField, strIdLabelErr){			
                var regExp = /^\D+$/
                if(!stringEmpty(document.getElementById(strIdField).value) && !regExp.test(document.getElementById(strIdField).value)){
                        $gJQ("#" + strIdLabelErr).show()
                        return false
                }
                $gJQ("#" + strIdLabelErr).hide()
                return true
        }
        
        function validate_numeric(strIdField, strIdLabelErr){
                var regExp = /^\d+$/
                if(!stringEmpty(document.getElementById(strIdField).value) && !regExp.test(document.getElementById(strIdField).value)){
                        $gJQ("#" + strIdLabelErr).show()
                        return false
                }
                $gJQ("#" + strIdLabelErr).hide()
                return true
        }
        
        function validate_mandatory(strIdField, strIdLabelErr){
                var field = document.getElementById(strIdField)

                if (field.tagName == "INPUT" || field.tagName == "TEXTAREA"){
                    if (field.type == "text" || field.type == "textarea"){
                        if(stringEmpty(field.value)){
                                $gJQ("#" + strIdLabelErr).show()
                                return false
                        }
                        $gJQ("#" + strIdLabelErr).hide()
                        return true
                    }
                    else if (field.type == "checkbox"){
                        if(!field.checked){
                                $gJQ("#" + strIdLabelErr).show()
                                return false
                        }
                        $gJQ("#" + strIdLabelErr).hide()
                        return true
                    }
                }
                else if (field.tagName == "SELECT"){
                        if (field.multiple !== true){
                                if(field.selectedIndex == 0){
                                        $gJQ("#" + strIdLabelErr).show()
                                        return false
                                }
                                $gJQ("#" + strIdLabelErr).hide()
                                return true
                        }
                        else{
                                field = $gJQ("#" + strIdField).val() || [];
                                if(field.length == 0){
                                        $gJQ("#" + strIdLabelErr).show()
                                        return false
                                }
                                $gJQ("#" + strIdLabelErr).hide()
                                return true
                        }
                }
                return false
        }
        
        function stringEmpty(strValue){
                return strValue.toString().replace(/^\s+/g,'').replace(/\s+$/g,'') == ""
        }
        
        return{
            initialize : initialize,
            bindings : bindings,
            form_onSubmit : form_onSubmit
        }
})();

(function(){
    if(typeof($gJQ) == "undefined"){$gJQ = $}

    $gJQ(document).ready(function(){
        if ($gJQ('.popup-link').length > 0){
            if (typeof($gJQ.fancybox) == "undefined"){
                if (navigator.appName == "Microsoft Internet Explorer") {
                    loadjscssfile("/global/static/css/jquery-fancybox.css", "css");
                    loadjscssfile("/global/static/js/jquery-fancybox.js", "js");
                }
                else {
                    $gJQ("head").append("<style type='text/css'  media='all'>" +
                        "@import '/global/static/css/jquery-fancybox.css';" +
                        "</style>" +
                        "<script language='JavaScript' type='text/javascript' src='/global/static/js/jquery-fancybox.js'></script>");   
                }
            }
            $gJQ(window).load(function() {
                $gJQ('a.popup-link').fancybox({
                    'width': 710,
                    'height': 370,
                    'transitionIn': 'fade',
                    'transitionOut': 'fade',
                    'type': 'iframe',
                    'href': this.href
                });
            });
        }
    });
})();
