var any_val_changed = false;
var save_changes = false;

var mytime, Myimg;
var photo;

function test()
{
	photo = "Popup.asp?"+photo;
    window.open(photo,'homepagephoto','width='+Myimg.width+',height='+(Myimg.height+27));
	Myimg=null;
}

function MainObjClick(Param)
{
	document.forms['form1'].OP_Action.value=Param;
	document.forms['form1'].submit();
}

function Popup3( id )
{
	window.open('Image.asp?id='+id,'Image', 'width=700, height=400');
}

function Popup2( id )
{
	window.open('BindInfo.asp?id='+id,'Image', 'width=700, height=400');
}


function popup(myphoto)
{
    photo=''+myphoto;
    Myimg=new Image();
    mytime=new Date();
	Myimg.onload=test;
    Myimg.src=myphoto+'?'+mytime.getTime();
}

function CheckIt(Par, ND){
	if (Par.value==1)
		{
		Par.value=0;
		document.forms['form1'].ApprovedDate.value="";
		}
	else
		{Par.value=1;
		document.forms['form1'].ApprovedDate.value=ND.value;
		}
}

function AddItem(Table)
{
var wnd = window.open(	"AddItem.asp?Table="+Table, "_blank",
							"width=600,height=200,status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1");
if (wnd) wnd.moveTo(AvWidth/2-450/2, AvHeight/2-400/2);

}
function Attaching(id,fid)
{
var wnd = window.open(	"AttachImg.asp?id="+id+"&filmid="+fid, "_blank",
							"width=400,height=400,status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1");
if (wnd) wnd.moveTo(AvWidth/2-450/2, AvHeight/2-400/2);
self.close();
}
function Attach(id)
{
var wnd = window.open(	"ImgList.asp?id="+id, "_blank",
							"width=400,height=400,status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1");
if (wnd) wnd.moveTo(AvWidth/2-450/2, AvHeight/2-400/2);
}

function Detach(id)
{
var wnd = window.open(	"DetachImg.asp?ID="+id, "_blank",
							"width=600,height=200,status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1");
if (wnd) wnd.moveTo(AvWidth/2-450/2, AvHeight/2-400/2);
}

function UploadPhoto()
{
var wnd = window.open(	"uploadform.asp", "_blank",
							"width=550,height=200,status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1");
if (wnd) wnd.moveTo(AvWidth/2-450/2, AvHeight/2-400/2);
}
function ClosePhoto(Name)
{
self.opener.document.form1.elements['ImageName'].value=Name;
self.close();
}

function CloseItem(Table)
{
var a='Avail'+Table;
if (document.form1.elements['Id'].value != "") {
	var oOption = self.opener.document.createElement("OPTION");
	if (Table=="Director") {
		oOption.text = document.form1.elements['DirectorFirstName'].value+' '+document.form1.elements['DirectorLastName'].value;
		}
	else
		{
		oOption.text = document.form1.elements[Table].value;
		}
		oOption.value = document.form1.elements['Id'].value;
	self.opener.document.form1.elements[a].add(oOption);
	SortList(self.opener.document.form1.elements[a]);
}
self.close();
}

////////////////////////////////////////////////////////////////////////
function MakeLists() {
MakeList(document.forms['form1'].CurrGenre);
MakeList(document.forms['form1'].CurrTheme);
}
function MakeList(optsv) {
	opts = optsv;
	var a=opts.name+'dumm';
	document.form1.elements[a].value = "";
	for(i=0; i<opts.length; i++) {
		document.form1.elements[a].value += "," + opts[i].value;
	}
	if (document.form1.elements[a].value != "")
		document.form1.elements[a].value = document.form1.elements[a].value.substr(1);
	
//	alert(document.form1.elements[a].value);
}

function SortList(optsv) {
	var opts = optsv;
	for(i=0; i<opts.length; i++) {
		var bOrdered = true;
		for(j=0; j<opts.length-1; j++)
			if (opts[j].text > opts[j+1].text) {
				tmpTxt = opts[j].text;
				opts[j].text = opts[j+1].text;
				opts[j+1].text = tmpTxt;
				tmpVal =  opts[j].value;
				opts[j].value = opts[j+1].value;
				opts[j+1].value = tmpVal;
				bOrdered = false;
			}
		if (bOrdered)
			break;
	}
}

