﻿String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function GetFilename(url) 
{ 
   if (url) 
   { 
      var m = url.toString().match(/.*\/(.+?)\./); 
      if (m && m.length > 1) 
      { 
         return m[1]; 
      } 
   } 
   return ""; 
} 


function HTMLEditor_ShowHTML(sender) {
    var previewpannel = $(sender).parent().parent().find("#HTMLEditor_PreviewPanel");
    var editorpannel = $(sender).parent().parent().find("#HTMLEditor_EidtPanel");
    $(sender).addClass("on");
    $($(sender).parent()).find("#Preview").removeClass("on");
	$(editorpannel).show();
	$(previewpannel).hide();
}

function HTMLEditor_ShowPreview(sender) {
    var previewpannel = $(sender).parent().parent().find("#HTMLEditor_PreviewPanel");
    var editorpannel = $(sender).parent().parent().find("#HTMLEditor_EidtPanel");
    $(editorpannel).hide();
    $(previewpannel).html($(editorpannel).find("textarea").val());
   	$(previewpannel).show();
	$(sender).addClass("on", true);
	$($(sender).parent()).find("#Html").removeClass("on");
}


function gotop() {
    if ($.browser.safari) {
        $(document).scrollTop(0);
    }
    else {
        $('html').scrollTop(0);
    }
}
function getCurrentDirectoryOfUrl() {
    var locHref = location.href;
    var locArray = locHref.split("/");
    delete locArray[locArray.length - 1];
    var dirTxt = locArray.join("/");
    return dirTxt;
}

function getRelativePath() {
    return decodeURI(window.location.pathname);
}


function asc2str(str) {
    return String.fromCharCode(str);
}

function UrlDecode(str) {
    var ret = "";
    for (var i = 0; i < str.length; i++) {
        var chr = str.charAt(i);
        if (chr == "+") {
            ret += " ";
        } else if (chr == "%") {
            var asc = str.substring(i + 1, i + 3);
            if (parseInt("0x" + asc) > 0x7f) {
                ret += asc2str(parseInt("0x" + asc + str.substring(i + 4, i + 6)));
                i += 5;
            } else {
                ret += asc2str(parseInt("0x" + asc));
                i += 2;
            }

        } else {
            ret += chr;
        }
    }
    return ret;
}

function insertParam2Url(url, key, value) {
    key = escape(key); value = escape(value);

    var s = url;
    var kvp = key + "=" + value;

    var r = new RegExp("(&|\\?)" + key + "=[^\&]*");

    s = s.replace(r, "$1" + kvp);

    if (!RegExp.$1) { s += (s.indexOf('?') > 0 ? '&' : '?') + kvp; };

    return s;
}

function getTotalHeight() {
    if ($.browser.msie) {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
    } else {
        return self.innerHeight;
    }
}

function getTotalWidth() {
    if ($.browser.msie) {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
    } else {
        return self.innerWidth;
    }
}

var minHeight = 250;


$(document).ready(function () {
    resizeBody();

    var topLinks = $("a[name='gotop']");
    $.each(topLinks, function () {
        $(this).click(function () {
            gotop();
        });
    });
    
    var backLinks = $("a[name='back']");
    $.each(backLinks,function(){
    	$(this).click(function(){history.back();});
    });
    
    var lCaseRelativePath = getRelativePath().trim().toLowerCase();

    var leftNavLinks = $("nav").find("a");
    var directory = lCaseRelativePath.split("/")[2];
    $.each(leftNavLinks, function () {
        var currUrl = $(this).attr("href").trim().toLowerCase();
        if (directory == currUrl.split("/")[2]) {
            $(this).toggleClass("on", true);
        }
    });
    
    // initial job posting
    if($("div#jQueryPlaceholderForJobs")!=null){  
    	$("div#jQueryPlaceholderForJobs").load("/Jobs/getJobPostings.aspx");
    }

    //initial submit form validate
    if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) {
        $('#footerLink').css('position', 'static');
    }
});

$(window).resize(function () {
    resizeBody();
});

function resizeBody() {
    var w = $("#main").width();
    $("article header").width(w - 60);
    $("#footerLink").width(w - 21);

    var h = $("article header").height();
    $("article section").css("padding-top", h + "px");
    
    if($(".scroll").length>0){
    	$("body").css("overflow","hidden");
    	var cur_h = $(".scroll").height();
    	var fixed_h = $("body").height()-440;
    	var min_h = 100;
    	if($(".scroll").css("min-height")!="auto"){    	
    		min_h = parseInt($(".scroll").css("min-height"));    	
    	}
    	
    	if(fixed_h>0 && (fixed_h>=min_h)){
    		if(cur_h != fixed_h){
    			$(".scroll").height(fixed_h);    			
    		}else{
    			$(".scroll").height(fixed_h);
    		}
    		$(".scroll").css("overflow-y","scroll");
    	}else{
    		$(".scroll").css("overflow","");
    	}
    	$(".scroll").css("overflow-x","hidden");
    	
    	if($.browser.mozilla){
    		window.location.href=window.location.href;
    	}
    }

}


function ParamInUrl(name, url) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(url);
    if (results == null)
        return "";
    else
        return results[1];
}

function ParamInDocumentUrl(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function htmlEscape(str) { 
    return String(str) 
            .replace(/&/g, '&amp;') 
            .replace(/"/g, '&quot;') 
            .replace(/'/g, '&#39;') 
            .replace(/</g, '&lt;') 
            .replace(/>/g, '&gt;'); 
} 

function send(id){
	//http%3A%2F%2F
	var contentDiv = $("#"+id);
	var subject = contentDiv.find("[name='title']").attr("title");
	var body = subject + unescape("%0\A%0\D")
			+ contentDiv.find("[name='date']").text() + + unescape("%0\A%0\D")
			+ contentDiv.find("[name='body']").html().replace("<br>",unescape("%0\A%0\D"));
			
	window.location.href = "mailto:?subject="+escape(subject)+"&body="+escape(body);
}

