var isStateGood = 0;

function onBodyLoad(){
     isStateGood = 1;
     ajaxEngine.registerRequest('submitForm', 'ajaxback.php' );  
     ajaxEngine.registerRequest('submitForm2', 'ajaxback2.php' );      
     ajaxEngine.registerRequest('submitForm3', 'viewCommentsBack.php' );      
     ajaxEngine.registerRequest('submitUser', 'signUp.php' );     
     ajaxEngine.registerRequest('userLogin', 'login.php' ); 
     ajaxEngine.registerRequest('userLoginViewComments', 'login2.php' ); 
     ajaxEngine.registerRequest('userSignUpViewComments', 'signUp2.php' ); 
     ajaxEngine.registerRequest('saveUserComment', 'saveComment.php' ); 
     ajaxEngine.registerRequest('userLoginMainPage', 'mainlogin.php' ); 
     ajaxEngine.registerRequest('userSignUpMainPage', 'mainsignup.php' );
     ajaxEngine.registerRequest('forgotPassword', 'forgotpass.php' );
     ajaxEngine.registerRequest('logout', 'logout.php' );
     ajaxEngine.registerRequest('logout2', 'logout2.php' );
     ajaxEngine.registerRequest('addToFavs', 'addToFavs.php' );
     ajaxEngine.registerRequest('removeFromFavs', 'removeFavs.php' );
}

function submitForm(rating, msg, vote, stars, gallery){	 
     if(isStateGood){
	     ajaxEngine.registerAjaxElement(msg);     
	     ajaxEngine.registerAjaxElement(vote);     
	     ajaxEngine.registerAjaxElement(stars);     
	     ajaxEngine.sendRequest('submitForm', 'rating=' + rating, 'divTag=' + msg, 'voteTag=' + vote, 'stars=' + stars, 'gallery=' + gallery);    
    	     return false;
      }
      else{
      	alert('Please wait for page to finish loading to rate a gallery');
      	return false;
      }
}

function setInnerText(image, gallery){
	ajaxEngine.sendRequest('submitForm2', 'flag='+0, 'theimage='+image, 'gallery='+gallery);	
}

function setInnerTextViewComments(image, gallery){
	ajaxEngine.sendRequest('submitForm3', 'flag='+0, 'theimage='+image, 'gallery='+gallery);	
}

function setInnerTextClose(image, gallery){	
	ajaxEngine.sendRequest('submitForm2', 'flag='+ 1, 'theimage='+image, 'gallery='+gallery);	
}
  
  
function clearCommentBox(){	
	document.getElementById('addCommentBox').value='';
}

function both(image, gallery){ 
	if(isStateGood){
		ajaxEngine.registerAjaxElement(image); 
		new Rico.Effect.Size(image, 240, 180, 200, 30);
		new Rico.Effect.FadeTo(image, .8, 500, 10);  	
		setTimeout("setInnerText(\""+image+"\",\""+gallery+"\")", 400);  
	}
	else{
		alert('Please wait for page to finish loading to add a comment');
	}
}

function viewCommentsGrow(image, gallery){ 
	if(isStateGood){
		ajaxEngine.registerAjaxElement(image); 
		new Rico.Effect.Size(image, 240, 180, 200, 30);
		new Rico.Effect.FadeTo(image, .8, 500, 10);  	
		setTimeout("setInnerTextViewComments(\""+image+"\",\""+gallery+"\")", 400);  
	}
	else{
		alert('Please wait for page to finish loading to add a comment');
	}
}

function closeComment(image, gallery){  	
	new Rico.Effect.Size(image, 240, 15, 200, 30);
	new Rico.Effect.FadeTo(image, 1, 500, 10);
	setInnerTextClose(image, gallery);    	
}

function viewComments(gallery){
	window.location="viewcomments.php?gallery="+gallery;
}

function changeText(){	
	document.getElementById('commentBox').value='';
}

function addComment2(gallery){
	if(document.getElementById('addCommentBox').value==""){
		document.getElementById('addCommentBox').value="Add Comment Here";
	}
	else if(document.getElementById('addCommentBox').value=="Add Comment Here"){
		// remain the same
	}else{		
		var commentText=document.getElementById('addCommentBox').value;
		window.location="saveComment2.php?gallery="+gallery+"&addCommentBox="+commentText;		
	}
	
}

