//link buttons to URL
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}


function expandShort (id) {
document.getElementById(id + "short").style.display = "inline";
document.getElementById(id + "long").style.display = "none";
return false;
}
function expandLong (id) {
document.getElementById(id + "short").style.display = "none";
document.getElementById(id + "long").style.display = "inline";
return false;
}


function login(formName) {
var username=trim(document.forms[formName].j_username.value);
var pwd=trim(document.forms[formName].j_password.value);
	if(username=='' || username=='Username') {
		alert("Please enter Username");
		return false;
	}
	else if(pwd=='' || pwd=='Password'){
		alert("Please enter Password");
		return false;
	}
	document.forms[formName].j_username.value=username;
	document.forms[formName].j_password.value=pwd;
	return true;
	}
function trim(sString)
{
	while (sString.substring(0,1) == ' ')
{
	sString = sString.substring(1, sString.length);
}
	while (sString.substring(sString.length-1, sString.length) == ' ')
{
	sString = sString.substring(0,sString.length-1);
}
	return sString;
}

 function Next()
 {
if(document.forms[2] != null && document.forms[2].currentPage!=null){
	document.forms[2].navigate.value='N';
	document.forms[2].submit();
  }else if(document.forms[1] != null && document.forms[1].currentPage!=null){
   	document.forms[1].navigate.value='N';
 	document.forms[1].submit();
  }	else {
    document.forms[0].navigate.value='N';
  	document.forms[0].submit();
  }
  	
 }
 
function Previous()
{
   if(document.forms[2] != null && document.forms[2].currentPage!=null){
    document.forms[2].navigate.value='P';
   	document.forms[2].submit();
  }else if(document.forms[1] != null && document.forms[1].currentPage!=null){
    document.forms[1].navigate.value='P';
   	document.forms[1].submit();
}else{
    document.forms[0].navigate.value='P';
   	document.forms[0].submit();
  }  
  
 }

function doNext(methodName){
 
if(methodName==null){
 Next();
 }else{
if(document.forms[1].currentPage==null){
 	document.forms[2].method.value = methodName;
    document.forms[2].navigate.value='N';
    document.forms[2].submit();
 }else{
  	document.forms[1].method.value = methodName;
   	document.forms[1].navigate.value='N';
    document.forms[1].submit();
    }
 }
 }
 
function doPrevious(methodName)
{
 if(methodName==null){
	 Previous();
  }else{
   if(document.forms[1].currentPage==null){
 		document.forms[2].method.value = methodName;
    	document.forms[2].navigate.value='P';
    	document.forms[2].submit();
 }else{
  		document.forms[1].method.value = methodName;
   		document.forms[1].navigate.value='P';
    	document.forms[1].submit();
    }
  }
 }

function First()
{
   if(document.forms[2] != null && document.forms[2].currentPage!=null){  
	    document.forms[2].navigate.value='F';
   		document.forms[2].submit();
  }else if(document.forms[1] != null && document.forms[1].currentPage!=null){
  		document.forms[1].navigate.value='F';
   		document.forms[1].submit();
  }	else {
	  	document.forms[0].navigate.value='F';
	   	document.forms[0].submit();
  }
  	
 }
function Last()
 {
   if(document.forms[2] != null && document.forms[2].currentPage!=null){
	    document.forms[2].navigate.value='L';
   		document.forms[2].submit();
  }else if(document.forms[1] != null && document.forms[1].currentPage!=null){
  		document.forms[1].navigate.value='L';
   		document.forms[1].submit();
}else{
  		document.forms[0].navigate.value='L';
   		document.forms[0].submit();
  }  
  
 }
function doFirst(methodName){
 
if(methodName==null){
 First();
 }else{
	 if(document.forms[1].currentPage==null){
 		document.forms[2].method.value = methodName;
    	document.forms[2].navigate.value='F';
    	document.forms[2].submit();
 }else{
  		document.forms[1].method.value = methodName;
     	document.forms[1].navigate.value='F';
    	document.forms[1].submit();
    }
 }
 }
 
function doLast(methodName)
{
 if(methodName==null){
	 Last();
  }else{
   if(document.forms[1].currentPage==null){
 	    document.forms[2].method.value = methodName;
    	document.forms[2].navigate.value='L';
     	document.forms[2].submit();
 }else{
  		document.forms[1].method.value = methodName;
    	document.forms[1].navigate.value='L';
    	document.forms[1].submit();
    }
  }
 }

function doNextNew(formId, methodName){
var myForm = document.getElementById(formId);
	if(methodName!=null)
		myForm.method.value=methodName;		
		myForm.navigate.value='N';
 		myForm.submit();
}

function doPreviousNew(formId, methodName)
{
var myForm = document.getElementById(formId);
	if(methodName!=null)
		myForm.method.value=methodName;		
		myForm.navigate.value='P';
 		myForm.submit();
 }
 

 function toPageNew(formId, pageNumber, methodName){
	var myForm = document.getElementById(formId);
	currPage=myForm.currentPage.value;
	if(currPage!=null)
		myForm.navigate.value=pageNumber;
	if(methodName!=null)
		myForm.method.value=methodName;				
	myForm.submit();
 }  
 
