function createAjaxObject() {
	var x;
	var browser = navigator.appName;
	
	if(browser == 'Microsoft Internet Explorer') {
		x = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		x = new XMLHttpRequest();
	}
	return x;
}

function isValidUserName(str) { // Alpabets, Numbers, Special Chars [._] only accepted
	if (/^(([a-zA-Z])+([._])*([0-9])*)+$/.test(str)) {
		return true;
	}
	return false;
}

function isValidEmail(testStr) {
	//if(/^\w+([\.-]?\w+)*@\w+([\.-]?\W+)*(\.\w{2,4})+$/.test(testStr)) {
	if(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(testStr)) {
		return true;
	}
	return false;
}

function isValidAlphaNumeric(str) { // Alpabets, Numbers only accepted
	if (/^(([a-z])*([A-Z])*([0-9])*)+$/.test(str)) {
		return true;
	}
	return false;
}

function isValidAlphabet(str) { // Alpabets only accepted, Space not accepted
	if (/^(([a-z])*([A-Z])*)+$/.test(str)) {
		return true;
	}
	return false;
}

function isValidString(str) {	// Alpabets with Space only accepted
	if (/^(([a-z])*([A-Z] )*)+$/.test(str)) {
		return true;
	}
	return false;
}

function isValidNumeric(str) { // Numbers only accepted
	if (/^(([0-9])*)+$/.test(str)) {
		return true;
	}
	return false;
}