function moveitem(src, dst, m) {
	var ar = new Array();
	var j=0;
	for(i=0; i<src.options.length; i++) {
		var srcOpt = src.options[i];
		if (srcOpt.selected) {
			var oOption = document.createElement("OPTION");
			oOption.text = srcOpt.text;
			oOption.value = srcOpt.value;


			try {
			  dst.add(oOption,null);
			}
// ... And if that doesn't work use the IE-only method
			catch (e) {
			 dst.add(oOption,dst.length);
			}

			ar[j++] = i;
		}
	}
	j=j-1;
	for(i=j; i>-1; i--) {
		src.options[ar[i]] = null;
	}
	SortList(src);
	SortList(dst);
	if (m==1) {
		MakeList(src)
	}
	else
	{
		MakeList(dst)
	}
}

////////////////////////////////////////////////////////////////////////

function add_ID(divName, id, params) {
	if(params != null && params != "")
		params = id + "&amp;" + params;
	else
		params = id;
	var ret = genDIV(id, params);
	if(ret[0]) {
		if(document.all.dmbMenu.length)
			var obj = document.all.dmbMenu[0];
		else
			var obj = document.all.dmbMenu;
		var width = obj.clientWidth;
		var height = obj.clientHeight;
		document.all[divName].outerHTML = ret[0];
		document.all[divName].style.width = width;
		document.all[divName].style.height = height;
	}
	if(ret[1]) {
		var obj = document.all.dmbMenu[1];
		var width = obj.clientWidth;
		var height = obj.clientHeight;
		document.all[divName + "_info"].outerHTML = ret[1];
		document.all[divName + "_info"].style.width = width;
		document.all[divName + "_info"].style.height = height;
	}
}

function ChkPrvWnd() {
	if(document.forms[0].Id.value != "")
		if(!isValidAll()) value_change();
}

function AreYouSure(formName, formAction) {
	if(!document.forms[formName])
		return;
	if(formAction == null)
		formAction = document.forms[formName].action;
	if((!save_changes) && any_val_changed && confirm("Save changes")) {
		if(isValidAll(true)) {
			_isValidAll = true;
			sav = window.open("", "save_win", "width=140,height=10,scrollbars=no,status=no,toolbar=no,menubar=no");
			sav.document.writeln("<HTML><BODY><h1>Saving...</h1><form name='autoForm' method='post' action='" + formAction + "'>");
		} else {
			_isValidAll = false;
			sav = window.open("", "_blank", "width=700,height=500,scrollbars=1,status=0,toolbar=0,menubar=1,resizable=1");
//			sav = window.open(m+id, name,"width="+w+",height="+h+",status=0,toolbar=0,menubar=1,scrollbars=1,resizable=1");
			sav.document.writeln("<HTML><BODY><form name='autoForm' method='post' action='" + formAction + "'>");
		}

		var el = document.forms[formName].elements;

		for(i = 0; i < el.length; i++)
			if(el[i].name != "" && ((el[i].type == "checkbox" && el[i].checked) || el[i].type != "checkbox"))
				sav.document.writeln("<input type=hidden name='" + el[i].name + el[i].id + "'>");
		if(_isValidAll)
			sav.document.writeln("<input type=hidden name=\"save_only\" value=\"1\">");
		else
			sav.document.writeln("<input type=hidden name=\"invalid_save\" value=\"1\">");
		sav.document.writeln("</form></BODY></HTML>");
		sav.document.close();
		sav.opener = self.opener;

		for(i = j = 0; i < el.length; i++)
			if(el[i].name != "" && ((el[i].type == "checkbox" && el[i].checked) || el[i].type != "checkbox")) {
				if(el[i].name + el[i].id != sav.document.autoForm.elements[j].name)
					alert("Possible an error has happened");
				sav.document.autoForm.elements[j++].value = el[i].value;
			}
		sav.document.autoForm.submit();
	}

}

function value_change() {
	any_val_changed = true;
}

function add_spec(mStr) {
	var cur_pos = 0, to_pos;

	while((to_pos = mStr.indexOf('&', cur_pos)) != -1)
		mStr = mStr.substring(0, to_pos) + '&amp;' + mStr.substring(cur_pos = to_pos + 1, mStr.length);
	return mStr;
}

var AvHeight = screen.availHeight-50;
var AvWidth = screen.availWidth;

function setFocusFirst() {
  setFocusPosition(1);
}


