/*
 * Oasis - general.js
 *
 */

//var journalsToDelete = null;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1)) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1)) ? true : false;

function changeColour(object, colour) {
        object.style.background = colour
}

//used only for forgot username and password, why register links
function submitLoginForm(form, param){
	if(param.length > 0)
	{
		document.forms[form].action = document.forms[form].action + "?"+ param;
	}
	document.forms[form].submit();
}
//CR 22 Rel 1.1 : Function added for displaying current year in Footer
function getCurrYear()
{
var d = new Date();
var lbl=document.getElementById("currDate");
if(ie5)
		lbl.innerText = d.getYear();
	else if(ns5)
		lbl.textContent = d.getYear();

  
}

// CR 24 Rel1.1 : Changes made to the below function for converting JournalLogin 'Journal Code' to lower case
function loginasjournal(){
var i='JN_';
var x= document.journalloginboxform.username1.value;
var y=x.toLowerCase();
document.journalloginboxform.username.value = "";

document.journalloginboxform.username.value=i+y;
//alert(document.journalloginboxform.username.value);
document.journalloginboxform.submit;
}
// CR 30 Rel1.1:Register as new user
function setStatus(s){
	if(s.length > 0)
	{
		var r = '';
		if (s.indexOf('"') == -1) {
			r = s;
		}
		else{
			for (var i = 0; i < s.length; i++) {
				if (s.charAt(i) == '"') {
					r = r + '\\';
				}
				r = r + s.charAt(i);
			}
		}
		window.setTimeout('window.status="' + r + '"', 1);
	}
	else
	{
		window.setTimeout('window.status=" "', 1);
	}
}

function updateJournalsToDelete(jrnlsform, rmform){
	var form = document.forms[jrnlsform];
	var currform = document.forms[rmform];
	var journalsToDelete = "";
	var element;
	
	for(var i = 0; i < form.length; i++) {
		element = form.elements[i];
		if ( element.type == "checkbox" ) {
			if(element.checked) {
			//alert(element.name);
				if(journalsToDelete.length > 0) {
					journalsToDelete += "|";
					journalsToDelete += element.name;
				}
				else{
					journalsToDelete += element.name;
				}
			}
		}
	}
	if(journalsToDelete.length > 0 ){
		document.forms[rmform].journalstodelete.value = journalsToDelete;
		var confirmRem = confirm("Are you sure you want to remove the selected journal(s)?");
		if(confirmRem){		
			return true;
		}else{
			return false;
		}
	}
	else {
		alert("No journals to remove");
		return false;
	}
}

function doPagination(form, action){

	var form = document.forms[form];
	//alert("Form "+form.name +"Action "+ action +"page number "+ form.page.value);
	var vpage = form.page.value;
	var pageNo = parseInt(vpage);
	
	if(action == "prev"){
		pageNo = pageNo - 1;
	}
	else if(action == "next"){
		pageNo = pageNo + 1;
	}
	form.page.value = pageNo;
	form.v_action.value = action;
	//alert("action"+action);
	//alert(form.page.value);
	form.submit();
}


// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {
    
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // verify that they are numbers and in the right range.
    // If the element has a "numeric" property defined, verify that
    // it is a number, but don't check its range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
    var e = f.elements[i];
    if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
        // first check if the field is empty
        if ((e.value == null) || (e.value == "") || isblank(e.value)) {
        empty_fields += "\n      " + e.label;
        continue;
        }

        // Now check for fields that are supposed to be numeric.
        if (e.numeric || (e.min != null) || (e.max != null)) {
        var v = parseFloat(e.value);
        if (isNaN(v) ||
            ((e.min != null) && (v < e.min)) ||
            ((e.max != null) && (v > e.max))) {
            errors += "- The field " + e.name + " must be a number";
            if (e.min != null)
            errors += " that is greater than " + e.min;
            if (e.max != null && e.min != null)
            errors += " and less than " + e.max;
            else if (e.max != null)
            errors += " that is less than " + e.max;
            errors += ".\n";
        }
        }
    }
    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted.
    // Otherwise return true.
    if (!empty_fields && !errors) return true;
    msg = ""; 
    /*msg  = "______________________________________________________\n\n";
    msg += "Your request was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n";

    if (empty_fields) {
    msg += "The following required field(s) are empty:"
        + empty_fields + "\n";
    if (errors) msg += "\n";
    }*/
    
     msg += errors;
    //alert(msg);
    return false;
}


function validateAndSubmit(f){
//alert("in validate and submit");
	var isError = false;
	if ( !verify(f) ) {
		isError = true;
	}
	
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	
	//If any error, reset the form values for GET request 
	if(isError){
		f.isError.value = isError;
		f.postName.value = "";
		f.guid.value = "";
		f.refPage.value = "";
		f.method = "get";
		f.action = sPage;
		f.submit();
		return false;
	}
	else{
		return true;
	}
}

function check(f,chk)
{
	//alert(f.name);
	if(chk)
	{
		//alert('checked yes');
		document.getElementById("alertCheckedov").value='Y';
	}
	else{
		//alert('no');
		document.getElementById("alertCheckedov").value='';
	}
	document.getElementById("UpdateAlert").value = "true";
	var updateCnt = new Number(f.adUpdCnt.value);
	f.adUpdCnt.value = updateCnt + 1;
	
	//alert('Updatecnt'+f.adUpdCnt.value);
	
	f.submit();
}


function CheckAlertForArticle(cbName, isAlertReqd, formobj, aid)
{
	//alert(cbName +"---"+ isAlertReqd+"---"+ formobj.name+"---"+formobj.alertCheckedov.value);
	if(isAlertReqd)
	{
		formobj.alertCheckedov.value='Y';
	}
	else
	{
		formobj.alertCheckedov.value='';
	}
	
	formobj.alertAid.value = aid;
	formobj.UpdateAlert.value = "true";
	//alert(formobj.alertCheckedov.value +"--"+formobj.alertAid.value +"---"+ formobj.UpdateAlert.value);
	formobj.submit();
}


function allAlerts(formtosubmit, flag) {
	//alert(formtosubmit +"---"+ flag);
	//alert(isLastPage);
	var form = document.forms[formtosubmit];
	//alert(form.name);
	form.allAlerts.value = flag;
	//alert(form.allAlerts.value);
	form.submit();
	
}


