function formatUSCurrency(num) {
	if (isNaN(num)) {
		num = "0";
	}
	sign = (num == (Math.abs(num)).toString());
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10) {
		cents = "0" + cents;
	}
	for ( var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
		num = num.substring(0, num.length - (4 * i + 3)) + ','
				+ num.substring(num.length - (4 * i + 3));
	}
	return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

function formatIndiaCurrency(num) {
	if (isNaN(num)) {
		num = "0";
	}
	sign = (num == (Math.abs(num)).toString());
	num = Math.floor(num * 100 + 0.50000000001);
	paise = num % 100;
	if (paise < 10) {
		paise = "0" + paise;
	}
	num = Math.floor(num / 100).toString();
	hund = num.substring(num.length - (3));
	num = num.substring(0, num.length - (3));
	if (num.length > 0) {
		for ( var i = 0; i < Math.floor((num.length - (1 + i)) / 2); i++) {
			num = num.substring(0, num.length - (3 * i + 2)) + ','
					+ num.substring(num.length - (3 * i + 2));
		}
		return (((sign) ? '' : '-') + 'Rs.' + num + ',' + hund + '.' + paise);
	} else {
		return (((sign) ? '' : '-') + 'Rs.' + hund + '.' + paise);
	}
}

function Start(page,width,height) {
	OpenWin = this
			.open(page, "CtrlWindow",
					'toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,left=50,top=100,width='+width+',height='+height);
}

function clearSelectOptions(select) {
	try{
		if(select !=null){
			select.options.length = 0;
		}
	}catch(err){}
}