function setFocusPosition(pos)
{
    var t,i,doc,r,p,fn,ps;
	ps=1;

    for(fn=0;fn<document.forms.length;fn++)
    {
        doc=document.forms[fn].elements;
        for(r=-1,p=0,i=0;i<doc.length;i++)
        {
            t=doc[i].type.toUpperCase();
            if((t=="TEXT")||(t=="TEXTAREA")||(t=="PASSWORD"))
            {
                if( r < 0 )
                	r=i;

                if(doc[i].value=="")
                {
                    doc[i].focus();
                    return;
                }
    		    if(ps==pos)
                {
					doc[i].select();
					return;
				}
                if(++p>2&r>=0)
                {
                    doc[r].focus();
                    return;
                }
				ps+=1;
            }
        }
    }
}

function OpenWindowXY(name, id, context, prefix, w, h)
{
    if (w == null) w = AvWidth*7/8;
    if (h == null) h = AvHeight*7/8;
    var m = name+".asp?";
    if (id != "") id = "id="+id;
    context="context="+context;
    if (prefix != "") prefix="prefix="+prefix;
    i = name.lastIndexOf("/");
    if (i != -1)
		name = name.substr(i+1);
	name = ReplaceCharacter(name, "/", "_");
    if(self.name != "") name += "_"+self.name;
    var wnd = window.open(m+id+"&"+context+"&"+prefix, name,
        "width="+w+",height="+h+",status=0,toolbar=0,menubar=1,scrollbars=1,resizable=1");
    if (wnd) wnd.moveTo(AvWidth/2-w/2, AvHeight/2-h/2);
    return wnd;
}

/*********
function OpenWindowXY(name,id,w,h)
{
	var m = main==""?name+".Asp?":"Main.Asp?Obj="+name+"&";
	var wnd = window.open(m+id, name,
		"width="+w+",height="+h+",status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1");
}
**********/

function OpenWnd(name, w, h) {
    if (w == null) w = AvWidth*7/8;
    if (h == null) h = AvHeight*7/8;
	var wnd = window.open(name, name.substring(0, name.indexOf('.', 0)),
		"width="+w+",height="+h+",status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1");
}

function OpenWindow(name) {
	OpenWindowXY(name,"",700,500,"main");
}

function OpenWindow1(name) {
	OpenWindowXY(name,"",700,500,"");
}

function OpenWindowPar1(name, id) {
	OpenWindowXY(name,id,700,500,"");
}

function isValidTxt(FldId, FldName, noAlert) {
	spaces=Replace(FldId.value, " ", "");
	if (FldId.value == "" || spaces == "") {
		if(noAlert == false)
			alert("Please enter the "+FldName);
		FldId.focus();
		return false;
	}
	return true;
}

function checkOnFuture(Day, Month, Year, currentDay, currentMonth, currentYear, noAlert, FldName) {
	if(Year < currentYear) return true;
	if(Year==currentYear) {
		if(Month < currentMonth) return true;
		if(Month == currentMonth)
			if(Day<=currentDay) return true;
	}
	if(noAlert == false)
		alert("You enter future Date. Please enter real Date");
	return false;
}
function MakeADMFields(currentDay,currentMonth,currentYear) {
	document.forms['form1'].DateofBirth.value = document.forms['form1'].Day.value+"/"+document.forms['form1'].Month.value+"/"+document.forms['form1'].Year.value;
	MakeTerms();
	return true;
}

function checkAdminReview() {
	if (document.forms['AddScore'].UseNewCritic.value == 1 )
		{
		if ( document.forms['AddScore'].NewCriticName.value == "")
		{
			alert("Enter new ScoreOrg Name");
			document.forms['AddScore'].NewCriticName.focus();
			return false;
		}else if (document.forms['AddScore'].NewCriticName.value.toLowerCase() == "tookey"){
			
			alert("Please enter tookey score on film page.");
			document.forms['AddScore'].NewCriticName.focus();
			return false;
		}
	}
return true;
}

