var errorFlag = 'no';

if ( gLang == 'en' ) {
	var ERR_YOURNAME_EMPTY			= "Please enter your name";
	var ERR_YOUREMAIL_INVALID		= "Email appears to have an invalid format";
	var ERR_YOURCONTACT_EMPTY		= "Please provide either Email, Phone number or Skype ID";
	var ERR_ENTRY_EMPTY			= "Please enter your request";
	var ERR_ENTRY_TOO_LONG			= "Please limit your entry to 512 characters";
	var REQUEST_EDIT_FIELD_CAPTCHA_NEW	= "Get new image";
	var REQUEST_VIEW_PLEASE_WAIT  		= "Please wait...";
} else if ( gLang == 'es' ) {
	var ERR_YOURNAME_EMPTY			= "Please enter your name";
	var ERR_YOUREMAIL_INVALID		= "Email appears to have an invalid format";
	var ERR_YOURCONTACT_EMPTY		= "Please provide either Email, Phone number or Skype ID";
	var ERR_ENTRY_EMPTY			= "Please enter your request";
	var ERR_ENTRY_TOO_LONG			= "Please limit your entry to 1024 characters";
	var REQUEST_EDIT_FIELD_CAPTCHA_NEW	= "Get new image";
	var REQUEST_VIEW_PLEASE_WAIT  		= "Please wait...";
}


/* ----------------------------------------- */
function validateEntry( ajaxFile ) {


	errorFlag = 'no';

	/* name */
	if( document.request.Name.value.length < 1 ) {
		document.getElementById('errName').innerHTML = ERR_YOURNAME_EMPTY;
		document.request.Name.focus(); errorFlag = 'yes';
	} else {
		document.getElementById('errName').innerHTML = "";
	}
	
	
	var contact = false;
	
	/* email */
	if( document.request.Email.value.length >= 1 ) {
		contact = true;
		if(!validEmail(document.request.Email.value)){
			document.getElementById('errEmail').innerHTML = ERR_YOUREMAIL_INVALID;
			document.request.Email.focus(); errorFlag = 'yes'; 
		} else {
			document.getElementById('errEmail').innerHTML = "";
		}
	} else {
		document.getElementById('errEmail').innerHTML = "";
	}

	

	/* phone */
	if( document.request.Phone.value.length >= 1 ) {
		contact = true;
	}

	/* skype */
	if( document.request.Skype.value.length >= 1 ) {
		contact = true;
	}

	if ( contact == false ) {
		errorFlag = 'yes';
		document.getElementById('errContact').innerHTML = ERR_YOURCONTACT_EMPTY;
	} else {
		document.getElementById('errContact').innerHTML = "";
	}
	
	
	
	/* captcha and save */

	if( document.request.Entry.value.length < 1 ) {
		document.getElementById('errEntry').innerHTML = ERR_ENTRY_EMPTY;
		document.request.Entry.focus(); errorFlag = 'yes';
	} else if ( document.request.Entry.value.length > 511 ) {		
		document.getElementById('errEntry').innerHTML = ERR_ENTRY_TOO_LONG;
		document.request.Entry.focus(); errorFlag = 'yes';
	} else {
		document.getElementById('errEntry').innerHTML = "";
	}

	var objSubmitted 		= new Object();
	objSubmitted.verifyCode		= document.getElementById('verifyCode').value;
	objSubmitted.verifyText		= document.getElementById('verifyText').value;
	var theUrl			= ajaxFile + '?at=validateCaptcha&rnd=' + Math.random();
	var strSubmitted 		= 'jstring=' + objSubmitted.toJSONString();
	var callback 			= { success:handleValidateCaptcha, failure:handleAjaxError, argument:[] }; 
	var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);

}

/* ----------------------------------------- */
function handleValidateCaptcha(o){
	var response 	= o.responseText.parseJSON();
	document.getElementById('errCaptcha').innerHTML = "";

	document.getElementById('errCaptcha').innerHTML = response.message;	
	

	if ( response.result != 'success' ) { 
		document.request.verifyText.value 	= "";
		errorFlag = 'yes';
	}
	
	if ( response.result == 'success' && errorFlag == 'no' ) {
		document.request.submit();	
	}
}


/* ----------------------------------------- */
function clearEntry() {

	document.getElementById('errName').innerHTML  = "";
	document.getElementById('errContact').innerHTML = "";
	document.getElementById('errEntry').innerHTML = "";
	document.getElementById('errCaptcha').innerHTML    = "";
	document.getElementById('submissionMsg').innerHTML = "";

	document.request.Name.value 		= "";
	document.request.Email.value 		= "";
	document.request.Phone.value 		= "";
	document.request.Skype.value 		= "";
	document.request.MediaName.value 	= "";
	document.request.MediaType.value 	= "";
	document.request.Entry.value 		= "";
	document.request.verifyText.value 	= "";

}



/* ----------------------------------------- */
function refreshCaptcha( ajaxFile ) {
	var objSubmitted 		= new Object();
	objSubmitted.ajaxFile		= ajaxFile;
	var theUrl			= ajaxFile + '?at=refreshCaptcha&rnd=' + Math.random();
	var strSubmitted		= 'jstring=' + objSubmitted.toJSONString();
	var callback 			= { success:handleRefreshCaptcha, failure:handleAjaxError, argument:[] };
	var transaction 		= YAHOO.util.Connect.asyncRequest('POST', theUrl, callback, strSubmitted);
	
}

/* ----------------------------------------- */
function handleRefreshCaptcha(o){
	var response 	= o.responseText.parseJSON();
	if(response.result == 'success') {
		var ajaxFile   = response.ajaxFile;
	   	var verifyCode = response.verifyCode;
	   	var urlPic     = response.urlPic;
		document.getElementById('captchaContent').innerHTML = "<input type='hidden' id='verifyCode' value='" + verifyCode + "'><img src='" + urlPic + "'><br><a class='requestEdit' href='javascript:void(0)' onclick=javascript:refreshCaptcha('" + ajaxFile + "')>"+REQUEST_EDIT_FIELD_CAPTCHA_NEW+"</a><br><br>";
		document.getElementById('errCaptcha').innerHTML = "";
		document.request.verifyText.value 	= "";
	}
}