function doFirstNew(formId, methodName){
	var myForm = document.getElementById(formId);
	myForm.navigate.value='F';
	if(methodName!=null)
		myForm.method.value=methodName;				
		myForm.submit();
}
 
function doLastNew(formId, methodName){
	var myForm = document.getElementById(formId);
	myForm.navigate.value='L';
	if(methodName!=null)
		myForm.method.value=methodName;				
		myForm.submit();
 }
function toPage(methodName,pageNumber){
 if(document.forms[1].currentPage==null){
	 document.forms[2].method.value = methodName;
     document.forms[2].navigate.value=pageNumber;
     document.forms[2].submit();
 }else{
  	 document.forms[1].method.value = methodName;
     document.forms[1].navigate.value=pageNumber;
     document.forms[1].submit();
    }
 }
 
 
function withoutMethodPage(pageNumber){
  if(document.forms[1].currentPage==null){
	    document.forms[2].navigate.value=pageNumber;
    	document.forms[2].submit();
 }else{
     	document.forms[1].navigate.value=pageNumber;
    	document.forms[1].submit();
    }
 
 }
 
function toSubmit() {

var pageNumber=trim(document.forms[1].pageNumberTo.value);
if(pageNumber!="")
	withoutMethodPage(pageNumber); 
}
 
function check(field,value)
{
 var i=0;
	if(!field.length){
		field.checked=value.checked;
}
	for(i=0; i<field.length;i++){	
    
		  field[i].checked = value.checked;
	 }
}

function limitTextToMax(limitField, maxCount) {
	if (limitField.value.length > maxCount) {
		alert('Your text has been truncated!');
		limitField.value = limitField.value.substring(0, maxCount);
	} 
}
function autofocus(field)
{
	document.getElementById(field).focus();
}

function isValidEmailGroup(formName, boxName)
{
	var box = eval( "document." + formName + "."+ boxName);
	var emailAddresses;
	var intIndexOfMatch = 0;
	if(box != null)
	{	
		if( box.value != null)
		{
			emailAddresses = box.value;
			// Get the index of any next matching substring.
			 intIndexOfMatch = emailAddresses.indexOf(";");			 
			while (intIndexOfMatch != -1)
			{
				 // Relace out the current instance.
				 emailAddresses = emailAddresses.replace( ";", ",");
			  
				 // Get the index of any next matching substring.
				 intIndexOfMatch = emailAddresses.indexOf(";");
			}	
		}
	}

		var arrEmail= emailAddresses.split(',');	
		var msg="";
		var temp;
		for(i=0; i<arrEmail.length; i++)
		{
			if(arrEmail[i] !=null)
			{
				temp = arrEmail[i];
				arrEmail[i] = trim(temp);
				if(isValidEmail(arrEmail[i])==false)
				{
					if(arrEmail[i].length >0)
						msg=msg+arrEmail[i]+", ";					
				}		
			}			
		}

		if( msg.length > 0)
		{
			msg = msg.substring(0, msg.length-2);
		}	
		else
		{
			box.value = arrEmail;
			var str = 	box.value;

			if(str.charAt(str.length-1) == ',')
				str = str.substring(0, str.length-1); 									
				box.value = str;
		}	
	
		return msg;
}	


function methodToCall(methodName,actionName,formName)
{
	document.getElementById(formName).action=actionName;
	document.getElementById(formName).method.value=methodName;
	return true;
}

function checkEmailAddress(form, boxName)
{
	var msg = isValidEmailGroup(form.name, boxName);
	if(msg.length > 0)
	{
		msg = 'Invalid Email addresses:  ' + msg;		
		alert(msg);
		return false;
	}
	else
	{
		return true;
	}	
}

function isValidEmail(str)
{
	str = trim(str);
	//The regular expression for a valid e-mail address
	emailpat = /^([a-zA-Z])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	if( !emailpat.test(str) )
		return false;
		return true;
}

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

function removeSpecialChar(code,formStyleId)
{
var re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
code = code.replace(re, "");
code = trim(code);
var codeType=document.getElementById(formStyleId).codeType.value;
if (codeType == "ISBN" && code.length > 0 && code.length < 10)
{
	var app = "";
	for ( i= 10-code.length; i>0; i--)
		 app = app + "0";
		 code = app + code;
		   		   	}
	document.getElementById(formStyleId).code.value = code;
	return;
}
// used for displaying the loading image
var imageIdArray = new Array();
var arrayIndex = 0;

function storeParams(imageId)
{
	imageIdArray[arrayIndex] = imageId;
	arrayIndex = arrayIndex + 1;
}

function getItemImage()
{
	var actionStr = 'itemimage?size=2&imageId=';
	var i =0;
	for(i=0; i<imageIdArray.length ;i++)
	{	
		var temp = 	document.getElementById('image_'+ (i+1));
		if(imageIdArray[i] != null && imageIdArray[i].length >0)
		{
			temp.src = actionStr + imageIdArray[i];
		}
		else
		{
			temp.src = 'common/secure/assets/images/no_image.gif';
		}	
	}	
}