function MakeTerms() {
document.forms['form1'].Terms.value =document.forms['form1'].terms1.value+','+document.forms['form1'].terms2.value+','+document.forms['form1'].terms3.value+','+document.forms['form1'].terms4.value+','+document.forms['form1'].terms5.value+','+document.forms['form1'].terms6.value+','+document.forms['form1'].terms7.value;
return true;
}
function GenderClick(Par) {
    if (Par){
		document.forms['form1'].Gender.value = Par;
	}
	else{
		document.forms['form1'].Gender.value = Par;
	}
return true;

}
function isValidDate(FldValue, FldName, noAlert, required, notallowFuture, currentDay, currentMonth, currentYear) {
	//return true;
	var initStr = new String(FldValue);
	initStr = Replace(initStr, " ", "");
	FldName.value = initStr;
	var timeStr = initStr.substr(10, 10)
	var date_string=FldValue;
	var new_date_str;
	var Day = "0"+document.forms['form1'].Day.value;
	var Month = document.forms['form1'].Month.value;
	var Year = document.forms['form1'].Year.value;
	var checkYear = parseInt(Year, 10);
	if(checkYear<1800 || checkYear>2200) {
		if(noAlert == false)
			alert("Please enter real Date");
		return false;
	}
	if(notallowFuture != null) {
		res = checkOnFuture(Day, Month, Year, currentDay, currentMonth, currentYear, noAlert, FldName);
		if (!res) return false;
	}
	if(Month != "" && Day != "" && Year != "") { 
		FldName.value=Day+"/"+Month+"/"+Year;
	}
	if(timeStr != "")
		var val = new Date(parseInt(Year,10), parseInt(Month,10)-1, parseInt(Day,10), parseInt(Hours, 10), parseInt(Minutes, 10), 00);
	else
		var val = new Date(parseInt(Year,10), parseInt(Month,10)-1, parseInt(Day,10));
	
	if(FldValue != "") {
		if(parseInt(val.getDate()) != parseInt(Day, 10) || parseInt(val.getMonth()) != parseInt(Month, 10)-1 || parseInt(val.getFullYear()) != parseInt(Year, 10)) {
			if(noAlert == false)
				alert("Please enter correct value for Date fields");
			return false;
		}
	}
	return true;
}

function isValidNum(FldId, FldName, noAlert, notallowFuture, now, required) {
	var temp=parseInt(FldId.value);
	if (isNaN(temp) && required) {
		if(noAlert == false)
			alert("Please enter the "+FldName);
		FldId.select();
		if (FldId.type != "hidden")
			FldId.focus();
		return false;
	}
	if ((FldId.value != ""+temp && FldId.value != "") || temp<0) {
		if(noAlert == false) 
			alert("Please enter correct value for "+FldName);
			FldId.select();
		if (FldId.type != "hidden")
				FldId.focus();
		return false;
	}
	if(notallowFuture != null && notallowFuture != "") {
		str = new String(now);
		currentYear = str.substr(6, 4);
		year=parseInt(FldId.value);
		if(year<1800 || year>currentYear) {
		if (year==0) return true;
		if(noAlert == false)
			alert("Please enter real Year in "+FldName);
			FldId.select();
			if (FldId.type != "hidden")
				FldId.focus();
			return false;
		}
	}
	return true;
}
function isValidTwoDates(FirstFldId, SecondFldId, f_FldName, s_FldName, noAlert, f_required, s_required, notallowFuture, now) {
	if(!isValidDate(FirstFldId, f_FldName, noAlert, f_required, notallowFuture, now) || !isValidDate(SecondFldId, s_FldName, noAlert, s_required, notallowFuture, now)) 
		return false;
	FirstDate = new Date(FirstFldId.value);
	SecondDate = new Date(SecondFldId.value);

	if(FirstDate.getFullYear() > SecondDate.getFullYear()
    || FirstDate.getFullYear() == SecondDate.getFullYear()
    	&& FirstDate.getMonth() > SecondDate.getMonth()
    || FirstDate.getFullYear() == SecondDate.getFullYear()
    	&& FirstDate.getMonth() == SecondDate.getMonth()
        && FirstDate.getDate() > SecondDate.getDate())
    {
		if(noAlert == false)
			alert("End date can't be less then Start date. Check Year");
		return false;
	}
	return true;
}
function isValidTwoSel(FirstFldId, SecondFldId, noAlert) {
	if(FirstFldId.value==SecondFldId.value) {
		if(noAlert == false)
			alert("Teams have to be different! Please select different teams in match");
		FirstFldId.focus();
		return false;		
	}
	return true;
}
function isValidSel(FldId, FldName, noAlert, notrequired) {
	var res = eval(FldId.value==""?"0":FldId.value);
	if (res <= 1 && !notrequired) {
		if(noAlert == false)
			alert("Please select the "+FldName);
		FldId.focus();
		return false;
	}
	return true;
}
function updateImageData(act) {
	if (document.forms['form1'].ImageName.value=="") {
		document.forms['form1'].ImageName.value='nopic.gif';
	}
		document.forms['form1'].OP_Action.value = act;
		document.forms['form1'].submit();
}

