// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// These functions are currently in use in 'my account' page in address book listing

//    function show_txt_box(id)
//    {   
//        $('span_' + id ).style.display = "none";
//        $('edit_' + id ).style.display = "none";
//        $('txt_' + id ).style.display = "";
//        $('save_' + id ).style.display = "";
//    }
//    function save_changes(id)
//    {   $('txt_' + id ).style.display = "none";
//        $('span_' + id).innerHtml = $('txt_' + id ).value;
//        $('span_' + id ).style.display = "";
//        $('save_' + id ).style.display = "none";
//        $('edit_' + id ).style.display = "";
//     //   <%#=remote_function(:url =>{ :action => :update_address_book }, :with=> "'address='+$F('txt_' + id))")%>
//    }
function pop_up_window(path)
{   
    features = "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=300,height=300,top=200,left=200"; 
    window.open (path,null,features);  
}
// END

 function is_number()
    {   val = $('choice_age').value
        for (var i=0; i < val.length; i++)
        {
            if ( val[i] < '0' || val[i] > '9')
            {   return false;                
            }                
        }  
        return true;
    }	


 function how_many_character_left(str,num,control)
 {	 	
	var ch_left = num - str.length;
	var html_text = ""
	if(ch_left == 1)	
	{
		html_text = "1 character remaining"
	}
	else if(ch_left > 1)
	{
		html_text = ch_left.toString() + " characters remaining";
	}
	else
	{
		html_text = "No character remaining";
	}	
	$(control).innerHTML = html_text;
 }
  

// Show, hide div's'
    
function show_div (obj)
{
    obj.style.display = "inline";
}
    
function hide_div (obj)
{
    obj.style.display = "none";
}
function load_cart(){
	new Ajax.Updater('shopping_cart', '/gift_wizard/shopping_cart', {asynchronous:true, evalScripts:true, onComplete:function(request){$('shopping_cart').style.display='block'; $('shopping_cart').style.left="22%"; $('shopping_cart').style.width="680px"; }});
}
function call_tell_friend(){
	new Ajax.Updater('tell_a_friend', '/tell_friend/new', {asynchronous:true, evalScripts:true, onComplete:function(request){$('tell_a_friend').style.top=get_browser_window_height()+'px'; $('tell_a_friend').style.display='block';}});
}
function get_browser_window_height(){
	var ie=document.all
	var body_start=(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	return (ie)? body_start.scrollTop : window.pageYOffset
}
function call_newsletter_signup(){
	new Ajax.Updater('newsletter_sign_up', '/newsletter_signup/new', {asynchronous:true, evalScripts:true, onComplete:function(request){$('newsletter_sign_up').style.display='block';}});
}
function call_generalsubscriber_signup(unique_id, free_sample, ret_url, analytics_string){
	var email_box = 'subs_email_'+unique_id ;
	var fname_box = 'subs_fname_'+unique_id ;
	var elm_email_box 	= $(email_box);
	var elm_fname_box 	= $(fname_box);
	var elm_enter_box 	= $('subs_enter_'+unique_id);
	var elm_msg_not		= $('subscribe_msg_not_'+unique_id);
	var elm_msg_success	= $('subscribe_msg_success_'+unique_id);
	var elm_msg_error	= $('subscribe_msg_error_'+unique_id);
	var elm_msg_wait	= $('subscribe_msg_wait_'+unique_id);
	
	if(elm_email_box!=null){ Validation.validate(email_box); }
	if(elm_fname_box!=null){ Validation.validate(fname_box); }
	if(elm_fname_box==null){ if(!Validation.validate(email_box)){ return false; } }
	else{ if(!Validation.validate(email_box) || !Validation.validate(fname_box)){ return false; } }
	
	elm_msg_not.style.display='none';elm_msg_success.style.display='none';elm_msg_error.style.display='none';elm_msg_wait.style.display='block';elm_enter_box.disabled=true;
	var param_string = 'subs_email='+encodeURIComponent(elm_email_box.value)+'&page_url='+encodeURIComponent(window.location);
	if (elm_fname_box!=null){ param_string += '&first_name='+encodeURIComponent(elm_fname_box.value); }
	if (free_sample){ param_string += '&free_sample=1'; }
	
	new Ajax.Request('/general_subscriber/new', {asynchronous:true, evalScripts:true, 
		onFailure:function(request){elm_msg_not.style.display='none';elm_msg_success.style.display='none';elm_msg_error.style.display='block';}, 
	 	onSuccess:function(request){if(request.responseText=='Not'){elm_msg_not.style.display='block';elm_msg_success.style.display='none';elm_msg_error.style.display='none';} 
			else{elm_msg_not.style.display='none';elm_msg_wait.style.display='none';elm_msg_error.style.display='none';elm_msg_success.style.display='block';
				elm_enter_box.disabled=false;elm_email_box.value='';if(elm_fname_box!=null){elm_fname_box.value='';}
				if(ret_url!=''){ window.location=ret_url;} if(analytics_string){_uacct = 'UA-2772878-1';urchinTracker('general_subscriber/new');}
			}
		}, parameters:param_string }); return false;
}
/*start for loading dropper*/
function call_loading_dropper(){
	if($('auto_popup_newsletter_sign_up')!=null){
		$('auto_popup_newsletter_sign_up').innerHTML='';
		$('auto_popup_newsletter_sign_up').className='display_none';
	}
	new Ajax.Updater('close_loading_dropper', '/newsletter_signup/loading_dropper', {asynchronous:true, evalScripts:true, onComplete:function(request){initbox();}});
}
//var show_popup = false;
//setTimeout('show_popup_reset()',2000);
//window.onclick = show_popup_escape();
//window.onclose = show_popup_closed();
var show_popup = false;//
function show_popup_reset(){
	//show_popup=true;
}
function show_popup_escape(){
	show_popup = false;
//	setTimeout('show_popup_reset()',2000);
}
function show_popup_closed(){
	if(show_popup==true){
		call_loading_dropper();
		return 'Before you go, if you like what you saw, have a coupon for 10% off. Simply tell us your email address below and we will send your coupon and timely alerts before major holidays when it is best to sign up for a Giftventure.';
	}
}
/*end for loading dropper*/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  //alert('X--- '+scrOfX);
  //alert('Y--- '+scrOfY);
  return [ scrOfX, scrOfY ];
}	
function change_states(country, state, obj_disable){
	var country_val = $(country).value
	var state_box_name = $(state).name.toString();
	var state_box_class = $(state).className.toString();
	if(!country_val.blank()){
		if(obj_disable!=null)
			obj_disable.disabled = true;
		new Ajax.Updater('state_selection_span', 
		'/state/get_states/'+country_val+'?state_id='+encodeURIComponent(state)+'&state_name='+encodeURIComponent(state_box_name)+'&state_class='+encodeURIComponent(state_box_class),
		 {asynchronous:true, evalScripts:true, onComplete:function(request){if(obj_disable!=null){obj_disable.disabled = false;}}} );
	}
}


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("Please enter a valid email address")
		return false
	 }

	 return true					
}