function setHiddenName(cbEle,hiEle){
	if(cbEle!=null){
		var index = cbEle.selectedIndex;
		var name = cbEle[index].innerHTML;
		hiEle.value = name;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

function isNull(value) {
	if (value == null || value.trim().length == 0) {
		return true;
	}
	return false;
}

function hideEnable(id) {
	if(document.getElementById(id)!=null){
		if (document.getElementById(id).style.display == "none") {
			document.getElementById(id).style.display = "";
		} else {
			document.getElementById(id).style.display = "none";
		}
	}
}

function currentDate() {
	return padDate(new Date());
}

function padDate(val){
	dt=PadNumber(val.getDate(),"00")+"-"+ padDate.monthnames[val.getMonth()]+"-"+val.getFullYear();
	return dt;
}

padDate.monthnames = {
		0:'Jan', 1:'Feb', 2:'Mar', 3:'Apr', 4:'May', 5:'Jun',
		6:'Jul', 7:'Aug', 8:'Sep', 9:'Oct', 10:'Nov', 11:'Dec'
	};

function PadNumber(Num,pad)
{return (pad+Num).substring(Num.toString().length);}

function parseDate(s)
{
	var pat=/^(\d{1,2})-(\w{3})-(\d{2}|\d{4})$/;
	var p1=pat.test(s);
	if (p1) with (RegExp) 
	{
		var Y = +$3;
		var M = parseDate.months[($2).toLowerCase()];
		if(Y<100)Y+=(Y<60?2000:1900);
		return new Date(Y, M, $1);
	}
}
parseDate.months = {
	jan: 0, feb: 1, mar: 2, apr: 3, may: 4, jun: 5,
	jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11
};



function dateComparator(a,b) {
    var dt1=parseDate(a); var dt2=parseDate(b);
    if (dt1.valueOf()==dt2.valueOf()) return 0;
    if (dt1<dt2) return -1;
    return 1;
}


function getfileExtention() {
	var extentions = Array();
	extentions[0] = ".gif";
	extentions[1] = ".jpg";
	extentions[2] = ".png";
	extentions[3] = ".bmp";
	extentions[4] = ".jpeg";
	return extentions;
}

function validatePic(filename) {
	var isValid = false;
	var extention = filename.substr(filename.lastIndexOf('.')).toLowerCase();
	var extentions = getfileExtention();

	for ( var i = 0; i < extentions.length; i++) {
		if (extentions[i] == extention) {
			isValid = true;
		}
	}
	return isValid;
}

function textOnly(text) {
	var textFormat = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\0-9]/;
	return (!checkFomate(textFormat, text));
}

function hasWhiteSpace(text){
	var textFormat = /^\s+$/;
	alert(checkFomate(textFormat, text));
	return (checkFomate(textFormat, text));
}

function isContainEmpty(text){
	var textFormat = /\s/g; 
	return (checkFomate(textFormat, text));
}
function checkFile(filename) {

	var fileInvalidchar = /[\/:*?\"<>|]/;
	alert(checkFomate(fileInvalidchar, filename));
}

function urlValidation(url) {
	var urlformat = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	return checkFomate(urlformat, url);
}
function emailValidation(email) {
	var emailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	return checkFomate(emailformat, email);
}

function zipCodeValidate(zipcod) {
	var zipcodeformat = /^[1-9][0-9]*$/;
	return checkFomate(zipcodeformat, zipcod);
}

function phoneValidate(phoneNo) {
	if (phoneNo.length <= 7) {
		return false;
	}
	var phoneformat = /^([0-9]*)$/;
	return checkFomate(phoneformat, phoneNo);
}

function lphoneValidate(phoneNo) {
	if (phoneNo.length < 6) {
		return false;
	}
	var phoneformat = /^([0-9]*)$/;
	return checkFomate(phoneformat, phoneNo);
}

function mphoneValidate(phoneNo) {
	if (phoneNo.length < 10) {
		return false;
	}
	var phoneformat = /^([0-9]*)$/;
	return checkFomate(phoneformat, phoneNo);
}

function checkFomate(format, string) {

	if (string.match(format)) {
		return true; // valid
	} else {
		// alert( 'The phone number entered is invalid!' );
		return false; // invalid
	}
}

function findSpecialChar(stringval) {

	var specialchar = "*|,\":<>[]{}`\';()@&$#%";
	for ( var i = 0; i < stringval.length; i++) {
		if (specialchar.indexOf(stringval.charAt(i)) != -1) {
			return true;
		}
	}
	return false;

}

function passwordvalidate(id) {
	if (document.getElementById(id).value.length < 6) {
		return "Password must be minimum 6 characters";
	} else if (document.getElementById(id).value.length > 10) {
		return "Password must be less than 10 characters";
	}
	return "valid";
}

function hide(id) {
	if(document.getElementById(id)!=null){
		if (document.getElementById(id).style.display != "none") {
			document.getElementById(id).style.display = "none";
		}
	}
}

function enable(id) {
	if(document.getElementById(id)!=null){
		if (document.getElementById(id).style.display == "none") {
			document.getElementById(id).style.display = "";
		}
	}
}

function emailVadiation(str) {

	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;

	if (str.indexOf(at) == -1) {
		return false;
	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0
			|| str.indexOf(at) == lstr) {
		return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0
			|| str.indexOf(dot) == lstr) {
		return false;
	}

	if (str.indexOf(at, (lat + 1)) != -1) {
		return false;
	}

	if (str.substring(lat - 1, lat) == dot
			|| str.substring(lat + 1, lat + 2) == dot) {
		return false;
	}

	if (str.indexOf(dot, (lat + 2)) == -1) {
		return false;
	}

	if (str.indexOf(" ") != -1) {
		return false;
	}

	return true;
}

function checkNotEmpty(elementname) {
	for (i = 0; i < elementname.length; i++) {
		if (document.getElementById(elementname[i]).value == "") {
			return false;
		}
	}
	return true;
}

function isEmptyField(id) {
	if (document.getElementById(id).value == "") {
		return true;
	}
	return false;
}



function createcssfile(filename) {

	var fileref = document.createElement("link");
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", filename);

	return fileref;
}

function replacejscssfile(oldfilename, newfilename, filetype) {
	var targetelement = "link";// determine element type to create nodelist
	// using
	var targetattr = "href";// determine corresponding attribute to test for
	var allsuspects = document.getElementsByTagName(targetelement);
	for ( var i = allsuspects.length; i >= 0; i--) { // search backwards
		// within nodelist for
		// matching elements to
		// remove
		if (allsuspects[i]
				&& allsuspects[i].getAttribute(targetattr) != null
				&& allsuspects[i].getAttribute(targetattr).indexOf(oldfilename) != -1) {
			var newelement = createcssfile(newfilename);
			allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
		}
	}
}

function firstToUpper(word) {
	firstLetter = word.substring(0, 1);
	restOfWord = word.substring(1);
	return (firstLetter.toUpperCase()+restOfWord.toLowerCase());
}

function ajaxsessionTimeout(xdoc){
	 var sesstime=getNodeValue(xdoc,"session","yes");
	 if (sesstime == "no"){
			window.location = ajaxerrorURL;
			return true;
		}
		return false;
}
function getNodeValue(node, tagname, default_val) {
	var elems = node.getElementsByTagName(tagname);
	if ((null != elems) && (null != elems[0])
			&& (null != elems[0].firstChild)) {
		return elems[0].firstChild.nodeValue;
	}
	return default_val;
}