function isValidTelephone(str) {	// Numbers, Special Chars ([/-#()+ ]), Space not accepted, String not accepted
	if (/^(([0-9])*([-#(/)+ ])*)+$/.test(str)) {
		return true;
	}
	return false;
}

function isValidTelephoneWithString(str) { // Numbers,Alpabets, Special Chars ([-#()+ ]), Space accepted
	if (/^(([a-z])*([A-Z])*([0-9])*([-#()+ ])*)+$/.test(str)) {
		return true;
	}
	return false;
}

function isValidZipcode(str) { // Alpabets, Numbers only accepted
	if (/^(([a-z])*([A-Z])*([0-9])*)+$/.test(str)) {
		return true;
	}
	return false;
}

function replaceURL(str) {
	ret_str = str.replace(/&/g,"-");
	ret_str = ret_str.replace(/ /g,"_");
	return ret_str;
}
	
function replaceAjax(str) {
	ret_str = str.replace(/&/g," GwA ");
	ret_str = ret_str.replace(/-/g," PfL ");
	ret_str = ret_str.replace(/\n/g," DlT ");
	ret_str = ret_str.replace(/</g," JwN ");
	ret_str = ret_str.replace(/\+/g," FqY ");
	ret_str = ret_str.replace(/\?/g," XjQ ");
	ret_str = ret_str.replace(/>/g," YvO ");
	ret_str = ret_str.replace(/`/g," AhE ");
	return ret_str;
}

function isValidFile(str,type) {
	// type = image,text,video
	
	if(!type || !str) {
		alert("Missing argument!, The Arguments are (File Control Value,File Type)");
		return false;                            
	}
	
	var x;
	var flag = false;
	var file_type;
	
	var lc_fileext = str.toLowerCase();
	var file_array = lc_fileext.split('.');
	var array_len = file_array.length;
	var file_ext = file_array[array_len-1];
	//alert(file_ext);
	if(type == 'image') {
		file_type = 'bmp,jpg,jpeg,png,gif';
	}
	if(type == 'text') {
		file_type = 'txt,doc,docx,pdf,rtf';
	}
	if(type == 'video') {
		file_type = 'mpeg,mpg,asf,dat,avi,wmv,dat';
	}
	
	var file_split = file_type.split(',');
	var file_array_len = file_split.length;
	
	for(x=0; x<file_array_len; x++)
	{
		if(file_ext == file_split[x])
		{
			flag = true;
			return true;
		}
	}
	if(!flag) {
		return false;
	}
}

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 clearFormValues(form_name, allId, newClass, inner_html_id, clear_ind_value) {
	
	if(form_name) {
		var test_flag = false;
		test_form = form_name.toUpperCase()
		if(test_form == "ALL" || test_form == "CLEAR_ALL" || test_form == "CLR" || test_form == "CLEAR") {
			test_flag = true;
		}
		
		form_length = document.forms.length;
	
		for(loop = 0; loop < form_length; loop++) {
			
			var new_form = document.forms[loop].name;
	
			if((new_form == form_name) || test_flag) {
				control_length = document.forms[loop].length
	
				for (var i = 0; i < control_length; i++) {
					var elem = document.forms[loop][i];
					var control_type = elem.type;
					
					if(control_type == "text" || control_type == "password" || control_type == "select-one" || control_type == "textarea") {
						elem.value = "";
					}
					
					if(control_type == "file") {
						elem.value = "";
						elem.outerHTML = elem.outerHTML;
					}
	
					if(control_type == "checkbox" || control_type == "radio") {
						elem.checked = false;
					}
				}
			}
		}
	}
	
	if(allId || newClass) {
		changeClass(allId, newClass);	
	}
	
	if(inner_html_id) {
		clearInnerHTML(inner_html_id);
	}	
	
	if(clear_ind_value) {
		clearIndividualValue(clear_ind_value);
	}
}

function changeClass(allId, newClass) { // allId -> all id's seperate with comma
	var arr = allId.split(",");
	var len = arr.length;
	for (i = 0; i < len; i++) {
		elementID = trim(arr[i]);
		if(document.getElementById(elementID)) {
			//var tempObj = document.getElementById(elementID);
			//tempObj.className = tempObj.className.replace(new RegExp(oldClass),newClass);
			document.getElementById(elementID).className = newClass;
		}
	}
}

function clearInnerHTML(allId) { // allId -> all id's seperate with comma
	var arr = allId.split(",");
	var len = arr.length;
	
	for (i = 0; i < len; i++) {
		elementID = trim(arr[i]);
		if(document.getElementById(elementID)) {
			document.getElementById(elementID).innerHTML = "";
		}
	}
}

function clearIndividualValue(allId) { // allId -> all id's seperate with comma
	var arr = allId.split(",");
	var len = arr.length;
	
	for (i = 0; i < len; i++) {
		elementID = trim(arr[i]);
		if(document.getElementById(elementID)) {
			document.getElementById(elementID)['value'] = "";
		}
	}
}

function openPopup(url,w,h) {
	var newwindow=window.open(url,'name','height='+h+',width='+w+',scrollbars=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}

function charRemaining(tField, nField, maxAllowed) {
	if (tField.value.length > maxAllowed) {
		tField.value = tField.value.substring(0, maxAllowed);
	}
	nField.value = maxAllowed - tField.value.length;
}

function checkORUncheck(total,sel_type,class_name) {
	if(!class_name) {
		class_name = "list_link";
	}
	if(sel_type == 'check') {
		for(i = 1; i <= total; i++) {
			document.getElementById("cblistid"+i)["checked"]=true;
		}
		document.getElementById("check_uncheck_id").innerHTML="<a href=javascript:checkORUncheck('"+total+"','uncheck','"+class_name+"') class="+class_name+">Uncheck All</a>";	
	} else {
		for(i = 1; i <= total; i++) {
			document.getElementById("cblistid"+i)["checked"] = false;
		}
		document.getElementById("check_uncheck_id").innerHTML="<a href=javascript:checkORUncheck('"+total+"','check','"+class_name+"') class="+class_name+">Check All</a>";
	
	}
}

function getActionConfirmMessage(action_type) {
	if(action_type == 'delete') {
		message = "Are you sure to delete these Record(s)?";
	} else if(action_type == 'activate') {
		message = "Are you sure to Activate these Record(s)?";
	} else if(action_type == 'inactivate') {
		message = "Are you sure to Inactivate these Record(s)?";
	} else if(action_type == 'send') {
		message = "Are you sure to send these Record(s)?";
	} else {
		message = "Are you sure?";
	}
	return message;
}

function sortFun(sort_field) {
	document.getElementById("sort_field")["value"] = sort_field;
	
	var sort_type = document.getElementById("sort_type")["value"];
	if(!sort_type || sort_type == 'desc') {
		sort_type = 'asc'
	} else {
		sort_type = 'desc'
	}
	document.getElementById("sort_type")["value"] = sort_type;
	searchFun();
}

function isValidNumber(num) { // Call this Function at a key press Event
	var num_val = document.getElementById(num).value;
	if(isNaN(num_val)) {
		var num_len = num_val.length;
		var num_value = num_val.substring(0, num_len-1);
		document.getElementById(num).value = num_value;
		if(isNaN(num_value)) {
			document.getElementById(num).value = "";
		}
		alert("Numbers Only Accepted");
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function roundNumber(num, dec) {
	if(!dec) {
		dec = 0;
	}
 	var num1 = new Number (parseFloat (num));
 	var return_num = num1.toFixed (dec);
 	return return_num;
}

function GetFckContents(control_name) {
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(control_name) ;
	
	// Get the editor contents in XHTML.
	var fck_value= oEditor.GetXHTML( true );             // "true" means you want it formatted.
	return fck_value;
}

function ClearFckContents(control_name) {
	var oEditor = FCKeditorAPI.GetInstance(control_name);
	oEditor.SetHTML("");
	return false;
}

function splitArray(str,sep) {
	splt_dat = str.split(sep);
	return splt_dat;
}

function getDateDiff(d,m,y, today_d,today_m,today_y) {
	m = m-1;
	var startingdate = new Date(y, m, d); //Month is 0-11 in JavaScript
	
	if(today_d && today_m && today_y) {
		today_m = today_m - 1;
		var today = new Date(today_y, today_m, today_d); //Month is 0-11 in JavaScript
	} else {
		var today = new Date();
	}
	var one_month = 1000*60*60*24;
	var res = Math.ceil( (today.getTime()-startingdate.getTime() ) / (one_month) );
	return res;
}