// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function checkLogin(){
		var userId = document.getElementById("user_user_name").value;

		if (userId == "") {
			alert("Please enter 'User Name'");
			return;
		}
		else 
			if (!alphabetsOnly(userId.substring(0, 1), "userId")) {
				return;
			}
			else 
				if (!alphanumericonly(userId, "userId")) {
					document.getElementById("user_user_name").value = "";
					return;
				}
				else{
						window.location = "/aditch/availability/" + userId;
					}	
		
	}
	
	function eMailChk(str) {

		var at="@";
		var dot=".";
		var quot = "\'" ;
		var dblQuote = "\"" ;
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		// check for empty string
		if(isEmpty(str)) {
			return false;
		}
		if (str.length < 3)
	 	{
	   		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
	   		return (false);
	 	}
		// check for @ symbol 
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		   return false;
		}
		//	check for (.) symbol 
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			alert("Please enter a complete email address in the form: yourname@yourdomain.com");
			return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
			alert("Please enter a complete email address in the form: yourname@yourdomain.com");
			return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			alert("Please enter a complete email address in the form: yourname@yourdomain.com");
			return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
			alert("Invalid E-mail ID ");
			return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
			alert("Invalid E-mail ID ");
			return false;
		 }
		if (str.indexOf(quot)>-1 || str.indexOf(dblQuote)>-1){
			alert("Quotes are not allowed");
			return false;
		}

		 return true					
	}
	function isEmpty(aTextField){
		if (aTextField == null) {
			return false;
		}
	}
	function alphanumericonly(field,fieldName){
			var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
			var allValid = true;
			for (i = 0;  i < field.length;  i++){
				ch = field.charAt(i);
				for (j = 0;  j < checkOK.length;  j++)
					if (ch == checkOK.charAt(j))
						break;
				if (j == checkOK.length){
					allValid = false;
					break;
				}
			}
				
			if (!allValid){
				alert(fieldName+" may consist of a-z, 0-9, and underscores");
				return (false);
			}
			else
				return true;
	}
	
	function numberWithHiphon(field,fieldName){
			var checkOK = "0123456789-";
			var allValid = true;
			for (i = 0;  i < field.length;  i++){
				ch = field.charAt(i);
				for (j = 0;  j < checkOK.length;  j++)
					if (ch == checkOK.charAt(j))
						break;
				if (j == checkOK.length){
					allValid = false;
					break;
				}
			}
				
			if (!allValid){
				alert(fieldName+" may consist of integer value");
				return (false);
			}
			else
				return true;
	}
	
	function numberWithDot(field,fieldName){
			var checkOK = "0123456789.";
			var allValid = true;
			for (i = 0;  i < field.length;  i++){
				ch = field.charAt(i);
				for (j = 0;  j < checkOK.length;  j++)
					if (ch == checkOK.charAt(j))
						break;
				if (j == checkOK.length){
					allValid = false;
					break;
				}
			}
				
			if (!allValid){
				alert(fieldName+" may consist of decimal number");
				return (false);
			}
			else
				return true;
	}
	
	function alphabetsOnly(field,fieldName){
		var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
			var allValid = true;
			for (i = 0;  i < field.length;  i++){
				ch = field.charAt(i);
				for (j = 0;  j < checkOK.length;  j++)
					if (ch == checkOK.charAt(j))
						break;
				if (j == checkOK.length){
					allValid = false;
					break;
				}
			}
				
			if (!allValid){
				alert(fieldName+" must start with alphabets");
				return (false);
			}
			else
				return true;
	}
	
	function numberOnlyPoints(myfield, e){
		var key;
		var keychar;
		if (window.event)
		   key = window.event.keyCode;
		else if (e)
		   key = e.which;
		else
		   return true;
		keychar = String.fromCharCode(key);
		// control keys
		if ((key==null) || (key==0) || (key==8) || 
		    (key==9) || (key==13) || (key==27) || (key==46))
		   return true;
		
		// numbers
		else if ((("0123456789").indexOf(keychar) > -1))
		   return true;
		else
		   return false;
	}
	
	
	function numberOnly(myfield,e){
		var field = document.getElementById(myfield).value;
		var key;
		var keychar;
		if (window.event)
		   key = window.event.keyCode;
		else if (e)
		   key = e.which;
		else
		   return true;
		keychar = String.fromCharCode(key);
		// control keys
		if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) 
			return true;
	// numbers
		else 
			if ((("0123456789").indexOf(keychar) > -1)) {
				return true;
			}
			else {
				document.getElementById(myfield).value = field;
				return;
			}	
	}
	
	

	function changeAdvtDiv(advt_type){
		if (advt_type == "image"){
			document.getElementById("iDiv").className="aTabStyle";
			document.getElementById("vDiv").className="tabStyle";
			document.getElementById("tDiv").className="tabStyle";
			document.getElementById("advertisement_advt_type").value="image";
			document.getElementById("imageDiv").style.display = "block";
			document.getElementById("vedioDiv").style.display = "none";
			document.getElementById("textDiv").style.display = "none";
		}else if (advt_type == "vedio"){
			document.getElementById("iDiv").className="tabStyle";
			document.getElementById("vDiv").className="aTabStyle";
			document.getElementById("tDiv").className="tabStyle";
			document.getElementById("advertisement_advt_type").value="vedio";
			document.getElementById("imageDiv").style.display = "none";
			document.getElementById("vedioDiv").style.display = "block";
			document.getElementById("textDiv").style.display = "none";
		}else if(advt_type = "text"){
			document.getElementById("iDiv").className="tabStyle";
			document.getElementById("vDiv").className="tabStyle";
			document.getElementById("tDiv").className="aTabStyle";
			document.getElementById("advertisement_advt_type").value="text";
			document.getElementById("imageDiv").style.display = "none";
			document.getElementById("vedioDiv").style.display = "none";
			document.getElementById("textDiv").style.display = "block";
		}
		
	}
	
	function changeAction(status){
		if(document.getElementById("active").checked == true){
			var active = 'true'
		}else{
			var active = 'false'
		}
		window.location = "/advertisements/advt_change_status/"+status+"?active="+active;
	}

	function expandFilterDiv(){
		if(document.getElementById("filterDiv").style.display == "none"){
			document.getElementById("filterDiv").style.display = "block"
		}else{
			document.getElementById("filterDiv").style.display = "none"
		}
		
	}
	
	function filterByDate(uid){
		var date_filter = document.getElementById("date_filter").value;
		var from_date = document.getElementById("from_date").value;
		var to_date = document.getElementById("to_date").value;
		if (date_filter == ""){
			if (to_date == "" && from_date == ""){
				alert("Please select any one of the filter criteria.");
				return;
			}else if((from_date == "" && to_date != "") || (from_date != "" && to_date == "")){
				alert("Please select both 'From' and 'To' 'Date'");
				return;
			}else if(to_date != "" && from_date != ""){
				var date = from_date + " and "+ to_date;
				window.location ="/impressions/filter_impression/"+uid+"?from_date="+from_date+"&to_date="+to_date;
			}else
				return;
		}else{
			window.location ="/impressions/filter_impression/"+uid+"?date_filter="+date_filter;
		}
	}

	function appFilterByDate(uid){
		var date_filter = document.getElementById("date_filter").value;
		var device_type = document.getElementById("device_type").value;
		var from_date = document.getElementById("from_date").value;
		var to_date = document.getElementById("to_date").value;
		var args = uid
		args = args + "?from_date=" + from_date + "&to_date=" + to_date+"&date_filter="+date_filter+"&device_type="+device_type;
		if ((from_date == "" && to_date != "") || (from_date != "" && to_date == "")) {
	 			alert("Please select both 'From' and 'To' 'Date'");
	 			return;
	 	}else	{
	  	window.location ="/impressions/app_filter_imp/"+args;
		}		
	}

	function selectAllAdvtCategory(size){
		if(size != "" && size != null){
			for(i=0;i<size;i++){
				var advt_category = "advt_category" + i;
				if (document.getElementById(advt_category).checked == true){
					document.getElementById(advt_category).checked = false;
				}else{
					document.getElementById(advt_category).checked = true;
				}
			}
		} 
	}
	
	function ActiveApplication(){
		if(document.getElementById("active").checked == true){
			var active = 'true'
		}else{
			var active = 'false'
		}
		window.location = "/applications/app_change_status?active="+active;
	}
	
	function selectAllAppCategory(size){
		if(size != "" && size != null){
			for(i=0;i<size;i++){
				var app_category = "app_category" + i;
				if (document.getElementById(app_category).checked == true){
					document.getElementById(app_category).checked = false;
				}else{
					document.getElementById(app_category).checked = true;
				}
			}
		} 
	}
	
	function showAndfilterChart(st_val, uid){
		if (st_val == null || st_val == ""){
			st_val = document.getElementById("st_val").value;
		}
		if(st_val == "" || st_val == null){
			st_val = 1;
		}
		document.getElementById("st_val").value=st_val;
		var device_type = document.getElementById("device_type").value;
		var from_date = document.getElementById("from_date").value;
		var to_date = document.getElementById("to_date").value;
		var args = uid
		args = args + "?from_date=" + from_date + "&to_date=" + to_date+"&device_type="+device_type+"&st_val="+st_val;
		if ((from_date == "" && to_date != "") || (from_date != "" && to_date == "")) {
			alert("Please select both 'From' and 'To' 'Date'");
			return;
		}
		else if (from_date != "" && to_date != "" && new Date(from_date) > new Date(to_date)) {
			alert("'To Date' must be greater than 'From Date'");
			return;
		}else	{
			if(st_val == "2"){
				
				new Ajax.Updater("chartDiv", "/advt_reports/advt_clk_report/"+args, {asynchronous:true, evalScripts:true});
				return;
			}else if(st_val == "1"){
				new Ajax.Updater("chartDiv", "/advt_reports/advt_imp_report_only/"+args, {asynchronous:true, evalScripts:true});
				return;
			}
	  	return;
		}		
	}
	
	function showAndfilterChartApp(st_val, uid){
		if (st_val == null || st_val == ""){
			st_val = document.getElementById("st_val").value;
		}
		if(st_val == "" || st_val == null){
			st_val = 1;
		}
		document.getElementById("st_val").value=st_val;
		var device_type = document.getElementById("device_type").value;
		var from_date = document.getElementById("from_date").value;
		var to_date = document.getElementById("to_date").value;
		var args = uid
		args = args + "?from_date=" + from_date + "&to_date=" + to_date + "&device_type=" + device_type + "&st_val=" + st_val;
		if ((from_date == "" && to_date != "") || (from_date != "" && to_date == "")) {
			alert("Please select both 'From' and 'To' 'Date'");
			return;
		}
		else 
			if (from_date != "" && to_date != "" && Date(from_date) > Date(to_date)) {
				alert("'To Date' must be greater than 'From Date'");
				return;
			}
			else {
				if (st_val == "2") {
					new Ajax.Updater("chartDiv", "/app_reports/app_clk_report/" + args, {
						asynchronous: true,
						evalScripts: true
					});
					return;
				}else if (st_val == "1") {
					new Ajax.Updater("chartDiv", "/app_reports/app_imp_report/" + args, {
						asynchronous: true,
						evalScripts: true
					});
					return;
				}else if(st_val == "3"){
					new Ajax.Updater("chartDiv", "/app_reports/app_revenue_report_only/" + args, {
						asynchronous: true,
						evalScripts: true
					});
					return;
				}else if(st_val == "4"){
					new Ajax.Updater("chartDiv", "/app_reports/app_ecpm_report/" + args, {
						asynchronous: true,
						evalScripts: true
					});
					return;
				}
					
				return;
			}
	}

	function checkCPM(){
		var advt_cpm = document.getElementById("advt_config_cpm").value;
		var cpm = document.getElementById("cpm").value;
		if(parseFloat(advt_cpm) < parseFloat(cpm)){
			document.getElementById("advt_config_cpm").value = cpm;
			alert("Please enter 'CPM' value greater than or equal to " + cpm);
			return;
		}else {
			var purch_amt = document.getElementById("master_data_purchase_amount").value;
			var impressions = (1000 / parseFloat(advt_cpm)) * parseFloat(purch_amt);
			document.getElementById("advt_config_max_impressions").value = parseInt(impressions);
			document.getElementById("impAmt").innerHTML = parseInt(impressions);
		}
	}
	
	function checkPurchaseAmount(){
		var purch_amt = document.getElementById("master_data_purchase_amount").value;
		var p_amt = document.getElementById("purchase_amount").value;
		if(parseFloat(purch_amt) < parseFloat(p_amt)){
			document.getElementById("master_data_purchase_amount").value = p_amt;
			alert("Please enter 'Purchase Amount' greater than or equal to " + p_amt);
			return;
		}else{
			var advt_cpm = document.getElementById("advt_config_cpm").value;
			var impressions = (1000 / parseFloat(advt_cpm)) * parseFloat(purch_amt);
			document.getElementById("advt_config_max_impressions").value = parseInt(impressions);
			document.getElementById("impAmt").innerHTML = parseInt(impressions);
		}
	}
	
	function reduceImp(){
		var advt_cpm = document.getElementById("advt_config_cpm").value;
		var ad_cpm = document.getElementById("advt_cpm").value;
		var cpm = document.getElementById("cpm").value;
		if(parseFloat(advt_cpm) < parseFloat(cpm)){
			document.getElementById("advt_config_cpm").value = ad_cpm;
			alert("Please enter 'CPM' value greater than or equal to " + cpm);
			return;
		}else {
			var balance_amt = document.getElementById("advt_config_balance_amount").value;
			var impressions = (1000 / parseFloat(advt_cpm)) * parseFloat(balance_amt);
			document.getElementById("rem_imp").value = parseInt(impressions);
			var imp_count = document.getElementById("imp_count").value;
			document.getElementById("advt_config_max_impressions").value = (parseInt(impressions) + parseInt(imp_count));
			document.getElementById("remImp").innerHTML = parseInt(impressions);
		}
	}
	
	function save_advt_details(){
		document.getElementById("butDiv").style.display="none"
		var domain = document.domain;
			var action = "";
			var tForm = document.getElementById('buyCreditForm');
//			if(domain == "localhost"){
//				action = "/advertisements/advt_save_details";
//			}
//			tForm.action = action;
			tForm.submit();
	}

	function setPubConfig(){
		var paypal_id = document.getElementById("publisher_paypal_id").value;
		if(paypal_id == ""){
			alert("Please enter 'Paypal Id'");
			return;
		}else{
			var tForm = document.getElementById("confForm");
			tForm.action="/publishers/pub_update_config";
			tForm.submit();
		}
	}

	function checkCPMRenew(){
		var advt_cpm = document.getElementById("advt_config_cpm").value;
		var cpm = document.getElementById("cpm").value;
		if(parseFloat(advt_cpm) < parseFloat(cpm)){
			document.getElementById("advt_config_cpm").value = cpm;
			alert("Please enter 'CPM' value greater than or equal to " + cpm);
			return;
		}else {
			var purch_amt = document.getElementById("master_data_purchase_amount").value;
			var impressions = (1000 / parseFloat(advt_cpm)) * parseFloat(purch_amt);
			var remaining = document.getElementById("remaining").value;
			document.getElementById("max_impression").value = parseInt(impressions);
//			impressions = parseInt(impressions) + parseInt(remaining);
			document.getElementById("impAmt").innerHTML = parseInt(impressions);
		}
	}

	function checkPurchaseAmountRenew(){
		var purch_amt = document.getElementById("master_data_purchase_amount").value;
		var p_amt = document.getElementById("purchase_amount").value;
		if(parseFloat(purch_amt) < parseFloat(p_amt)){
			document.getElementById("master_data_purchase_amount").value = p_amt;
			alert("Please enter 'Purchase Amount' greater than or equal to " + p_amt);
			return;
		}else{
			var advt_cpm = document.getElementById("advt_config_cpm").value;
			var impressions = (1000 / parseFloat(advt_cpm)) * parseFloat(purch_amt);
			var remaining = document.getElementById("remaining").value;
			document.getElementById("max_impression").value = parseInt(impressions);
//			impressions = parseInt(impressions) + parseInt(remaining);
			document.getElementById("impAmt").innerHTML = parseInt(impressions);
		}
	}	
	
	function changeAppType(id){
		if(id == "1"){
			document.getElementById("mobDiv").style.display="block";
			document.getElementById("webDiv").style.display="none";
		}else if(id == "2"){
			document.getElementById("mobDiv").style.display="none";
			document.getElementById("webDiv").style.display="block";
		}
	}
	
	function sendMailForPaypalId(size){
		var args = ""
		if(size != "" && size != null){
			for(i=0;i<size;i++){
				var sel_check = "sel_check" + i;
				if (document.getElementById(sel_check).checked == true){
					revn_rept = "revn_rept_" + i
					var r_id = document.getElementById(revn_rept).value;
					args = args == "" ?  r_id : args + "," + r_id
				}
			}
		} 
		if(args != "" && args != null){
			document.getElementById("paypalId").style.display="none"
			window.location="/report/mail_for_paypalid/"+args
		}else{
			alert("Sorry! There is no selected item found.");
			return;
		}
	}
	
	function ProcessPaypalTransaction(){
		document.getElementById("paypalTranId").style.display="none"
	}
	
	function searchRevSharing(id){
		document.getElementById("dtSel").style.display="none";
		if(id == "2"){
			document.getElementById("dtSel").style.display="block";
			var date_2i = document.getElementById("date__2i").value;
			var date_1i = document.getElementById("date__1i").value;
			var date_3i = document.getElementById("date__3i").value;
			var dis_count = document.getElementById("dis_count").value;
			if(date_2i !="" && date_1i != ""){
				window.location = "/report/pub_search_filter/"+id+"?date_1i="+date_1i+"&date_2i="+date_2i+"&date_3i="+date_3i+"&dis_count="+dis_count;
			}else
				return;
		}else
			window.location = "/report/pub_search_filter/"+id
	}
	
	function submitForm(frm, contrl, action){
		var tForm = document.getElementById(frm);
		tForm.action="/"+contrl+"/"+action;
		tForm.submit();
		
	}
	
	function filterBestAppPerfomace(uid){
		var app_id = document.getElementById("application_id").value;
		var start_year = document.getElementById("date__1i").value;
//		var end_year = document.getElementById("date__3i").value;
		var start_month = document.getElementById("date__2i").value;
//		var end_month = document.getElementById("date__4i").value;
		var args = uid
		args = args + "?from_year=" + start_year + "&from_month="+ start_month + "&serval_id=" + app_id;
//		if ((parseInt(start_year) > parseInt(end_year)) || ((parseInt(start_month) > parseInt(end_month)) && (parseInt(start_year) == parseInt(end_year)))) {
//			alert("'To Date' must be greater than 'From Date'");
//			return;
//		}else{
			window.location="/revenue_reports/search_app_performance/"+args;
//		}
	}

	function setCampaignValue(uid){
		if(uid == "0"){
			alert("Please select Campaign");
			return;
		}else
			window.location="/advertisements/advt_set_campaign/"+uid
	}
	
	function advtFilter(){
		var active =document.getElementById("active").value;
		var campaign_id =document.getElementById("campaign_id").value;
		window.location="/advertisements/advt_index/"+campaign_id+"/"+active;
	}

	function filterPurchaseAmt(){
		var month = document.getElementById("date__2i").value;
		var year = document.getElementById("date__1i").value;
		var count = document.getElementById("dis_count").value;
		
		window.location="/report/advt_report?month="+month+"&year="+year+"&count="+count
	}

	function checkSignupDiv(id){
		if(id == "1"){
			document.getElementById("signupDiv").style.display="block";
			document.getElementById("publisherDiv").style.display="none";
		}else{
			document.getElementById("signupDiv").style.display="none";
			document.getElementById("publisherDiv").style.display="block";
		}
	}

	function showImage(id){
		var showDiv = "showDiv" + id;
		document.getElementById(showDiv).style.display="block";
	}
	
	function hideImage(id){
		var showDiv = "showDiv" + id;
		document.getElementById(showDiv).style.display="none";
	}
	
	function changeStatusApproval(id){
		window.location='/advertisers/pending_approval?status='+id;
	}

	function showVedio(id){
		if(id == "1"){
			document.getElementById("vedioDiv").style.display="none"
		}else{
			document.getElementById("vedioDiv").style.display="block"
		}
	}
	
	function newAdvtPurchase(status){
		var tForm = document.getElementsByName('_xclick')[0];
		var action = "";
		if (status == "true"){
			tForm.action = "/advertisements/advt_save_details";						
		}else{
			tForm.action = "https://www.paypal.com/cgi-bin/webscr";
		}
		tForm.submit();
		return true;
	}
	

