﻿// Modal Windows Script File
// Author: Mohammad Tajari
// Date: Sep 26, 2005

var DialogWidth = "750px";
var DialogHeight = "550px";

function Modal(url, width, height) {
    var theWidth = IsNull(width) ? DialogWidth : width;
    var theHeight = IsNull(height) ? DialogHeight : height;
    var returns = self.showModalDialog(root + LanguageBasedURL(url), "", "dialogWidth:" + theWidth + ";dialogHeight:" + theHeight + ";help:no;scroll:no;resizable:yes;status:yes");
	return IsNull(returns) ? "" : returns;
}

function Open(url, width, height) {
    var theWidth = IsNull(width) ? DialogWidth : width;
    var theHeight = IsNull(height) ? DialogHeight : height;
    self.open(root + LanguageBasedURL(url), "", "width="+theWidth+",height="+theHeight+",resizable=no,status=no,toolbar=no,menubar=no,location=no");
}

function LanguageBasedURL(url) {
    var lang = new QueryString().Get("lang");
    var uri = url.split("?");
    var languageBasedUrl = uri[0];
    languageBasedUrl += "?lang=" + lang;
    languageBasedUrl += uri.length > 1 ? "&" + uri[1] : "";
    return languageBasedUrl;
}

function Resize(){
    if (self.dialogWidth == DialogWidth && self.dialogHeight == DialogHeight){
        self.dialogWidth = self.screen.availWidth;
        self.dialogHeight = self.screen.availHeight;
    }
    else{
        self.dialogWidth = DialogWidth;
        self.dialogHeight = DialogHeight;
        self.dialogTop = (self.screen.availHeight - parseInt(self.dialogHeight)) / 2;
        self.dialogLeft = (self.screen.availWidth - parseInt(self.dialogWidth)) / 2;
    }
}

function IsNull(obj) {
    return typeof (obj) == "undefined";
}

function Wait(){
    document.body.style.cursor = "wait";
}

function OnHelp(url){
	self.showHelp(url);
}

function OnMessage(persists){
	with (document.forms[0]){
		if (Message.value != ""){
			alert(Message.value);
			if (!persists){
				Message.value = "";
			}
		}
	}
}

function ItemDeleteConfirm(type){
	type = type == null ? "مورد" : type;
	return self.confirm("آیا مطمئن هستید که می خواهید این " + type + " را حذف کنید؟");
}

function ImageDeleteConfirm(){
	return self.confirm("آیا مطمئن هستید که می خواهید این تصویر را حذف کنید؟");
}

function ShowModal(HeightMargin, PreviewPage ,ImageURL){
	var obj = new Object();
	obj.Image = new eImage(ImageURL);
	obj.Screen = new Object();
	obj.Screen.Width = self.screen.availWidth;
	obj.Screen.Height = self.screen.availHeight - HeightMargin;
	if (obj.Image.width < obj.Screen.Width && obj.Image.height < obj.Screen.Height){
		obj.Width = obj.Image.width;
		obj.Height = obj.Image.height;
	}
	else if (obj.Image.width / obj.Screen.Width >= obj.Image.height / obj.Screen.Height){
		obj.Height = obj.Image.height / (obj.Image.width / obj.Screen.Width);
		obj.Width = obj.Screen.Width;
	}
	else{
		obj.Width = obj.Image.width / (obj.Image.height / obj.Screen.Height);
		obj.Height = obj.Screen.Height;
	}
	return self.showModalDialog(PreviewPage, obj, "status:no;dialogWidth:"+obj.Width+"px;dialogHeight:"+(obj.Height+HeightMargin)+"px;help:no;scroll:no");
}

function eImage(ImageURL){
	var img = new Image();
	img.src = ImageURL;
	return img;
}

function DoPreLoadImages() {
	if (!IsNull(PreLoadImages)){
		for (i=0 ; i < PreLoadImages.length ; i++){
			new eImage(PreLoadImages[i]);
		}
	}
}

function SwapStatus(Item, persist){
    var StatusItem = document.getElementById(Item);
    StatusItem.style.display = StatusItem.style.display == "none" ? "block" : "none";
    var HiddenField = Item + "HiddenField";
    if (persist){
        try{
            document.getElementById(HiddenField).value = StatusItem.style.display;
        }catch(e){
            alert("Please register " + HiddenField);
        }
    }
}

function GetStatus(Item){
    var StatusHiddenField = document.getElementById(Item + "HiddenField");
    return StatusHiddenField.value;
}

function InitializeStatus(Item){
    document.getElementById(Item).style.display = document.getElementById(Item + "HiddenField").value;
}

function GetValue(Item){
    return document.getElementById(Item).value;
}

function Mask(TextBox,Location,Delimiter){
    var Locations = Location.split(',');
    for (var i = 0; i <= Locations.length; i++){
        for (var j = 0 ; j <= TextBox.value.length ; j++){
            if (j == Locations[i]){
                if (TextBox.value.substring(j, j+1) != Delimiter){
                    TextBox.value = TextBox.value.substring(0,j) + Delimiter + TextBox.value.substring(j,TextBox.value.length)
                }
            }
        }
    }
}

function ChooseActivity(CodeObjectID, TypeObjectID){
    var returned = Modal('/modal/activity.aspx?lang=fa-ir');
    if (!IsNull(returned)){
        var returns = returned.split(",");
        document.getElementById(CodeObjectID).value = returns[0];
        document.getElementById(TypeObjectID).value = returns[1];
    }
}

function SearchActivity(Type, CodeObjectID){
    var returned = Modal('/modal/activitysearch.aspx?lang=fa-ir&type=' + Type);
    if (!IsNull(returned)){
        document.getElementById(CodeObjectID).value = returned;
    }
}

function QueryString(qs) {
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); 
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

QueryString.prototype.Get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_ != null ? default_ : "";
}

QueryString.prototype.Contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

