function Contests() {
}

//used on upload photo page
Contests.checkPhotoSelected = function () {
   if(file_is_required == null ||  file_is_required == 0) {
   	   return true;
   }

   var photo_status = Core.getText('photo_file');
   if(photo_status == null || photo_status == '') {
      photo_status = Core.getText('photo_url');   
   } 
  
   if(photo_status == null || photo_status == '') {
      Core.showElement('photo_not_selected');
      Core.setFocus('photo_title');
      return false;
   } else {
      Core.hideElement('photo_not_selected');
      Core.hideElement('incorrect_file_format');           
      if(photo_status.toUpperCase().indexOf('.JPG') > -1) {
         Core.hideElement('photo_not_jpg_format');
         return true;
      } else { 
         if(check_jpg_format) {
            Core.showElement('photo_not_jpg_format');
            if (Core.getText('photo_url') == null)
               Core.setFocus('photo_title');
            else 
               Core.setFocus('photo_url');
            return false;
         } else if(check_video_format) {
            return Contests.checkVideoFormat()
         } else {
            return true;
         }
      }               
   }
   return true;
}


Contests.checkVideoFormat = function() {
   Core.hideElement('incorrect_file_format');
   var photo_field = Core.getText('photo_file');
   
   // check against popular, but incorrect formats
   var formats = new Array('gif','png', 'bmp', 'bak', 'bat', 'doc', 'xls', 'pdf', 'zip', 'rar', 'ini', 'txt', 'log','mp3', 'htm', 'html', 'ppt', 'tmp', 'xml', 'mdb', 'exe', 'csv', 'com');   
   for (i=0; i<formats.length; ++i) {
      if( photo_field.toLowerCase().indexOf('.'+ formats[i]) > -1  ) {
         
         Core.showElement('incorrect_file_format');
         return false;
      }
   } 
   return true;
}


Contests.checkFileType = function(types_str, filename) {
   i = 0;
   var types_arr = array();
   types_arr = split(' ', types_str);
   while(types_arr[i] != null) {
      if(filename.toLowerCase().indexOf(types_arr[i]) ) {         
         return true;;
      }
      i++;
   }
   
   //TODO check translation
   return confirm('Are you sure that file filename have a valid format');
}



//used on upload photo page
// currently not used
Contests.submitUploadForm = function() {
   //var terms = checkTermsAccepted();
   
   if(Core.getText('logged_in') != 1) {
      Core.showElement('not_logged_in_error');
      return false;
   } else {
      Core.hideElement('not_logged_in_error');
   }
   
   var photo = checkPhotoSelected();
   if(photo) {
      document.getElementById("photo_upload").submit();
      Core.showElement('uploading'); 
      Core.hideElement('upload_table');
   }
}

//used on upload photo page
Contests. checkTermsAccepted = function(){
   var status = Core.getText('terms_accepted');   
   if(status == null || status == '') {
      Core.showElement('terms_not_accepted');
      Core.setFocus('terms_accepted');
      return false;
   } else {
      Core.hideElement('terms_not_accepted');
      return true;
   }
}

//used on upload photo page
Contests.checkCopyrightOk = function() {
   var status = Core.getText('copyright_ok');   
   if(status == null || status == '') {
      Core.showElement('copyright_not_ok');
      Core.setFocus('copyright_ok');
      return false;
   } else {
      Core.hideElement('copyright_not_ok');
      return true;
   }
}

//used on upload photo page
Contests.signupCheck = function() {
   var email_checked = EasySignup.checkItem('email');
   var photo_selected = Contests.checkPhotoSelected();
   var terms = Contests.checkTermsAccepted();
   var user_fields = Contests.checkUserFields();  
   var entry_title      = Contests.checkTitle();
   
   return ((terms && photo_selected && email_checked && user_fields && entry_title) ? true : false);
}

