/***************************************************************************************/
// General functions
/***************************************************************************************/

/////////////////////////////////////////////////////////////////////////////////////////
// Removes the leading and trailing spaces in a strings and returns the trimmed string
/////////////////////////////////////////////////////////////////////////////////////////
function OpenNamedWindow(winname,wintype, turl, wd, ht) {
        var windowFeatures =  '';
        if(wintype == 'tablename') {
                window_width = 450;
                window_height = 300;
                window_top = (screen.availHeight-window_height)/2
                window_left = (screen.availWidth-window_width)/2
                windowFeatures += "width=" + window_width + ",height=" + window_height + ",top="
                windowFeatures += window_top
                windowFeatures += ",left="
                windowFeatures += window_left
                windowFeatures += ',status=1'
                windowFeatures += ',scrollbars=yes'
        }
        if (wintype == 'general') {
          window_width = wd;
          window_height = ht;
          window_top = (screen.availHeight-window_height)/2
          window_left = (screen.availWidth-window_width)/2
          windowFeatures += "width=" + window_width + ",height=" + window_height + ",top="
          windowFeatures += window_top
          windowFeatures += ",left="
          windowFeatures += window_left
          windowFeatures += ',scrollbars=yes'
     }
        if (wintype == 'invoice') {
          window_width = wd;
          window_height = ht;
          window_top = (screen.availHeight-window_height)/2
          window_left = (screen.availWidth-window_width)/2
          windowFeatures += "width=" + window_width + ",height=" + window_height + ",top="
          windowFeatures += window_top
          windowFeatures += ",left="
          windowFeatures += window_left
          windowFeatures += ',scrollbars=yes,menubar=yes'
     }
window.open(turl,winname,windowFeatures);
}

function trimSpaces(stringValue) {
	// Checks the first occurance of spaces and removes them
	for(i = 0; i < stringValue.length; i++) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i > 0) {
		stringValue = stringValue.substring(i);
	}
	
	// Checks the last occurance of spaces and removes them
	strLength = stringValue.length - 1;
	for(i = strLength; i >= 0; i--) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i < strLength) {
		stringValue = stringValue.substring(0, i + 1);
	}
	
	// Returns the string after removing leading and trailing spaces.
	return stringValue;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Check whether a string contain permitted characters only
/////////////////////////////////////////////////////////////////////////////////////////
function checkAllowedChars(strToCheck, allowedChars)
{
     var acLen     = allowedChars.length;
     var stcLen     = strToCheck.length;
     strToCheck     = strToCheck.toLowerCase();
     var i;
     var j;
     var rightCount = 0;
     for(i = 0; i < acLen; i++)
     {
          switch(allowedChars.charAt(i))
          {
          case 'A':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) >= 'a' && strToCheck.charAt(j) <= 'z';
               }
               break;
          case 'N':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) >= '0' && strToCheck.charAt(j) <= '9';
               }
               break;
          default:
               for(j = -1; -1 != (j = strToCheck.indexOf(allowedChars.charAt(i), j + 1)); rightCount++);
               break;
          }
     }
     if(rightCount == stcLen)
     {
          return true;
     }
     return false;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a date entered is a valid date or not.