function updateData(act) {
		document.forms['form1'].OP_Action.value = act;
		//alert("update " + act)
		document.forms['form1'].submit();
}

function saveAsNew(act) {

	document.forms['form1'].Id.value = "";
	updateData(act);
}

function CloseMTR() {
	if (self.opener)
		self.opener.location.href = self.opener.location.href;
	self.close();
}

function OpenSelectForEdit(WindowName, obj) {
	OpenWindowPar(WindowName, "Id="+((obj.value == -1) ? "" : obj.value));
}

function OpenSelectForEdit1(WindowName, obj, addPar) {
	OpenWindowPar1(WindowName, "Id="+((obj.value == -1) ? "" : obj.value) + "&" + (addPar==null?"":addPar));
}

function WinSelImage(SubDir) {
	window.open("WinSelImage.asp?SubDir="+SubDir,"Images");
}

function TakeImage(SubDir,SrcName) {
	var Obj, RelativePath = SrcName;
	Obj=document.forms[0]["img"+SubDir];
	n = SrcName.indexOf("/", 7);
	if (n != -1)
		RelativePath = SrcName.substring(n, SrcName.length);
	if (Obj) Obj.value = RelativePath;
	Obj = document.images["src"+SubDir];
	if (Obj) Obj.src = SrcName;
}

function Trim(s) {
	while(s.charAt(s.length-1)==' ')
		s = s.substring(0, s.length-2);
	return s;
}

function GetCurSelText(obj) {
	curVal = obj.value;
	len = obj.children.length;
	for(i=0; i!=len; i++) {
		if (obj.children[i].value == curVal)
			return obj.children[i].text;
	}
	return "Error!!!";
}

function updateVal(Obj, CheckField) {
	if(Obj.checked) CheckField.value=1
	else CheckField.value=0
	//var ov = parseInt(Obj.value);
	//var hv = parseInt(HidField.value==""?"0":HidField.value);
	//if (Obj.checked) HidField.value = hv+ov; else HidField.value = hv-ov;
}
function updateStrVal(Obj, Field) {
	Field.value = Obj.value;
	//var	ov = parseInt(Obj.value);
	//var	hv = parseInt(HidField.value==""?"0":HidField.value);
	//if (Obj.checked) HidField.value = hv+ov; else HidField.value = hv-ov;
}

function OpenDeleteWindow(Id, Table, act) {
	document.forms['form1'].OP_Action.value = act;
	document.forms['form1'].submit();
}

function QueryWithout( str, sLookFor ) {
    var result = "" + str + "&";
    var i;
    do {
		var i = result.indexOf(sLookFor, 0);
		if (i > 0  &&  (result.charAt(i-1)=='?'  ||  result.charAt(i-1)=='&')) {
			var sLeft = result.substr(0, i);
			var sRight = result.substr(i);
            var j = sRight.indexOf("&", 0);
            result = sLeft + sRight.substr(i);
		}
	} while(i > 0);
	if (result.substr(result.length-1) == '&')
		result = result.substr(0, result.length-1);

	return result;
}

function checkclick(par) {
	if (par.value==1){
		par.value=0;
	}
	else
	{
		par.value=1;
	}
}

function Mcheckclick(par,par2) {
	if (par.value!=0){
		par.value=0;
	}
	else
	{
		par.value=par2;
	}
}

function Replace(str, sFnd, sRep) {
	var tmp = ""+str;
	var i=0, len = tmp.length;

	for(; i<len; i++)
    {
		if (tmp.charAt(i) == sFnd) {
			tmp = tmp.substr(0, i) + sRep + tmp.substr(i+1);
			if (sRep=="") i = i-1;
		}
	}
	return tmp;
}
function ReplaceCharacter(str, sFnd, sRep) {
    var tmp = ""+str;
    var i=0, l = tmp.length;

    for(; i<l; i++)
        if (tmp.charAt(i) == sFnd)
            tmp = tmp.substr(0, i) + sRep + tmp.substr(i+1);
    return tmp;
}