Contests.flashUploadCheck = function() {
  // Core.hideElement('choose_flash_file_error');
   Contests.hideFlashFileNotSelected();

   var email_checked = EasySignup.checkItem('email');
   var terms = Contests.checkTermsAccepted();
   var user_fields = Contests.checkUserFields();
   var entry_title = Contests.checkTitle();
   
   if (terms && email_checked && user_fields && entry_title) 
      return true; 
   else {
      Core.hideElement('photo_not_selected');
      Contests.showFlashFileNotSelected();
      return false;
   }
}

Contests.showFlashFileNotSelected = function() {
   Core.showElement('choose_flash_file_error');
}

Contests.hideFlashFileNotSelected = function() {
   Core.hideElement('choose_flash_file_error');   
   
}

//used on upload photo page
Contests.checkTitle = function () {
   var title = Core.getText('photo_title');   
   if(title == null || title == '') {
      Core.showElement('title_not_ok');
      Core.setFocus('photo_title');
      return false;
   } else {
		if(title.length < 3) {
			alert("Too short Title. Hint: The shorter entry's title the farther its success!");
			Core.showElement('title_not_ok');
			Core.setFocus('photo_title');
			return false;
		}
		
		Core.hideElement('title_not_ok');
		return true;
   }
}

//used on upload photo page
Contests.checkAllConditions = function () { //submit_photo) { //signupCheck(submit_photo) { 
   var email_checked  = true; //EasySignup.checkItem('email')
   var photo_selected = Contests.checkPhotoSelected();
   var terms          = Contests.checkTermsAccepted();
   var copyright_ok   = Contests.checkCopyrightOk();
   var entry_title      = Contests.checkTitle();
   var user_fields      = Contests.checkUserFields();
     
   var status = ((terms && photo_selected && email_checked && copyright_ok && entry_title && user_fields) ? true : false);
   
   return status;
}


Contests.checkUserFields = function()  {
   var status = true;
   if(Contests.required_user_fields != null) {
     var i = 0;
      while(field_name = Contests.required_user_fields[i] ) {
         var field_content = Core.getText(field_name);
         if(field_content == null || field_content == '') {
            Core.showElement(field_name + '_error');
            status = false;
         } else  {
            Core.hideElement(field_name + '_error');
         }
         i++;
      }
   }
  
    if(status == false) {
      alert('Please fill all required data!');
    }  

   return status;
}


// select url in upload form
Contests.chUrl = function (){
   Core.hideElement('chfile');
   Core.showElement('churl');
	
   Core.hideElement('chFlashButton');
	
   Core.showElement('chHTML');
   Core.showElement('chHTMLButton');	
}

// select file in upload form   
Contests.chFile = function (){
   Core.hideElement('churl');
   Core.showElement('chfile');
}


// select url in upload form
Contests.chFlash = function (){
   Core.hideElement('chHTML');
   Core.hideElement('chHTMLButton');
   Contests.hideFlashFileNotSelected();
	
	Core.hideElement('photo_not_selected');
	Core.hideElement('photo_not_jpg_format');
	Core.hideElement('incorrect_file_format');
	
   Core.showElement('chFlash');
   Core.showElement('chFlashButton');
}

// select file in upload form   
Contests.chHTML = function (){
   Core.hideElement('chFlash');
   Core.hideElement('chFlashButton');
   Contests.hideFlashFileNotSelected();
   
   Core.showElement('chHTML');
   Core.showElement('chHTMLButton');
}


Contests.showContestDescription = function() {
	Core.showElement('long_descr');
	Core.hideElement('short_descr');
	var hostname = Core.localizeURL('contests.fotki.com');
	hostname = hostname.replace(/\:\d+/,"");	
	setCookie('show_contest_descr', 1, "", "/", hostname);
}

Contests.hideContestDescription = function() {
	Core.showElement('short_descr');
	Core.hideElement('long_descr');
	var hostname = Core.localizeURL('contests.fotki.com');
	hostname = hostname.replace(/\:\d+/,"");
	setCookie('show_contest_descr', 0, "", "/", hostname);
}


window.onload = function() {
	if(getCookie("show_contest_descr") == 1) {
		Contests.showContestDescription();
	} else {
		Contests.hideContestDescription();
	} 
}