/////////////////////////////////////////////////////////////////////////////////////////
function checkDate(ddVal, mmVal, yyVal) {
	if(ddVal <= 0 || ddVal > 31 || mmVal <= 0 || mmVal > 12 || yyVal <= 0 || yyVal.length != 4) {
		alert("Please enter a valid date");
		return false;
	}
	monthArray = new Array("January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	if(mmVal == 2) {
		if(yyVal % 4 == 0) {
			if(ddVal > 29) {
				alert("February " + yyVal + " has only 29 days");
				return false;
			}
		}
		else {
			if(ddVal > 28) {
				alert("February " + yyVal + " has only 28 days");
				return false;
			}
		}
	}
	else {
		if(mmVal == 4 || mmVal == 6 || mmVal == 9 || mmVal == 11) {
			if(ddVal > 30) {
				alert(monthArray[mmVal-1] + " has only 30 days");
				return false;
			}
		}
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether the first date argument is less than the second date argument
// Date arguments should be in the format - dd/mm/yyyy
/////////////////////////////////////////////////////////////////////////////////////////
function checkDateDifference(lowDate, highDate) {
//alert(lowDate);
//alert(highDate);
	lowDateSplit = lowDate.split('/');
	highDateSplit = highDate.split('/');

	date1 = new Date();
	date2 = new Date();

	date1.setDate(lowDateSplit[0]);
	date1.setMonth(lowDateSplit[1] - 1);
	date1.setYear(lowDateSplit[2]);

	date2.setDate(highDateSplit[0]);
	date2.setMonth(highDateSplit[1] - 1);
	date2.setYear(highDateSplit[2]);

	if(date1.getTime() < date2.getTime()) {
		return true;
	}
	else {
		return false;
	}
}


/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a string is a valid email address.
/////////////////////////////////////////////////////////////////////////////////////////
function checkEmail(emailString) {
	splitVal = emailString.split('@');
	
	if(splitVal.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitVal[0].length <= 0 || splitVal[1].length <= 0) {
		alert("Please enter a valid email address");
		return false;
	}
	
	splitDomain = splitVal[1].split('.');
	if(splitDomain.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Displays the decimal value formatted to 2 decimal places and rounded to the next
// 10 paise
/////////////////////////////////////////////////////////////////////////////////////////
function numFormat(formElement, decimalValue) {
		amtValue = new String(decimalValue);
		valueSplit = amtValue.split(".");
		if(valueSplit.length <= 1) {
			amtValue = valueSplit[0] + ".00";
		}
		else {
			if(valueSplit[1].length < 2) {
				amtValue = valueSplit[0] + "." + valueSplit[1] + "0";
			}
			else {
				if(valueSplit[1].length > 2) {
					amtValue = valueSplit[0] + "." + valueSplit[1].substring(0, 2);
				}
			}
		}

		valueSplit = amtValue.split(".");

		paiseValue = Math.ceil(valueSplit[1] / 10);
		rupeeValue = 0;
		if(paiseValue >= 10) {
			rupeeValue = parseInt(valueSplit[0]) + 1;
			valueSplit[0] = new String(rupeeValue);
			paiseValue = 0;
		}
		paiseString = new String(paiseValue);
		paiseString += "0";
		amtValue = valueSplit[0] + "." + paiseString;

		frmElement = eval(formElement);
		frmElement.value = amtValue;
}
//function to open in custom window
function openWindow(url,window_name,winWidth,winHeight,fscroll) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	

	sLeft = (sWidth - winWidth) / 2;
	sTop = (sHeight - winHeight) / 2;
	if(fscroll == '') {fscroll = 0}
	window.open(url,window_name,"width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=" + fscroll + ",resizable=0");
}
function closeWindow() {

	parent.window.opener.window.focus();
	parent.window.close();
}
function printWindow() {
	//parent.mainFrame.focus();
	//window.print(parent.mainFrame);
	window.print(self);
}

function roundValue(decimalValue){
	tempValue = 0;
	tempValue = Math.round(decimalValue * 100)/100;
	return tempValue;
}

function checkIfElementExists(fldName)
{
	var elementLength = 0;
	var totalElements = 0;
	totalElements = document.forms[0].elements.length;
	for(i = 0; i < totalElements; i++)
	{
		if(document.forms[0].elements[i].name == fldName)
		  {
			elementLength++;
		 }			
	}
	if(elementLength <= 0)
	{
		return false;
	}
	return true;
}

function openNewWindow(fileStr,winWidth,winHeight) 
{
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;	
	window.open("showPicture.asp?filename=" + fileStr, "File", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=1");
	return;
}
function showPhoto(selImgPos,winWidth,winHeight) 
{
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;	
	window.open("viewPhotoList.asp?selImgPos=" + selImgPos, "File", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
	return;
}

function showAlbum() {

	sWidth = screen.availWidth;
	sHeight = screen.availHeight;	
	sTop = (sHeight - 600) / 2;
	sLeft = (sWidth - 430) / 2;	
	
	selValue = document.album.albumtitle.options[document.album.albumtitle.selectedIndex].value;

	if(selValue.length <= 0) {
		alert("Please select an album.");
		document.album.albumtitle.focus();
		return false;
	}
	switch (selValue) {
		case "1": 
			pageUrl = "/nikhil/html/video1.htm"
			break;
		case "2":
			pageUrl = "/nikhil/html/video2.htm"
			break;
		case "3":
			pageUrl = "/nikhil/html/video3.htm"
			break;
		case "4":
			pageUrl = "/nikhil/html/video4.htm"
			break;
	}
	window.open(pageUrl, "Album", "width=617,height=408,top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
	return;
	
	alert(frmElem.value);
}