/*
J  a  v  a  c  a  t  z

Description:

Tel: (852)29877951
Fax: (852)29879294
Email: office@javacatz.com
Website: www.javacatz.com

Created: 2006-07-28
*/

function whitespace_only(str) {
  var whitespace = " \n\r\t";
  for (var i = 0; i < str.length; i++) {
    current_char = str.charAt(i);
    if (whitespace.indexOf(current_char) == -1) {
      return false;
    }
  }
  return true;
}

function checkEmail(str) {
	var at = "@";
	var dot = ".";
	var index_at = str.indexOf(at);
	var str_length = str.length;
	var index_dot = str.indexOf(dot);
	var last_index_dot = str.lastIndexOf(dot);

	if( index_at == -1 ) return false; 
	if( index_at==-1 || index_at==0 || index_at==str_length ) return false;
	if( index_dot==-1 || index_dot==0 || index_dot==str_length || last_index_dot==str_length-1 ) return false;
	if( str.indexOf(at,(index_at+1)) != -1 ) return false;
	if( str.substring(index_at-1,index_at)==dot || str.substring(index_at+1,index_at+2)==dot ) return false;
	if( str.indexOf(dot,(index_at+2)) == -1 ) return false;
	if( str.indexOf(" ") != -1 ) return false;

	return true;
}


/* CONTACT US FORM */