function reportSpam(comment, gallery){
	var answer = confirm("Are you sure you want to report this comment as spam?")
	if (answer){			
		window.location="reportSpam.php?comment="+comment+"&gallery="+gallery;
	}
	else{

	}
}

function signUp(image, gallery){
	ajaxEngine.sendRequest('submitForm2', 'flag=2', 'theimage='+image, 'gallery='+gallery);		
}

function signUpViewComments(divTag, gallery){
	if(isStateGood){		
		var username=document.getElementById('mainusername').value;
		var password=document.getElementById('mainpassword').value;
		var email=document.getElementById('mainemail').value;		
		ajaxEngine.registerAjaxElement(divTag);
		ajaxEngine.registerAjaxElement('mainLogin');
		ajaxEngine.registerAjaxElement('welcomeSet');
		ajaxEngine.sendRequest('userSignUpViewComments', 'theimage='+divTag, 'username='+username, 'password='+password, 'email='+email, 'gallery='+gallery);	
	}
	else{
		alert('Please wait for page to finish loading before registering');
	}
	
}

function signUpMainPage(divTag){
	if(isStateGood){		
		var username=document.getElementById('mainusername').value;
		var password=document.getElementById('mainpassword').value;
		var email=document.getElementById('mainemail').value;		
		ajaxEngine.registerAjaxElement(divTag);
		ajaxEngine.registerAjaxElement('welcomeSet');		
		ajaxEngine.sendRequest('userSignUpMainPage', 'theimage='+divTag, 'username='+username, 'password='+password, 'email='+email);	
	}
	else{
		alert('Please wait for page to finish loading before registering');
	}
	
}


function displayRegistrationViewComments(divTag, gallery){	
	document.getElementById(divTag).innerHTML='<table width="140" border="0" align="right" cellpadding="0" cellspacing="1"><tr>'+	
	'<td><div align="left" class="style4"><font color="white">Username:</font></div></td>'+
	'</tr><tr><td><div align="left"><label><input name="mainusername" id="mainusername" type="text" maxlength="12" size="17" />'+
	'</label></div></td></tr><tr><td><div align="left"><span class="style4"><font color="white">Password:</font></span></div></td>'+
	'</tr><tr><td><div align="left"><input name="mainpassword" id="mainpassword" type="password" size="17" maxlength="12" />'+
	'</div></td></tr>'+
	'<tr><td><div align="left"><span class="style4"><font color="white">Email:</font></span></div></td>'+
	'</tr><tr><td><div align="left"><input name="mainemail" id="mainemail" type="email" size="17" maxlength="50" />'+
	'</div></td></tr>'+
	'<tr><td><div align="left"><table width="100" border="0" cellspacing="0" cellpadding="0">'+
	'<tr><td align="left"><div align="right"><label><div align="left">'+
	'<input type="submit" onClick="javascript:signUpViewComments(\'viewBox\', '+gallery+')" class="btn" name="Submit2" value="Sign Up" />'+
	'</div></label></div></td><td width="3">&nbsp;</td><td><div align="left"><label>'+
	'<input type="submit" onClick="javascript:displayLoginViewComments(\'mainLogin\', '+gallery+')" class="btn" name="Submit" value="Cancel" />'+
	'</label></div></td></tr></table></div></td></tr></table>';
}

function displayLoginViewComments(divTag, gallery){	
	document.getElementById(divTag).innerHTML='<table width="140" border="0" align="right" cellpadding="0" cellspacing="1"><tr>'+
	'<td><div align="left" class="style4"><font color="white">Username:</font></div></td>'+
	'</tr><tr><td><div align="left"><label><input name="mainusername" id="mainusername" type="text" maxlength="12" size="17" />'+
	'</label></div></td></tr><tr><td><div align="left"><span class="style4"><font color="white">Password:</font></span></div></td>'+
	'</tr><tr><td><div align="left"><input name="mainpassword" id="mainpassword" type="password" size="17" maxlength="12" />'+
	'</div></td></tr>'+
	'<tr><td><div align="left"><table width="100" border="0" cellspacing="0" cellpadding="0">'+
	'<tr><td align="left"><div align="right"><label><div align="left">'+
	'<input type="submit" onClick="javascript:userLoginViewComments(\'viewbox\', '+gallery+')" class="btn" name="Submit2" value="Log In" />'+
	'</div></label></div></td><td width="3">&nbsp;</td><td><div align="left"><label>'+
	'<input type="submit" onClick="javascript:displayRegistrationViewComments(\'mainLogin\', '+gallery+')" class="btn" name="Submit" value="Sign Up" />'+
	'</label></div></td></tr><tr><td colspan="3"><table width="140" border="0" cellspacing="0" cellpadding="0">'+
	'<tr><td width="3">&nbsp;</td>'+
	'<td><div align="left"><a href="#" onClick="javascript:forgotPassword(\'mainLogin\');" class="style7">Forget Your Password?</a></div></td>'+
	'</tr></table></div></td></tr></table></div></td></tr></table>';
}

