/*
	All scripts :amjith ps :11172008
	More info avilable on /coding_formats.pdf
*/

// this will protect variables from '$' conflit
// user jQuery insterd of '$' from now
// Example : $.post() is replaced by jQuery.post
// added by Amjith dated:12162008
jQuery.noConflict()

// function checks whether username is already exists in database.
function check_username_already_exists(){
	var username = jQuery('#email_id').val();
	
	if(username != ""){
		if(echeck(username)){
			jQuery('#check_username_already_exists').html('<img src="' + base_url + 'images/hprogress.gif" border="0" title="Loading.." alt="Loading.."/>');		
				
			jQuery.post(site_url + 'users/check_user_name_available', {user_name :username}, function(data) {
				if(data == 1){
					jQuery('#check_username_already_exists').html('<img src="' + base_url + 'images/error.png" border="0" title="Not available" alt="Not available"/> &nbsp;Username already exists<br/><a href="#">Retrive password</a>');
				}else{
					jQuery('#check_username_already_exists').html('<img src="' + base_url + 'images/check.gif" border="0" title="Available" alt="Available"/> &nbsp;Username available');		
				}
				//jQuery('#check_username_already_exists').hide('slow', 6000);
				//jQuery('#check_username_already_exists').html('sadasd');
			});
		}else{
			jQuery('#check_username_already_exists').html('<span class="error_msg">Username should be valid emailid.</span>');	
		}
	}else{
		jQuery('#email_id').focus();
	}
}
// function used to assign default value to the input box
function check_key_input(obj, val){
	if(obj.value == ""){
		obj.value = val;
	}
}
function check_key_input_blank(obj, val){
	if(obj.value == val){
		obj.value = "";
	}
}
// funtion to make inputs blank if they having default values
function validate_contact_form(){
	var mobile_n = jQuery('#mobile_number').val();
	var land_n = jQuery('#land_number').val();
	if(mobile_n == "Mobile number"){
		jQuery('#mobile_number').val("");
	}
	if(land_n == "Landphone number"){
		jQuery('#land_number').val("");
	}
}	

function show_div(id, anim){
	if (jQuery("#" + id).is(":hidden")) {
        jQuery("#" + id).slideDown(anim);
     } else {
        jQuery("#" + id).slideUp(anim);
     }

}

function check_registration_form(){
	jQuery('#error_msg').html('<img src="' + base_url + 'images/loading.gif" border="0">');
	if(!echeck(jQuery('#email').val())){
		jQuery('#error_msg').html('Invalid Email Id');
		return false;
	}else{
		jQuery.post( site_url + "users/check_emailid_exists" ,{ email_id: jquery('#email').val()}, function(data){
		   if(data == 1){
				jQuery('#error_msg').html('Same Email Id already registred');
				return false;
		   }
		});
	}
	return true;
}


// check Email Validation
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    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
		 }

 		 return true					
}
function clear_val_email_login(val, id){
	if(val == ""){
		jQuery("#" + id).val('Email Id');
	}else if(val == "Email Id"){
		jQuery("#" + id).val('');
	}
}	
function clear_val_password_login(val, id){
	if(val == ""){
		jQuery("#" + id).val('password');
	}else if(val == "password"){
		jQuery("#" + id).val('');
	}
}	

function selected_country(){
	
}

function getLineByLine(){
	
	 stringresult = document.getElementById('txtresult').value;	
	
	 var strReplaceAll = stringresult;

	 var intIndexOfMatch = strReplaceAll.indexOf( ", " );
	 // Loop over the string value replacing out each matching
	 // substring.
	 while (intIndexOfMatch != -1){
		 // Relace out the current instance.
		 strReplaceAll = strReplaceAll.replace( ", ", "\n" )
		  
		 // Get the index of any next matching substring.
		 intIndexOfMatch = strReplaceAll.indexOf( ", " );
	 }
	document.getElementById('txtresult').value = strReplaceAll;
	return false;
}

function getComaByComa(){
	
	 stringresult = document.getElementById('txtresult').value;	
	
	 var strReplaceAll = stringresult;

	 var intIndexOfMatch = strReplaceAll.indexOf( "\n" );
	 // Loop over the string value replacing out each matching
	 // substring.
	 while (intIndexOfMatch != -1){
		 // Relace out the current instance.
		 strReplaceAll = strReplaceAll.replace( "\n", ", " )
		  
		 // Get the index of any next matching substring.
		 intIndexOfMatch = strReplaceAll.indexOf( "\n" );
	 }
	document.getElementById('txtresult').value = strReplaceAll;
	return false;
}

function select_all(id){
	var text_val= document.getElementById(id)
	text_val.focus();
	text_val.select();
}

function write_txt(){
	jQuery("#loading").html('<img src="/idgrabber/img/loader.gif" alt="">');
	var data_s = document.getElementById('txtresult').value;
	jQuery.post("savetxt", { txt_data:data_s },
	   function(data){
	     //window.open ("../uploads/txt/" + data,"txt_download","menubar=1,resizable=1,width=350,height=350"); 
	     location.href = "txtdownloads/" + data;
		 jQuery("#loading").html("");
	   });
	   return false;
}
function write_pdf(){
	jQuery("#loading").html('<img src="/idgrabber/img/loader.gif" alt="">');
	var data_s = document.getElementById('txtresult').value;
	jQuery.post("savetxt", { txt_data:data_s },
	   function(data){
	     //window.open ("../uploads/txt/" + data,"txt_download","menubar=1,resizable=1,width=350,height=350"); 
		 //alert(data);
		 if(data){
				 location.href = "createpdf/" + data;
				 jQuery("#loading").html("");
			   }
		 
	   });
	   return false;
}
function disable_b(iid){
	jQuery("#"+iid).val("Please wait....");
	jQuery("#"+iid).attr("disabled", "disabled");
	
}
function check_form_invite(iid){
	if(jQuery("#invite_login").val() == ""){
		jQuery("#invite_login").focus();
	}
	jQuery("#"+iid).val("Please wait....");
	jQuery("#"+iid).attr("disabled", "disabled");
}

function updateEmailsExtracted(){
    jQuery('#total_email_id_extracted').load('emails/getemailcountajax');
}
setInterval( "updateEmailsExtracted()", 10000 );

/*End of page*/

