/*
 * SimpleModal Basic Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: basic.js 212 2009-09-03 05:33:44Z emartin24 $
 *
 */

$(document).ready(function () {
	$('#basic-modal input.basic, #basic-modal a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal-content').modal();
	});
});

$(document).ready(function () {
	$('#basic-modal2 input.basic, #basic-modal2 a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal2-content').modal();
	});
});

$(document).ready(function () {
	$('#basic-modal3 input.basic, #basic-modal3 a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal3-content').modal();
	});
});

$(document).ready(function () {
	$('#basic-modal4 input.basic, #basic-modal4 a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal4-content').modal();
	});
});

$(document).ready(function () {
	$('#basic-modal5 input.basic, #basic-modal5 a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal5-content').modal();
	});
});

$(document).ready(function () {
	$('#basic-modal6 input.basic, #basic-modal6 a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal6-content').modal();
	});
});

$(document).ready(function () {
	$('#basic-modal7 input.basic, #basic-modal7 a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal7-content').modal();
	});
});

$(document).ready(function () {
	$('#basic-modal8 input.basic, #basic-modal8 a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal8-content').modal();
	});

});

$(document).ready(function() { 	
	var options = { 
		beforeSubmit:  validate,
        success:       showFeedBack  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#myForm').ajaxForm(options); 
}); 

$(document).ready(function() { 
	var options = { 
		beforeSubmit:  blockLogin,
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#myForm1').ajaxForm(options); 
}); 

$(document).ready(function() { 
	var options = { 
		beforeSubmit:  blockSearch,
        success:       showResults  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#searchForm').ajaxForm(options); 
}); 

$(document).ready(function() { 
	var options = { 
		beforeSubmit:  blockReview,
        success:       submitReview  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#reviewForm').ajaxForm(options); 
}); 

function blockLogin(formData, jqForm, options) { 
	var str = '<div id="question" style="display:block; cursor: default">Logging in....<br><br></div>';				
	$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
	return true;
}

function validateEmail(elementValue){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue);
 }

function validate(formData, jqForm, options) { 

	var email = $("#email").val();
	if (validateEmail(email)) {
	var str = '<div id="question" style="display:block; cursor: default">Saving....<br><br></div>';				
	$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
	return true;
	} else {
	 var str = '<div id="question" style="display:block; cursor: default">Please enter a valid E-Mail.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';				

		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 

			$.unblockUI(); 
			 $("#email").focus();
			return false; 
		}); 

		return false;


	}
	
}

function blockSearch(formData, jqForm, options) { 
	var str = '<div id="question" style="display:block; cursor: default">Posting Search....<br><br></div>';				
	$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
	return true;
}
function blockReview(formData, jqForm, options) { 
	var str = '<div id="question" style="display:block; cursor: default">Posting Review....<br><br></div>';				
	$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
	return true;
}

function showFeedBack(responseText,statusText) {
	if(responseText=="done"){
	 var str = '<div id="question" style="display:block; cursor: default">Thanks! We will get back to you soon with important Chorus updates and announcements. <br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';				

		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 
			 $('.simplemodal-close').trigger('click');

			$.unblockUI(); 
			return false; 
		}); 
	} else {
	 var str = '<div id="question" style="display:block; cursor: default">Something went wrong. Try again.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';				

		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 
			$("#email").focus();
			$.unblockUI(); 
			return false; 
		}); 


	}





}

function submitReview(responseText,statusText) {



	rsp = eval(responseText);
	if(rsp[0]['MyActions.rate']['RetVal'] == "ok") {

	 var str = '<div id="question" style="display:block; cursor: default">Reviewed Successfully.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';				

		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 
			 location.href=site+"search.php";

			$.unblockUI(); 
			return false; 
		}); 

	} else {
	 var error = rsp[0]['MyActions.rate']['ErrorCode'];
	 var str = '<div id="question" style="display:block; cursor: default">Problem reveiwing. Try Again.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';				

	 if (error=='100')
	 {
	 var str = '<div id="question" style="display:block; cursor: default">To post a review rating is required. Select a Rating and try reviewing again.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';		
		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 

			$.unblockUI(); 
			return false; 
		}); 

	 
	 } else if(error == '452') {
	 var str = '<div id="question" style="display:block; cursor: default">Invalid Session. Relogin.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';		
		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 
			 location.href=site+"logout.php";
			$.unblockUI(); 
			return false; 
		}); 
	 

	 }
	}
}