function displayRegistrationMainPage(divTag){	
	document.getElementById(divTag).innerHTML='<table width="140" border="0" align="right" cellpadding="0" cellspacing="1"><tr>'+	
	'<td><div align="left" class="style4"><font color="white">Username:</font></div></td>'+
	'</tr><tr><td><div align="left"><label><input name="mainusername" id="mainusername" type="text" maxlength="12" size="17" />'+
	'</label></div></td></tr><tr><td><div align="left"><span class="style4"><font color="white">Password:</font></span></div></td>'+
	'</tr><tr><td><div align="left"><input name="mainpassword" id="mainpassword" type="password" size="17" maxlength="12" />'+
	'</div></td></tr>'+
	'<tr><td><div align="left"><span class="style4"><font color="white">Email:</font></span></div></td>'+
	'</tr><tr><td><div align="left"><input name="mainemail" id="mainemail" type="email" size="17" maxlength="50" />'+
	'</div></td></tr>'+
	'<tr><td><div align="left"><table width="100" border="0" cellspacing="0" cellpadding="0">'+
	'<tr><td align="left"><div align="right"><label><div align="left">'+
	'<input type="submit" onClick="javascript:signUpMainPage(\'mainLogin\')" class="btn" name="Submit2" value="Sign Up" />'+
	'</div></label></div></td><td width="3">&nbsp;</td><td><div align="left"><label>'+
	'<input type="submit" onClick="javascript:displayLoginMainPage(\'mainLogin\')" class="btn" name="Submit" value="Cancel" />'+
	'</label></div></td></tr></table></div></td></tr></table>';
}


function displayLoginMainPage(divTag){	
	document.getElementById(divTag).innerHTML='<table width="140" border="0" align="right" cellpadding="0" cellspacing="1"><tr>'+
	'<td><div align="left" class="style4"><font color="white">Username:</font></div></td>'+
	'</tr><tr><td><div align="left"><label><input name="mainusername" id="mainusername" type="text" maxlength="12" size="17" />'+
	'</label></div></td></tr><tr><td><div align="left"><span class="style4"><font color="white">Password:</font></span></div></td>'+
	'</tr><tr><td><div align="left"><input name="mainpassword" id="mainpassword" type="password" size="17" maxlength="12" />'+
	'</div></td></tr>'+
	'<tr><td><div align="left"><table width="100" border="0" cellspacing="0" cellpadding="0">'+
	'<tr><td align="left"><div align="right"><label><div align="left">'+
	'<input type="submit" onClick="javascript:userLoginMainPage(\'mainLogin\')" class="btn" name="Submit2" value="Log In" />'+
	'</div></label></div></td><td width="3">&nbsp;</td><td><div align="left"><label>'+
	'<input type="submit" onClick="javascript:displayRegistrationMainPage(\'mainLogin\')" class="btn" name="Submit" value="Sign Up" />'+
	'</label></div></td></tr><tr><td colspan="3"><table width="140" border="0" cellspacing="0" cellpadding="0">'+
	'<tr><td width="3">&nbsp;</td>'+
	'<td><div align="left"><a href="#" onClick="javascript:forgotPassword(\'mainLogin\');" class="style7">Forget Your Password?</a></div></td>'+
	'</tr></table></div></td></tr></table></div></td></tr></table>';
}



function userLogin(image, gallery){
	var username=document.getElementById('username').value;
	var password=document.getElementById('password').value;	
	ajaxEngine.registerAjaxElement('mainLogin');
	ajaxEngine.registerAjaxElement('welcomeSet');
	ajaxEngine.sendRequest('userLogin','theimage='+image, 'username='+username,'password='+password,'gallery='+gallery);	
}

