function trimString (str) {
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
String.prototype.trim = trimString;

var expand_minus = new Image();
expand_minus.src="../images/expand_minus.gif";

//This function is called in reports when the user submits the form and it will be usefull for changing the graph (bar or line)
//neel
function changegraph(type) {
	var frmObj = document.appsreport;
	var bartypeVal = frmObj.bartype.value.trim();
	if ( type != '' ) {
		frmObj.bartype.value = type;
	} else {
		if ( bartypeVal == "" ) {
			frmObj.bartype.value = 'bar';
		}
	}
	
	//Check for valid date range
	if(document.getElementById('customRadio') && document.getElementById('customRadio').checked == true){
		if(frmObj.fromdate.value.length != 10 || frmObj.todate.value.length != 10){
			return false;	//Return false, if dates are empty or invalid
		}
		/*if(!validateDateRange(frmObj.fromdate.value, frmObj.todate.value, 30)){
			//Show some error message
			alert("Invalid date range");
			return false;
		}*/
	}

	frmObj.submit();
}

//This function is used for js validation for custom selection of date range in reports
//Here limit is no of days, the range should be.
function validateDateRange(date1, date2, limit){
	var d1 = set_date(date1);
	var d2 = set_date(date2);
			     //milliseconds / seconds / minutes /hours
	var d1Days = parseInt(d1.getTime() / 1000 / 60 / 60 / 24);
	var d2Days = parseInt(d2.getTime() / 1000 / 60 / 60 / 24);
	if((d2Days < d1Days) || ((d2Days - d1Days) > limit))	return false;
	return true;
	function set_date(dateString){	//dateString in dd-mm-yyyy
		var t = dateString.split("-");
		var d1 = new Date();
		d1.setFullYear(t[2], t[1] - 1, t[0]);
		return d1;
	}
}

//This function will invoike a new window when the user clicks on link
function modelWindow(obj,act_type,id,url,fieldname){
	if(act_type == 'delete'){
		if(confirm('Are You sure you want to delete the record')){
		}else{
			return false;
		}
	}
	if(fieldname.trim()!=''){
		document.getElementById(fieldname).value=id;
	}
	obj.action = url;
	obj.raction.value=act_type;
	obj.submit();
}

//This function is used to clear the text in textarea(contactus.tpl.html,supcnter_contactus.tpl.html)
function cleartext(obj,event){
	if(event!='blur'){
		if(obj.message.value=="Type your message here..."){
			obj.message.value="";
		} 
	} else if(event=='blur') {
		if(obj.message.value==""){
			obj.message.value='Type your message here...';
		}
	}
}

//This function will get all the dashboard values when the user try to select the application
function getdbvalues_archive(url,id){
	var params = "action=getvalues&appid="+id;
	var xmlHttp=getHTTPObject();
	xmlHttp.open("POST", url, true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange=function(){
										if( xmlHttp.readyState == 4){
											//disabling applist box
											document.dashappfrm.dashapplist.disabled=true;
											if(xmlHttp.status == 200){
												var result=xmlHttp.responseText;
												if(result){
													document.getElementById('dbajaxchange').innerHTML=result;
													//enabling applist box
													document.dashappfrm.dashapplist.disabled=false;
												}
											}else{
												// something went wrong, report the error
												alert("HTTP "+xmlHttp.status+". An error was » encountered: "+ xmlHttp.statusText );
												//enabling applist box
												document.dashappfrm.dashapplist.disabled=false;
											}
										}
	};
	xmlHttp.send(params);
}


//This function will invoike when the user try to change the creditpercentage (allmyadds.tpl.html)
//neel Done
function getcreditchange(frm,bannerid,creditpercentage,prvcreditpercentage,creditpercentage_total,url,prevprctname,listname){
	percentage = Number(creditpercentage)-Number(prvcreditpercentage);
	var remval = Number(percentage)+Number(creditpercentage_total);
	if(remval>100) {
		alert("All your future earnings are already allotted");
		listname.value=prvcreditpercentage;
		return;
	}
	if(creditpercentage!=prvcreditpercentage){
		var params = "action=changecrdt&bannerid="+bannerid+"&creditpercentage="+creditpercentage;
		var xmlHttp=getHTTPObject();
		xmlHttp.open("POST", url, true);
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange=function(){
											if( xmlHttp.readyState == 4){
												if(xmlHttp.status == 200){
													var result=xmlHttp.responseText;
													if(result>0){
														document.getElementById('totpercntg').innerHTML=result;
														frm.tot_credit.value=result;
														prevprctname.value=creditpercentage;
													}
												}else{
													// something went wrong, report the error
													alert("HTTP "+xmlHttp.status+". An error was » encountered: "+ xmlHttp.statusText );
												}
											}
		};
		xmlHttp.send(params);
	}
}

//to enable or disable when the user clicks on link of enable or disable
//neel Done
function getenabledisable(bannerid,fieldname,divid,url,fieldname1,fieldname2,prevprctname,frm){
   
 	var status=document.getElementById(fieldname).value;
	var points=document.getElementById(fieldname1).value;
	var creditpercentage=fieldname2.options[fieldname2.selectedIndex].value;
	var displayKey = "";

	if (status == 1) {
		displayKey = "Disable";
	} else if ( status == 0 ) {
		displayKey = "Enable";
	}
	if (displayKey == "Disable")
	{
		if(!confirm("Are you sure you want to disable this ad?(or) This ad has points assigned to it. Disabling it will send the points to back to your pool of available points, from where you can assign them again to any of your ads. Continue?")){
			return;
		} else {
			if(points!='' && points>=0){
				var pnts = points;
			} else {
				var pnts = 0;
			}
		}
	} else {
		if(!confirm(displayKey+ " this ad ?") ) {
			return;
		} else {
			var pnts = 0;
		}
	}

	var params = "action=changeed&bannerid="+bannerid+"&status="+status+"&points="+pnts;
	var xmlHttp=getHTTPObject();
	xmlHttp.open("POST", url, true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange=function(){
		if( xmlHttp.readyState == 4){
			if(xmlHttp.status == 200){
				var result=xmlHttp.responseText;
				resultArr = result.split("^^");
				if(resultArr[0] == 1){
					document.getElementById(divid).innerHTML="Disable";
				}else if(resultArr[0] == 0){
					document.getElementById(divid).innerHTML="Enable";
				}
				document.getElementById(fieldname).value = resultArr[0];
				if(resultArr[1] > 0) {
					document.getElementById('totpercntg').innerHTML=resultArr[1];
					frm.tot_credit.value=resultArr[1];
					prevprctname.value=creditpercentage;
					fieldname2.selectedIndex = 0;
				}
			} else {
				// something went wrong, report the error
				alert("HTTP "+xmlHttp.status+". An error was » encountered: "+ xmlHttp.statusText );
			}
		}
	};
	xmlHttp.send(params);
}

/*
function getenabledisable(bannerid,fieldname,divid,url,fieldname1,fieldname2,prevprctname,frm){
    //var creditpercentage = eval(frm.fieldname2.options[frm.fieldname2.selectedIndex]);
	//form.colorsList.options[form.colorsList.selectedIndex].text
	//var sindex = eval(frm+"."+fieldname2+".selectedIndex")
    //var creditpercentage = frm.fieldname2.options[]);
	//alert(sindex)
	//return false;
	var cp =eval(fieldname2+".options["+fieldname2+".selectedIndex]");
	var cp_sel=eval(fieldname2+".selectedIndex");
	var status=document.getElementById(fieldname).value;
	var points=document.getElementById(fieldname1).value;
	var creditpercentage=cp.value;
	var displayKey = "";
	if (status == 1) {
		displayKey = "Disable";
	} else if ( status == 0 ) {
		displayKey = "Enable";
	}
	if (displayKey == "Disable" && points>0)
	{
		if(!confirm("Are you sure you want to disable this ad?(or) This ad has points assigned to it. Disabling it will send the points to back to your pool of available points, from where you can assign them again to any of your ads. Continue?")){
			return;
		} else {
			var pnts = points;
		}
	} else {
		if( !confirm(displayKey+ " this ad ?") ) {
			return;
		} else {
			var pnts = 0;
		}
	}
	var params = "action=changeed&bannerid="+bannerid+"&status="+status+"&points="+pnts;
	var xmlHttp=getHTTPObject();
	xmlHttp.open("POST", url, true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange=function(){
		if( xmlHttp.readyState == 4){
			if(xmlHttp.status == 200){
				var result=xmlHttp.responseText;
				resultArr = result.split("^^");
				alert(resultArr);
				if(resultArr[0] == 1){
					document.getElementById(divid).innerHTML="Disable";
				}else if(resultArr[0] == 0){
					document.getElementById(divid).innerHTML="Enable";
				}
				document.getElementById(fieldname).value = resultArr[0];
				if(resultArr[1] > 0) {
					document.getElementById('totpercntg').innerHTML=resultArr[1];
					document.getElementById('tot_credit').innerHTML=resultArr[1];
					prevprctname.value=creditpercentage;
					cp_sel="0";
					//cp.selected  = 'selected'; 
				}
			} else {
				// something went wrong, report the error
				alert("HTTP "+xmlHttp.status+". An error was » encountered: "+ xmlHttp.statusText );
			}
		}
	};
	xmlHttp.send(params);
}
*/


//neel
function checkcontact(url){
	var frm = document.contacts;
	var cname = frm.companyname.value.trim();
	var pcname = frm.primarycontactname.value.trim();
	var peid = frm.primaryemailid.value.trim();
	var ppno = frm.primaryphonenumber.value.trim();
	var scname = frm.secondarycontactname.value.trim();
	var seid = frm.secondaryemailid.value.trim();
	var spno = frm.secondaryphonenumber.value.trim();
	frm.contactbtn.disabled=true;
	var params = "action=contactinfo&cname="+cname+"&pcname="+pcname+"&peid="+peid+"&ppno="+ppno+"&scname="+scname+"&seid="+seid+"&spno="+spno;
	var xmlHttp=getHTTPObject();
	xmlHttp.open("POST", url, true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange=function(){
										if( xmlHttp.readyState == 4){
											if(xmlHttp.status == 200){
												var result=xmlHttp.responseText;
												if(result){
													document.getElementById("msg").innerHTML='<font color="green">'+result+'</font>'
													frm.contactbtn.disabled=false;
												}
											}else{
													// something went wrong, report the error
													alert("HTTP "+xmlHttp.status+". An error was » encountered: "+ xmlHttp.statusText );
											}
										}
	};
	xmlHttp.send(params);
}

function hideReportDiv(id) {
	switch (id) {
		case 'reportdiv1':
			document.getElementById('reportdiv1').style.display = 'none';
			document.getElementById('reportdiv2').style.display = '';
			break;
		case 'reportdiv2':
		default :
			document.getElementById('reportdiv1').style.display = '';
			document.getElementById('reportdiv2').style.display = 'none';
	}
}


//This function is for creating an xmlobject which is used for calling server side scripting (AJAX)
function getHTTPObject(){
	var xmlHttp;
	//if running Internet Explorer
	if(window.ActiveXObject){
		try{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e){
			try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xmlHttp=false;
			}
		}
	}  else if(window.XMLHttpRequest) {
		xmlHttp=new XMLHttpRequest();
	}
	//return the created object or display an error message.
	if(!xmlHttp){
		alert("Error creating the XMLHttpRequest Object");
	} else {
		return xmlHttp;
	}
}


// Allowing the Currency or Length  fields to accept only numbers and '.' (used manageads.tpl.html)
function validate_chrg(e){
	if (!e) var e = window.event;
	if (e.keyCode) val = e.keyCode;
	else if (e.which) val = e.which;
	if(navigator.appName == "Netscape"){
		// BackSpace || TAB || End || Home || LeftArrow || RightArrow || Delete - keys
		if(val==8 || val==9 || val==35 || val==36 || val==37 || val==39 || val==46)
		return true;
	}
	if ((val !=13) && (val !=46) && (val < 48)  || (val > 57))
	return false;
}

//using is index.tpl.html
function emailCheck(emailStr) {
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat)==null) {
		return false;
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 ||
		domArr[domArr.length-1].length>4) {
		return false;
	}
	if (len<2) {
		return false;
	}
	return true;
}

//this function is using in index.tpl for validating the SignUp 
// neel
function validateSingUpform(){
	var errMsg = "";
	var bool = true;
	var frm = document.register;

	var emailid = frm.emailid.value.trim();
	var passwd = frm.passwd.value.trim();
	var rpasswd = frm.c_passwd.value.trim();

	if ( emailid == "" ) {
		errMsg += "Please enter an Email address\n";
		bool = false;
	} else {
		if( !emailCheck(emailid) ) {
			errMsg += "Please enter a valid Email address\n";
			bool = false;
		}
	}

	if ( passwd == "" ) {
		errMsg += "Please enter a Password.\n";
		bool = false;
	}

	if ( rpasswd == "" && passwd != "" ) {
		errMsg += "Please re-enter the password to confirm\n";
		bool = false;
	}

	if ( passwd != "" && rpasswd != "" ) {
		if ( passwd == rpasswd ) {
		} else {
			errMsg += "Passwords entered do not match, please re-enter the passwords\n";
			bool = false;
		}
	}

	if(bool) {
		if(frm.tc.checked == false){
			errMsg += "You have to accept Terms and Conditions";
			bool = false;
		}
	}
	frm.emailid.value = frm.emailid.value.trim();

	if ( !bool ) {
		alert( errMsg );
	}
	return bool;
}

//This function is used for validation while login time (index.tpl.html)
function loginfrm() {
	var errMsg = "";
	var bool = true;
	var frm = document.login_form;

	var emailid = frm.lemail.value.trim();
	var passwd = frm.lpasswd.value.trim();
	if ( emailid == "" ) {
		errMsg += "Please enter an Email address\n";
		bool = false;
	} else {
		if( !emailCheck(emailid) ) {
			errMsg += "Please enter a valid Email address\n";
			bool = false;
		}
	}
	if ( passwd == "" ) {
		errMsg += "Please enter a Password.\n";
		bool = false;
	}
	if ( !bool ) {
		alert( errMsg );
	}
	return bool;
}

//this function is used to open terms and conditions window (index.tpl.html)
var termsUrl = '../terms.html';
function openWindow() {
	window.open(termsUrl, 'terms', 'left=20,top=20,width=600,height=650,toolbar=0,location=0,status=0,menubar=0,resizable=0,scrollbars=1');
	return false;
}

function validateAPPSTORE(frm){
	url=frm.url.value.trim();
	if(url!=''){
		return true;
	} else {
		return false;
	}
}

//this function is used for checking url
function validateURL(frm){
	url=frm.url.value.trim();
	var tomatch = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
	if(url!=''){
		if(url!='' && tomatch.test(url)){
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

//this function is used for checking Numer
function  validateNumber(frm){
	var val=frm.url.value.trim();
	var anum=/(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(val))
	   return true;
	else{
		return false;
	}
}

function hideDiv(id) {
	document.getElementById(id).style.display = 'none';		
}


/*function showDiv(id) {
			switch (id) {
				case 'help_1':
						document.getElementById('help_1').style.display = '';
						document.getElementById('help_2').style.display = 'none';
						document.getElementById('help_3').style.display = 'none';
						document.getElementById('help_4').style.display = 'none';
						document.getElementById('help_5').style.display = 'none';
						document.getElementById('help_6').style.display = 'none';
						break;
				case 'help_2':
						document.getElementById('help_1').style.display = 'none';
						document.getElementById('help_2').style.display = '';
						document.getElementById('help_3').style.display = 'none';
						document.getElementById('help_4').style.display = 'none';
						document.getElementById('help_5').style.display = 'none';
						document.getElementById('help_6').style.display = 'none';
						break;
				case 'help_3':
						document.getElementById('help_1').style.display = 'none';
						document.getElementById('help_2').style.display = 'none';
						document.getElementById('help_3').style.display = '';
						document.getElementById('help_4').style.display = 'none';
						document.getElementById('help_5').style.display = 'none';
						document.getElementById('help_6').style.display = 'none';
						break;
				case 'help_4':
						document.getElementById('help_1').style.display = 'none';
						document.getElementById('help_2').style.display = 'none';
						document.getElementById('help_3').style.display = 'none';
						document.getElementById('help_4').style.display = '';
						document.getElementById('help_5').style.display = 'none';
						document.getElementById('help_6').style.display = 'none';
						break;
				case 'help_5':
						document.getElementById('help_1').style.display = 'none';
						document.getElementById('help_2').style.display = 'none';
						document.getElementById('help_3').style.display = 'none';
						document.getElementById('help_4').style.display = 'none';
						document.getElementById('help_5').style.display = '';
						document.getElementById('help_6').style.display = 'none';
						break;
				case 'help_6':
						document.getElementById('help_1').style.display = 'none';
						document.getElementById('help_2').style.display = 'none';
						document.getElementById('help_3').style.display = 'none';
						document.getElementById('help_4').style.display = 'none';
						document.getElementById('help_5').style.display = 'none';
						document.getElementById('help_6').style.display = '';
						break;
			}
		}*/

	/*function showDiv(id) {
		//totalCount is 8 as far now, if new help div is added please increment constant in for condition
		for(var i = 1; i <= 8; i++){
			if(document.getElementById('help_'+i)){
				document.getElementById('help_'+i).style.display = 'none';
			}
		}
		document.getElementById(id).style.display = '';
	}*/

	function showDiv(id) {
		//totalCount is 8 as far now, if new help div is added please increment constant in for condition
		var show = '';
		if(document.getElementById(id).style.display != 'none'){
			var show = 'none';
		}

		for(var i = 1; i <= 8; i++){
			if(document.getElementById('help_'+i)){
				document.getElementById('help_'+i).style.display = 'none';
			}
		}
		document.getElementById(id).style.display = show;
	}
	
	//This function is used for show/hide of Groupby fieldset in reports
	function toggleCalControls(){
		if(document.getElementById('filterGroupByUL').style.display != "none"){
			document.getElementById('filterGroupByUL').style.display = "none";
			document.getElementById('customGroupByUL').style.display = "block";
			document.getElementById('toggleLink').innerHTML = "hide";
			document.getElementById('customRadio').checked = true;
		}else{
			document.getElementById('filterGroupByUL').style.display = "block";
			document.getElementById('customGroupByUL').style.display = "none";
			document.getElementById('toggleLink').innerHTML = "show";
			document.getElementById('filterRadio1').checked = true;
		}
		
	}


	//This fucntion is to select menus in faq page
	function selectFAQTab(idx){
		for(var i = 1; i <= 6; i++){
			//faq_cat_active selectDiv1 tabDiv
			document.getElementById('tabDiv'+i).className = '';
			document.getElementById('selectDiv'+i).style.display = 'none';
		}
		document.getElementById('tabDiv'+idx).className = 'faq_cat_active';
		document.getElementById('selectDiv'+idx).style.display = 'block';
	}