function ajaxCheckReason(relLoc) {
	var reason = escape($F('reason'));
	if(reason=="") $('ajaxReason').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else if(whitespace_only($F('reason'))) $('ajaxReason').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxReason').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}

function ajaxCheckTitle(relLoc) {
	var title = escape($F('title'));
	if(title=="") $('ajaxTitle').innerHTML = "<span class='grayout'>(Optional)</span>";
	else if(whitespace_only($F('title'))) $('ajaxTitle').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxTitle').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}

function ajaxCheckCompany(relLoc) {
	var company = escape($F('company'));
	if(company=="") $('ajaxCompany').innerHTML = "<span class='grayout'>(Optional)</span>";
	else if(whitespace_only($F('company'))) $('ajaxCompany').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxCompany').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}

function ajaxCheckFullName(relLoc) {
	var fullname = escape($F('fullname'));
	if(fullname=="") $('ajaxFullName').innerHTML = "<span class='redout'>(Required)</span>";
	else if(whitespace_only($F('fullname'))) $('ajaxFullName').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxFullName').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}

function ajaxCheckFirstName(relLoc) {
	var firstname = escape($F('firstname'));
	if(firstname=="") $('ajaxFirstName').innerHTML = "<span class='redout'>(Required)</span>";
	else if(whitespace_only($F('firstname'))) $('ajaxFirstName').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxFirstName').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}

function ajaxCheckLastName(relLoc) {
	var lastname = escape($F('lastname'));
	if(lastname=="") $('ajaxLastName').innerHTML = "<span class='redout'>(Required)</span>";
	else if(whitespace_only($F('lastname'))) $('ajaxLastName').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxLastName').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}

function ajaxCheckPhone(relLoc) {
	var phone = escape($F('phone'));
	if(phone=="") $('ajaxPhone').innerHTML = "<span class='redout'>(Required)</span>";
	else if(whitespace_only($F('phone'))) $('ajaxPhone').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxPhone').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}

function ajaxCheckEmail(relLoc) {
	var email = escape($F('email'));
	if(email=="") $('ajaxEmail').innerHTML = "<span class='redout'>(Required)</span>";
//	else if(checkEmail($F('email'))) $('ajaxEmail').innerHTML = "<img src='"+relLoc+"img/forms/ajax-red-check.gif' alt='' width='15' height='12' /> <span class='redout'>Pending verification</span>";
	else if(checkEmail($F('email'))) $('ajaxEmail').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
	else $('ajaxEmail').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
}

function ajaxCheckMessage(relLoc) {
	var message = escape($F('message'));
	if(message=="") $('ajaxMessage').innerHTML = "<span class='redout'>(Required)</span>";
	else if(whitespace_only($F('message'))) $('ajaxMessage').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxMessage').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}

function ajaxCheckMessageOptional(relLoc) {
	var message = escape($F('message'));
	if(message=="") $('ajaxMessage').innerHTML = "<span class='grayout'>(Optional)</span>";
	else if(whitespace_only($F('message'))) $('ajaxMessage').innerHTML = "<img src='"+relLoc+"img/forms/ajax-cross.gif' alt='' width='12' height='12' />";
	else $('ajaxMessage').innerHTML = "<img src='"+relLoc+"img/forms/ajax-check.gif' alt='' width='15' height='12' />";
}




/* CONTACT FORM */

function ajaxContactPreloader() {
	$('formFields').style.display = "none";
//	$('contact_system').style.display = "none";
	$('preloader').style.display = "block";
	return true;
}

function showContactSubmitResponse(originalRequest) {
	if (originalRequest.responseText.indexOf("Your information has been submitted successfully.") != (-1)) $('contacttext').style.display = "none";
	else $('contacttext').style.display = "block";
	$('preloader').style.display = "none";
//	$('contact_system').style.display = "block";
//	$('contact_system').innerHTML = originalRequest.responseText;
}

function ajaxContactSubmit() {
	var url = 'ajaxContactSubmit.php';
	var pars = 'reason='+escape($F('reason'))+'&title='+escape($F('title'))+'&company='+escape($F('company'))+'&firstname='+escape($F('firstname'))+'&lastname='+escape($F('lastname'))+'&phone='+escape($F('phone'))+'&email='+escape($F('email'))+'&message='+escape($F('message'));
	new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showContactSubmitResponse});
}




/* NEWSLETTER SUBSCRIPTION */

function ajaxNewsSubscribePreloader() {
	$('subscribe_form').style.display = "none";
	$('subscribe_email_system').style.display = "none";
	$('subscribe_email_preloader').style.display = "block";
	return true;
}

function showNewsSubscribeSubmitResponse(originalRequest) {
	$('subscribe_email_success').style.display = "none";
	$('unsubscribe_email_success').style.display = "none";
	$('subscribe_email_failure').style.display = "none";

	if (originalRequest.responseText.indexOf("Below is what you submitted to international-subscribe@hongkong-chefs.com on") != (-1)) {
		$('subscribe_form').style.display = "block";
		$('subscribe_email_success').style.display = "block";
	} else if (originalRequest.responseText.indexOf("Below is what you submitted to international-unsubscribe@hongkong-chefs.com") != (-1)) {
		$('subscribe_form').style.display = "block";
		$('unsubscribe_email_success').style.display = "block";
	} else {
		$('subscribe_form').style.display = "block";
		$('subscribe_email_failure').style.display = "block";
	}
	$('subscribe_email_preloader').style.display = "none";
	$('subscribe_email_system').style.display = "block";
/*
	var generator=window.open('','name','height=400,width=500');
	generator.document.write(originalRequest.responseText);
	generator.document.close();
*/
}

function ajaxNewsSubscribeSubmit(status) {
	var url = 'cgi-bin/formmail.pl';
	if(status=='Subscribe') {
		var pars = 'subject='+escape($F('subject'))+'&email='+escape($F('subscribe_email'))+'&recipient='+escape($F('recipient_subscribe'))+'&submit='+escape($F('submit'));
		$('subscribe_email_button').disabled = "true";
	} else if(status=='Unsubscribe') {
		var pars = 'subject='+escape($F('subject'))+'&email='+escape($F('unsubscribe_email'))+'&recipient='+escape($F('recipient_unsubscribe'))+'&submit='+escape($F('submit'));
		$('unsubscribe_email_button').disabled = "true";
	}
	new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showNewsSubscribeSubmitResponse});
}



/* EVENT SIGNUP */

function ajaxSignupPreloader() {
	$('signuptext').style.display = "none";
	$('signup_system').style.display = "none";
	$('preloader').style.display = "block";
	return true;
}

function showSignupSubmitResponse(originalRequest) {
	if (originalRequest.responseText.indexOf("Your sign up request has been sent successfully.") != (-1)) {
		$('signuptext').style.display = "none";
	} else $('signuptext').style.display = "block";
	$('preloader').style.display = "none";
	$('signup_system').style.display = "block";
	$('signup_system').innerHTML = originalRequest.responseText;
}

function ajaxSignupSubmit(relLoc) {
	var url = relLoc+'ajaxSignupSubmit.php';
	var pars = 'fullname='+escape($F('fullname'))+'&phone='+escape($F('phone'))+'&email='+escape($F('email'))+'&message='+escape($F('message'))+'&subject='+escape($F('subject'));
	new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showSignupSubmitResponse});
}



/* SUBMIT NEWS TO US */

function ajaxNewsSubmissionPreloader() {
	$('newssubmittext').style.display = "none";
	$('newssubmit_system').style.display = "none";
	$('preloader').style.display = "block";
	return true;
}

function showNewsSubmissionSubmitResponse(originalRequest) {
	if (originalRequest.responseText.indexOf("Your submission has been sent successfully.") != (-1)) {
		$('newssubmittext').style.display = "none";
	} else $('newssubmittext').style.display = "block";
	$('preloader').style.display = "none";
	$('newssubmit_system').style.display = "block";
	$('newssubmit_system').innerHTML = originalRequest.responseText;
}

function ajaxNewsSubmissionSubmit(relLoc) {
	var url = relLoc+'ajaxNewsSubmissionSubmit.php';
	var pars = 'fullname='+escape($F('fullname'))+'&phone='+escape($F('phone'))+'&email='+escape($F('email'))+'&message='+escape($F('message'))+'&subject='+escape($F('subject'));
	new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showNewsSubmissionSubmitResponse});
}



