﻿// For changeManage.aspx
function mySubmitManagerReview(rFlag,pFlag,dID){
    document.form1.reviewFlag.value = rFlag;
    if (rFlag==0) {
        if (document.form1.comments.value=="") {
            alert("Please enter the comments for rejection.");
            document.form1.comments.focus();
            return;
        }
    }
    mySubmitManager(pFlag,dID); 
}
function mySubmitManager(pFlag,lFlag){
    if (pFlag==21) {
        if(! confirm("This will create a new duplicate draft proposed change.\nAre you sure that you want to continue?") ){return;}
    }
    document.form1.processFlag.value=pFlag;
    if (pFlag==1) {
        document.form1.listAllFlag.value=lFlag;
    } else {
        document.form1.dID.value=lFlag;
    }
    document.form1.submit();
}
function mySubmitSort(sFlag,pFlag){
    if (! document.form1.processFlag){
        myAlertLoading();
        return;
    }
    document.form1.sortFlag.value=sFlag;
    document.form1.processFlag.value=pFlag;
    document.form1.submit();
}
function copyDescData(form){
    if (form.myDescTech.value==form.myDescUser.value) {return; }
    if (form.myDescTech.value=="" || confirm("Are you sure that you want to replace the technical description with user description?")) {
        form.myDescTech.value=form.myDescUser.value;
    }
}
//  for plannedChangeView.aspx
function mySubmitView(pFlag,dID){
    if (! document.form1.processFlag){
        myAlertLoading();
        return;
    }
    document.form1.processFlag.value=pFlag;
    if (pFlag==0) {
        document.form1.listAllFlag.value=dID;
    } else {
        document.form1.dID.value=dID;
    }
    document.form1.submit();
}
function myAlertLoading(){
    //var loadMsg = "Page is still loading. Please wait a moment.";
    //alert(loadMsg);
}
function mySubmitSearch(sFlag) {
    if (! document.form1.processFlag){
        myAlertLoading();
        return;
    }
    document.form1.processFlag.value=0;
    document.form1.searchFlag.value=(sFlag+1)%2;
    document.form1.submit();
}

//// For changeEdit.aspx
var newWin;
function validItCE(form) {
    if (form.processFlag.value>1) {
        
        if (form.myTitle.value=="") {
            alert("Please enter the title.");
            form.myTitle.focus();
            return false;
        }
        if (form.myCategory.value=="") {
            alert("Please select the category.");
            document.getElementById("b1").focus();
            return false;
        }
        if (form.myDescUser.value=="") {
            alert("Please enter the user description.");
            form.myDescUser.focus();
            return false;
        }
        if (form.myIntnlDoc.value=="") {
            alert("Please enter the information of the backout procedure.");
            form.myIntnlDoc.focus();
            return false;
        }
        
        if (form.myExtraEmails.value!="") {
            var i;
            var aryEmail = form.myExtraEmails.value.split(",");
            var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            for (i=0 ;i<aryEmail.length;i++) {
                if (! filter.test(aryEmail[i])){
			        alert("Please enter a valid email address.("+(i+1)+":"+aryEmail[i]+")");
			        form.myExtraEmails.focus();
			        return false;
	        	}
            }
        }
        
        coordinateDate(form);          
        
    }
    return true;
}

function owin(URL,w,h) {
	var WinWidth = parseInt(w);
	var WinHeight = parseInt(h);
	var WinLeft = screen.width - WinWidth - 10;
	var WinTop = (screen.height - WinHeight)/2;
	var WinProperties = "height=" + WinHeight + ",width=" + WinWidth + ",top=" + WinTop + ",left=" + WinLeft + ",scrollbars=yes,resizable=yes,status=no,menubar=no,toolbar=no,location=no";
	newWin = window.open(URL,"newWin",WinProperties);
	newWin.focus();
}
function coordinateDate(form){
    if (form.myBDate.value != "" ) {
        var flag = false;
        if ( form.myEDate.value == "" ) {
            flag =  true;            
        } else {
            var dt1 = new Date(form.myBDate.value);
            var dt2 = new Date(form.myEDate.value);
            if (dt1>dt2) {
                flag =  true;
            }
        }
        if ( flag ) {
            form.myEDate.value = form.myBDate.value;
            form.myEHH.SelectedIndex = form.myBHH.SelectedIndex;
            form.myEMM.SelectedIndex = form.myBMM.SelectedIndex;
            form.myEAP.SelectedIndex = form.myBAP.SelectedIndex;
        }
    }    
    return
}

// Script obtained from http://www.felgall.com/jstip45.htm
// This function will automatically add rows to a textarea as
// the user fills the textarea with content.
function resize (t) {
    a = t.value.split('\n');
    b = 1;
    for (x=0;x < a.length; x++) {
        if (a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
    }
    b+= a.length;
    if (b > t.rows) t.rows = b;
}


