$(document).ready(function() {  
 
    //select all the a tag with name equal to modal
    $('a[name=showpolicydialog]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');		
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  
     
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
      
        //Set the popup window to center
        $('#dialog').css('top',  winH/2-$(id).height()/2);
        $('#dialog').css('left', winW/2-$(id).width()/2);
     
        //transition effect
        $('#dialog').fadeIn(2000); 
		$(window).scrollTop(0);
    });
     
    //if close button is clicked
    $('a[name=hidepolicydialg]').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
		$('#dialog, .policywindow').fadeOut("slow");
        $('#mask, .policywindow').fadeOut("slow");
    });     
     
    //if mask is clicked
    $('#mask').click(function () {		
        $(this).fadeOut("slow");
        $('.policywindow').fadeOut("slow");
    });         
		     
});

function doBooking(){	
	if(checkGuestInfo()){
		var currenturl=document.location.href;	
		protocol = window.location.protocol; 
		hostname = window.location.host ;
		pathname = window.location.pathname ;	
		//hotel_id= document.MyForm.hotel_id.value;
		//aff_id= document.MyForm.aff_id.value;
		pathname=pathname.replace('bookhp_step1','bookhp_step2')
		path=protocol+'//'+hostname+pathname;	
		document.form_hp_booking.action=path;
		document.form_hp_booking.submit();			
	}
}

function checkGuestInfo(){	
	var found=false;	
	var name_str = document.getElementById('jscheck').value;
	var new_string = name_str.substring(0,(name_str.length)-1);
	var fields=new_string.split("|");
	var i;
	for (i = 0; i < fields.length; i++){		
		if (document.getElementById(fields[i]).value == ""){			
			found=true;
			break;
		}	
	}
	var result=true;
	if (found){
		var tmp=fields[i].split("_");
		var msg=' field can not be left blank.';	
		if(tmp[1]=='first'){
			msg='First name'+msg;
		}else if (tmp[1]=='last'){
			msg='Last name'+msg;			
		}else if (tmp[1]=='email'){						
			msg='Email'+msg;			
		}		
		document.getElementById('jsalert').innerHTML=msg;
		document.getElementById('jsalert').style.display = "";
		document.getElementById(fields[i]).setAttribute("class", "FieldError");
		document.getElementById(fields[i]).focus();
		result= false;
	}
	var address = document.getElementById("guest_email").value;
	if(address!=""){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(address) == false) {			
			document.getElementById('jsalert').innerHTML='Invalid Email Address';
			document.getElementById('jsalert').style.display = "";
			document.getElementById("guest_email").setAttribute("class", "FieldError");
			document.getElementById("guest_email").focus();			
			result=false;
		}		
	}		
	return result;	
}

function changeclass(id){	
	if(document.getElementById(id).value!=""){
		document.getElementById(id).setAttribute("class", "");
	}else{
		document.getElementById(id).setAttribute("class", "FieldError");	
	}	
}

function showCancelationPopup(pid){
	var currenturl=document.location.href;	
	protocol = window.location.protocol; 
	hostname = window.location.host;	
	//pathname = window.location.pathname ;	
	//alert('protocol='+protocol+'  hostname='+hostname+' pathname='+pathname);
	//hotel_id= document.MyForm.hotel_id.value;
	//aff_id= document.MyForm.aff_id.value;
	//pathname=pathname.replace('bookhp_step1/','/cancelationpolicy?pid='+pid);
	path=protocol+'//'+hostname+'/cancelationpolicy?pid='+pid;	
	window.open(path,'CancelationPolicy', 'width=600, height=400, top=250, left=350');    		
}

function showVoucherPopup(pid){
	var currenturl=document.location.href;	
	protocol = window.location.protocol; 
	hostname = window.location.host ;
	pathname = window.location.pathname ;	
	//hotel_id= document.MyForm.hotel_id.value;
	//aff_id= document.MyForm.aff_id.value;
	pathname=pathname.replace('bookhp_step2/','showhpvoucher?pid='+pid);
	path=protocol+'//'+hostname+pathname;	
	window.open(path,'ShowVoucher', '');    		
}

function Submit2Secure(hc,sid,pid){
	var currenturl=document.location.href;
	hostname = window.location.host ;
	pathname = window.location.pathname ;
	hotel_id= document.MyForm.hotel_id.value;
	aff_id= document.MyForm.aff_id.value;
	var s_path='https://booking.roomseller.com/bookhp_step1/' + hotel_id+'/' + aff_id +'/?hc=' + hc + '&sid='+sid + '&pid=' + pid;	
	popupBookingSecureServer(s_path, 920, 800, 'yes', '');
    document.MyForm.action=s_path;
	document.MyForm.submit();
}

function popupBookingSecureServer(adresa, w, h, scr,title){ 
var l=(screen.availWidth-w)/2 , t=(screen.availHeight-h)/2;
//alert(title)
win=window.open(adresa,'PopupSevure',"width="+w+",height="+h+",left="+l+",top="+t+",statusbar=yes,toolbar=no,scrollbars="+scr+",navbar=no");
win.focus();
}

function Send_HP_Voucher(bookid){
	protocol = window.location.protocol; 
	hostname = window.location.host ;
	pathname = window.location.pathname ;
	path=protocol+'//'+hostname+'/send_hp_voucher/'+bookid+'/';
	window.open(path,'SendVoucher', 'width=600, height=400, top=250, left=350');
}

