/*********
Regex : http://regexlib.com/DisplayPatterns.aspx?cattabindex=6&categoryId=7
**********/
var PSIM = {
    version : "0.1",
    event : null,
    context : {cultureInfo :null}, 
    supportedCulture : {
        en_US : 0,
        fr_CA : 1
    },
       
    addEvent : function(evt){
        this.event = new PSIMEvent(evt); 
        return this.event;
    },
    
    destroyEvent : function(){ this.event = null; },
    hasEvent : function() { return (this.event == null)? false : true; },
    hasCultureInfo : function() { return (this.context.cultureInfo == null)? false : true; }
}


var Class = {
  create: function() {
    return function() { 
      this.initialize.apply(this, arguments);
    }
  }
}
/*****************
psim:Message javascript reference object
*****************/
PSIM.Message = Class.create();
PSIM.Message.prototype = {
    ID : null,
    errorMessageCssClass : null,
    messageCssClass : null,
    initialize : function(id){
        this.ID = id;
    }
}



/****************
Generic functions, variables
*****************/

function addEvent(obj, evType, fn)
{ 
     if (obj.addEventListener)
     { 
       obj.addEventListener(evType, fn, false); 
       return true; 
     } 
     else if (obj.attachEvent)
     { 
       var r = obj.attachEvent("on"+evType, fn); 
       return r; 
     } 
     else 
     { 
       return false; 
     } 
}



function leadZero(v,length) {
	if (typeof(length) != "number") { length = 2; }
	v = new String(v);

	for (var i = v.length; i < length; i++){
		v = "0" + v;
	}
	return v;
}



/*****************
Override for ValidationSummaryOnSubmit 
provided with the ValidationSummary ASP.NET component.
******************/

function ValidationSummaryOnSubmit2(validationGroup){
    if (typeof(PSIM_Message) == "undefined") { return; }

    var ul = document.getElementById(PSIM_Message.ID);
    //remove all client side LI
    var totalLI = ul.childNodes.length;
    
    for(var x = totalLI; x >= 0 ; x = x-1){
        if(typeof(ul.childNodes[x]) != "undefined" && ul.childNodes[x].nodeType == 1 /*&& ul.childNodes[x].className == "clientSide"*/){
            ul.removeChild(ul.childNodes[x]);
        }
    }   
    
    var scroll = false;
    
    for (i = 0; i < Page_Validators.length; i++) {
        if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") 
        {
           var li = document.createElement("li");
           
           var text = document.createTextNode(Page_Validators[i].errormessage);
           
           if (PSIM_Message.errorMessageCssClass != null){
                li.className = PSIM_Message.errorMessageCssClass;
           }
           
           li.appendChild(text);
           ul.appendChild(li);
           scroll = true;
        }
    }
    
    ul.style.display = (ul.childNodes.length > 0 )? "" : "none";    
    
    if(scroll){ window.scrollTo(0,0); }
}



addEvent(window,"load",function (){ 
        if (typeof(ValidationSummaryOnSubmit) == "function"){ ValidationSummaryOnSubmit = ValidationSummaryOnSubmit2 }
    });
    
    
    
function ValidateAndDisableButton(buttonId, validationGroup){
	if (typeof(Page_ClientValidate) == 'function') 	{
		if (Page_ClientValidate(validationGroup))
			DisableButton(buttonId)
	}
	else
		DisableButton(buttonId)
}
    
function DisableButton(objId) {
    window.setTimeout("disableButton('"+objId+"')", 0);
}

function disableButton(objId) {
    document.getElementById(objId).disabled=true;
}

function OpenPopup(URL,target,opener)
{
	var openedWindow = window.open(URL,
									target,
									"directories=0, location=0, menubar=0,resizable=1, scrollbars=0, status=1, titlebar=1, toolbar=0 width=800px, height=400px",
									false);
	openedWindow.opener = opener
	openedWindow.focus();
}

function RefreshParent()
{
	try {
		window.opener.location = window.opener.location;
	}
	catch(err)	{
		// Here, we don't have any other way to test if the opener is open
	}
}