function showResults(responseText, statusText)  { 

	var rsp = eval(responseText);

	var retval = rsp[0]['Apps.search']['RetVal'];

	if(retval != "ok") {


	 var error = rsp[0]['Apps.search']['ErrorCode'];
	 var str = '<div id="question" style="display:block; cursor: default">Problem reveiwing. Try Again.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';				

	 if (error=='100')
	 {
	 var str = '<div id="question" style="display:block; cursor: default">To post a review rating is required. Select a Rating and try reviewing again.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';		
		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 

			$.unblockUI(); 
			return false; 
		}); 

	 
	 } else if(error == '452') {
	 var str = '<div id="question" style="display:block; cursor: default">Invalid Session. Relogin.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';		
		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 
			 location.href=site+"logout.php";
			$.unblockUI(); 
			return false; 
		}); 
	 

	 } else {
	 var str = '<div id="question" style="display:block; cursor: default">Problem searching. Try Again.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';		
		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 
			$.unblockUI(); 
			return false; 
		}); 
	 }

	} else {
		var status = rsp[0]['Apps.search']['StatusMessage'];
		if(status) {

			$("#search_results").html("<tr><td>&nbsp;</td></tr><tr><td>"+status+"</td></tr>");

		} else {

			var tonelist = rsp[0]['Apps.search']['Rsp']['tonelist'];
			var pagedata = rsp[0]['Apps.search']['Rsp']['pagedata'];
			var np = ''
			var sessionid = $("#session_var").val();
			var searchstr = $("#search_var").val();
			var url = $("#searchForm").attr("action");

			if(pagedata != null) {
				if(pagedata['fromidx']!=1) {
					fromidx = parseInt(pagedata['fromidx'])-rec_per_page;
					toidx = parseInt(pagedata['toidx'])-rec_per_page;
					np += '<td><a href="#" link="'+url+'?method=Apps.search&searchstr='+searchstr+'&sessionid='+sessionid+'&eauth=1&ct=CG&fromidx='+fromidx+'&toidx='+toidx+'" class="pagi" style="text-decoration:none;"><input type="button" class=button value="<< Prev"></a></td>';
				}
			}
			if(pagedata != null) {
				if(pagedata['toidx'] < pagedata['count']) {
					fromidx = parseInt(pagedata['fromidx'])+rec_per_page;
					toidx = parseInt(pagedata['toidx'])+rec_per_page;
					np += '<td><a href="#" link="'+url+'?method=Apps.search&searchstr='+searchstr+'&sessionid='+sessionid+'&eauth=1&ct=CG&fromidx='+fromidx+'&toidx='+toidx+'" class="pagi" style="text-decoration:none;"><input type="button" class=button value="Next >>"></a></td>';
				}
			}
			$("#search_results").html('<BR/>');

			if(np) {
				$("<tr>"+np+"</tr><tr><td>&nbsp;</td></tr>").appendTo("#search_results");
			}


			for(i in tonelist) {

				var name = tonelist[i].cname; 
				var price = tonelist[i].price; 
				var artist = tonelist[i].artist; 

				var art =  tonelist[i].albumart;
				var crefid = tonelist[i].crefid;

				var html = "<tr cellspacing='2' cellpadding='2'><td width=60px><img src='"+art+"' height=60 width=60></td><td width=50%><b style='font-weight:bold;font-style:italic;'>"+name+"</b><BR>"+artist+"<br><font color=white>"+price+"</font></td><td><a href='review.php?crefid="+crefid+"' class='button' style='text-decoration:none;color:white;padding:3px;'>Review</a></td></tr><tr><td>&nbsp;</td></tr>";
				$(html).appendTo("#search_results");

			}



			if(np) {
				$("<tr>"+np+"</tr><tr><td>&nbsp;</td></tr>").appendTo("#search_results");
			}

		 $(document).ready(function(){
				  $('a.pagi').bind('click',function(event){
					var str = '<div id="question" style="display:block; cursor: default">Loading Apps.<br><br> </div>';				
					$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 

					event.preventDefault();
					$.get($(this).attr('link'),{},function(response){ 
						showResults(response,'200');
						$.unblockUI(); 
					})	
				 })
		});
		}
		$.unblockUI(); 
	}
	
}





function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 

	var rsp = eval(responseText);

	var retval = rsp[0]['User.signIn']['RetVal'];

	if(retval != "ok") {
		url = site.match(/:\/\/(.[^/]+)/)[1];
		Set_Cookie( 'CHORUS','invalid', -5,'/',url);
		var str = '<div id="question" style="display:block; cursor: default">Login invalid. Try again.<br><br> <input type="button" id="ok" value="Ok" class="button" /></div>';				

		$.blockUI({ message: $(str), css: {padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: '1', width: '420px',color: '#d7b65f' } }); 
		 $('#ok').click(function() { 

			$.unblockUI(); 
			return false; 
		}); 
	} else {

		var sessionid=rsp[0]['User.signIn']['Rsp']['signin']['sessionid'];
		//alert(sessionid);
		Set_Cookie("sessionid",sessionid,"","/","honne");
		location.href=site+"search.php";





	}
 
} 

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}