function userLoginViewComments(divTag, gallery){
	var username=document.getElementById('mainusername').value;
	var password=document.getElementById('mainpassword').value;	
	if(isStateGood){
		ajaxEngine.registerAjaxElement(divTag);
		ajaxEngine.registerAjaxElement('mainLogin');
		ajaxEngine.registerAjaxElement('welcomeSet');
		ajaxEngine.sendRequest('userLoginViewComments','theimage='+divTag, 'username='+username,'password='+password,'gallery='+gallery);	
	}
	else{
		alert('Please wait for page to finish loading before logging in');
	}	
}


function userLoginMainPage(divTag){
	var username=document.getElementById('mainusername').value;
	var password=document.getElementById('mainpassword').value;	
	if(isStateGood){		
		ajaxEngine.registerAjaxElement(divTag);
		ajaxEngine.registerAjaxElement('welcomeSet');
		ajaxEngine.sendRequest('userLoginMainPage','theimage='+divTag, 'username='+username,'password='+password);		
	}
	else{
		alert('Please wait for page to finish loading before logging in');
	}	
}

function submitUser(image, gallery){	
	var username=document.getElementById('username').value;
	var password=document.getElementById('password').value;
	var email=document.getElementById('email').value;
	ajaxEngine.registerAjaxElement('mainLogin');
	ajaxEngine.registerAjaxElement('welcomeSet');
	ajaxEngine.sendRequest('submitUser', 'theimage='+image, 'username='+username, 'password='+password, 'email='+email, 'gallery='+gallery);	
}

function saveComment(image, gallery){		
	var commentText=document.getElementById('commentBox').value;	
	ajaxEngine.sendRequest('saveUserComment', 'theimage='+image, 'gallery='+gallery, 'comment='+commentText);
}

function displayLogin(divTag, gallery){
	document.getElementById(divTag).innerHTML='<b><span class="style6">Enter your login information below:</span></b><br /><br /><table>'+
	'<tr><td class="style1"><font color="white"><b>Username:</b></font></td><td><input type="textfield" maxlength="12" id="username" name="username" /></td></tr>'+
	'<tr><td class="style1"><font color="white"><b>Password:</b></font></td><td><input type="password" maxlength="12" id="password" name="password" /></td></tr>'+	
	'<tr><td colspan="2"></td></tr>'+
	'<tr><td colspan="2" align="center"><input type="button" value="Login" onClick="javascript:userLoginViewComments(\''+divTag+'\','+gallery+')" class="btn" /> <input type="button" value="Cancel" class="btn" onClick="javascript:displayTheDefault(\''+divTag+'\','+gallery+')" /></td></tr>'+	
	'</table><br /><span class="style1"><a href="#" onClick="javascript:displayRegistration(\''+divTag+'\', '+gallery+'); return false;"><font color="white">Register With The SauceBox Bros Network</font></a></span>';		
}

function displayRegistration(divTag, gallery){
	document.getElementById(divTag).innerHTML='<b><span class="style6">Enter your information below</span></b><br /><br /><table>'+	
	'<tr><td class="style1"><font color="white"><b>Username:</b></font></td><td><input type="textfield" id="username" maxlength="12" name="username" /></td></tr>'+
	'<tr><td class="style1"><font color="white"><b>Password:</b></font></td><td><input type="password" id="password" maxlength="12" name="password" /></td></tr>'+
	'<tr><td class="style1"><font color="white"><b>Email:</b></font></td><td><input type="textfield" id="email" name="email" /></td></tr>'+
	'<tr><td colspan="2"></td></tr>'+
	'<tr><td colspan="2" align="center"><input type="button" onClick="javascript:signUpViewComments(\''+divTag+'\','+gallery+');" value="Sign Up" class="btn" /> <input type="button" value="Cancel" class="btn" onClick="javascript:displayTheDefault(\''+divTag+'\','+gallery+')" /></td></tr>'+
	'</table>';
}

function displayTheDefault(divTag, gallery){	
	document.getElementById(divTag).innerHTML='<span class="style6"><span class="style17">Would you like to comment? </span><br />'+
	'Join <a href="#" onClick="javascript:displayRegistration(\''+divTag+'\', '+gallery+'); return false;" title="addcomment">Saucebox Bros Free Network</a>,<br />'+
        'or <a href="#" onClick="javascript:displayLogin(\''+divTag+'\', '+gallery+'); return false;">Login</a> if you are already a member. </span>';
}


function sendPassword(divTag){
	if(isStateGood){
		var username=document.getElementById('forgotusername').value;		
		ajaxEngine.registerAjaxElement(divTag);
		ajaxEngine.sendRequest('forgotPassword', 'username='+ username,'divTag=' + divTag);    
	}
	else{
		alert('Please wait for page to finish loading before making this request');
	}
}

function forgotPassword(divTag){
	document.getElementById(divTag).innerHTML='<table width="140" cellpadding="0" border="0"><tr><td class="style1" align="left"><font color="white"><b>Username:</b></font></tr><tr></td><td align="left"><input type="textfield" id="forgotusername" size="17" maxlength="12" name="forgotusername" /></td></tr>'+
	'<tr><td colspan="2"></td></tr>'+
	'<tr><td colspan="2" align="left"><input type="button" value="Email It" onClick="javascript:sendPassword(\''+divTag+'\')" class="btn" /> <input type="button" value="Cancel" class="btn" onClick="javascript:displayLoginMainPage(\''+divTag+'\')" /></td></tr>'+	
	'</table>';			
}

function logout(divTag){
	if(isStateGood){
		ajaxEngine.registerAjaxElement(divTag);	
		ajaxEngine.registerAjaxElement('welcomeSet');
		ajaxEngine.sendRequest('logout','theimage='+divTag);	
	}
	else{
		alert('Please wait for page to finish loading before logging out');
	}
}

function logout2(divTag, gallery){
	if(isStateGood){
		ajaxEngine.registerAjaxElement(divTag);	
		ajaxEngine.registerAjaxElement('welcomeSet');
		ajaxEngine.registerAjaxElement('viewBox');
		ajaxEngine.sendRequest('logout2','theimage='+divTag, 'gallery='+gallery);	
	}
	else{
		alert('Please wait for page to finish loading before logging out');
	}
}

function logout3(){
	window.location="logout3.php";
}


function contactUs(){
	if(document.getElementById('name').value==""){
		alert('You must fill out your name');
		document.getElementById('name').focus();
		return false;
	}
	else if(document.getElementById('email').value==""){
		alert('You must fill out your email address');
		document.getElementById('email').focus();
		return false;
	} 
	else if(document.getElementById('subject').value==""){
		alert('You must fill out a subject');
		document.getElementById('subject').focus();
		return false;
	} 
	else if(document.getElementById('message').value==""){
		alert('You have left the message area blank');
		document.getElementById('message').focus();
		return false;
	} 
	else {
		var answer = confirm("Are you sure you want to send this message?");
		if (answer){				
			return true;
		}
		else{
			return false;
		}				
	}
	
}

function retrieveFavorites(){
	window.location="favorites.php";
}

function addToFavorites(galleryId, favoriteDiv){
	if(isStateGood){	
		ajaxEngine.registerAjaxElement(favoriteDiv);
		ajaxEngine.sendRequest('addToFavs', 'gallery='+galleryId, 'favoriteDiv='+favoriteDiv);	
	}
	else{
		alert('Please wait for page to finish loading before adding to your favorites');
	}	
}

function removeFromFavorites(galleryId, imageDiv, favoriteDiv, courtesyDiv){
	if(isStateGood){	
		ajaxEngine.registerAjaxElement(imageDiv);
		ajaxEngine.registerAjaxElement(favoriteDiv);
		ajaxEngine.registerAjaxElement(courtesyDiv);
		document.getElementById(imageDiv).innerHTML=' ';
		document.getElementById(courtesyDiv).innerHTML=' ';
		new Rico.Effect.Size(imageDiv, 240, 180, 200, 30);
		new Rico.Effect.FadeTo(imageDiv, 1, 500, 10); 
		ajaxEngine.sendRequest('removeFromFavs', 'gallery='+galleryId, 'favoriteDiv='+favoriteDiv);	
	}
	else{
		alert('Please wait for page to finish loading before removing from your favorites');
	}	
}



