//===================== SHOW/HIDE ON BLUR / FOCUS ===================//
// USAGE -  add class 'default' to the text field to get the effect

jQuery.validator.addMethod("selectNone", 
	function(value, element){ 
		if (element.value == "0"){ 
		  return false; 
		} 
		else return true; 
	}
); 


var active_color = '#595959'; // Colour of user provided text
var inactive_color = '#999999'; // Colour of default text

$(document).ready(function() {
  $("input.default").css("color", inactive_color);
  var default_values = new Array();
  $("input.default").focus(function() {
    if (!default_values[this.id]) {
      default_values[this.id] = this.value;
    }
    if (this.value == default_values[this.id]) {
      this.value = '';
      this.style.color = active_color;
    }
    $(this).blur(function() {
      if (this.value == '') {
        this.style.color = inactive_color;
        this.value = default_values[this.id];
      }
    });
  });
});

//===================== AUTO SUGGESTION SEARCH ===================//
$(document).ready(function() {
	$(".search_index").keyup(function(){
		var keyword = $(this).val();
		var id = $(this).attr('id');
		var dataString = 'method=search&searchword='+ keyword+'&id='+id;
		if(keyword!=''){
			$.ajax({
				type: "POST",
				url: "scripts/ajax/index.php",
				data: dataString,
				cache: false,
				success: function(html)
				{
					$("#display").html(html).show();
				}
			});
		}else{
			$("#display").html('');
		}
		return false;
	});
});	

/* ====== DESIGNER REGISTER PAGE JS ============ */ 
$().ready(function(){
	$("#frm_designerregister").validate({
		rules: {
			email: {
				required: true,
				email: true,
				emailCheckDesigner:true,
			},
			//paypal_email: {
			//	required: true,
			//	email: true,
			//},
			username: {
				required: true,
				usernameCheckDesigner:true,
			},
			password: "required",
			confirm_password: {
				required:true,
				equalTo:"#password",
			},
			agree : "required",
			captcha: {
				required: true,
				checkCaptcha: true,
			},
			link_to_portfolio: {
		        required: true,
				url: true,
			},
		},
		messages: {
			email: {
				required: "&nbsp;",
				email:"Invalid Email.",
				emailCheckDesigner:"Already in use",
			},
			//paypal_email: {
			//	required: "Required.",
			//	email:"InValid PayPal Email.",
			//},
			username: {
				required: "&nbsp;",
				usernameCheckDesigner:"Username already in use",
			},
			password: "&nbsp;",
			confirm_password:{
				required: "&nbsp;",
				equalTo: "Password doesn't match",
			},
			agree : "&nbsp;",
			captcha:{
				required: "&nbsp;",
				checkCaptcha : "Incorrect captcha.",
			},
			link_to_portfolio: {
		        required: "Link to portfolio required",
				url: "Not a valid URL",
			},
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});			   	
});

$.validator.addMethod('checkCaptcha', function(value) {
									
	/*$("#status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');*/											 	
	var postURL = "scripts/ajax/index.php";
	$.ajax({
		cache:false,
		async:false,
		url: postURL,
		data: "method=validateCaptcha&captcha="+value,
		dataType : 'html',
		type: "POST",
		success: function(msg) {
			result = (msg=='OK') ? true : false;
		}
	});
	return result;
	}, ''
);


  // extend the validation plugin to do remote username and email dupe checking

$.validator.addMethod('emailCheckDesigner', function(value) {
	/*$("#status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');*/											 	
	var postURL = "scripts/ajax/index.php";
	$.ajax({
		cache:false,
		async:false,
		type: "POST",
		data: "method=check_duplicate&email="+value+"&type=designer",
		url: postURL,
		success: function(msg) {
			/*$("#status").html('');*/	
			result = (msg=='OK') ? true : false;
		}
	});
	return result;
	}, ''
);

$.validator.addMethod('usernameCheckDesigner', function(value) {
	/*$("#status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');*/											 	
	var postURL = "scripts/ajax/index.php";
	$.ajax({
		cache:false,
		async:false,
		type: "POST",
		data: "method=check_duplicate&username="+value+"&type=designer",
		url: postURL,
		success: function(msg) {
			/*$("#status").html('');*/	
			result = (msg=='OK') ? true : false;
		}
	});
	return result;
	}, ''
);
/* ====== END - DESIGNER REGISTER PAGE JS ============ */ 

/* ====== DESIGNER CONFIRMATION PAGE JS ============ */ 
function leave(callback) {
  window.location = "index.php?view="+callback;
}
/* ====== END - CONFIRMATION PAGE JS ============ */ 

/* ====== LOGIN PAGE JS ============ */ 
$().ready(function(){
	$("#frm_login").validate({
		rules: {
			username: "required",
			password: "required",
		},
		messages: {
			username: "&nbsp;",
			password: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});			   	
});	
/* ====== END - LOGIN PAGE JS ============ */ 

/* ====== DEFAULT PAGE JS ============ */ 
/*$().ready(function(){
	$("#frm_default").validate({
		rules: {
			name: "required",
			project_type_id: {selectNone:true},
			project_scope: "required",
		},
		messages: {
			name: "&nbsp;",
			project_type_id: "&nbsp;",
			project_scope: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});			   	
});	*/
/* ====== END - DEFAULT PAGE JS ============ */ 

/* ======START:- CONTEST PAGE FOR THE COMMMENTS JS ============ */ 
$().ready(function(){
	$("#frm_comments").validate({
		rules: {
			comments: "required",
		},
		messages: {
			comments: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});			   	
});	
/* ====== END - CONTEST PAGE FOR THE COMMMENTS  JS ============ */ 

/* ====== RECOVER PAGE JS ============ */ 
$().ready(function(){
	$("#frm_recoverpassword").validate({
		rules: {
			email: {
				required: true,
				email: true,
			},
			username: "required",
		},
		messages: {
			email: {
				required: "&nbsp;",
				email:"&nbsp;",
			},
			username: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
	$("#frm_recoverusername").validate({
		rules: {
			email: {
				required: true,
				email: true,
			},
			password: "required",
		},
		messages: {
			email: {
				required: "&nbsp;",
				email:"&nbsp;",
			},
			password: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});				   	
});	
/* ====== END - RECOVER PAGE JS ============ */ 

/* ====== START : LAUNCH PROJECT JS ============ */ 

jQuery.validator.addMethod("name_color", function(value, element) { 
  if(value == ""){
	  $("#name").css({"background-color": "#ffdedd"});
	  return false;
  }else{
	  $("#name").css({"background-color": "#FFF"});
	  return true;
  }
});
jQuery.validator.addMethod("project_scope_color", function(value, element) { 
  if(value == ""){
	  $("#project_scope").css({"background-color": "#ffdedd"});
	   return false;
  }else{
	  $("#project_scope").css({"background-color": "#FFF"});
	   return true;
  }
});
jQuery.validator.addMethod("project_description_color", function(value, element) { 
  if(value == ""){
	  $("#project_description").css({"background-color": "#ffdedd"});
	   return false;
  }else{
	  $("#project_description").css({"background-color": "#FFF"});
	   return true;
  }

});

$().ready(function(){
	$("#frm_launch_project").validate({
		rules: {
			project_type_id: {selectNone:true},
			name: {name_color:true},
			project_scope: {project_scope_color:true},
			project_description: {project_description_color:true},
			/*audience_mess1: "required",
			audience_mess2: "required",
			audience_mess3: "required",*/
			logo_idea: "required",
		},
		messages: {
			project_type_id: "&nbsp;",	
			name: "&nbsp;",
			project_scope: "&nbsp;",
			project_description: "&nbsp;",
			/*audience_mess1: "Required",
			audience_mess2: "Required",
			audience_mess3: "Required",*/
			logo_idea: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
});
/* ====== END   : LAUNCH PROJECT JS ============ */ 


/* ====== START : COMPANY VIEW JS ============ */ 
$(document).ready(function(){
	$('#doReg').click( function(){
		$('#login-cmpbox').hide();
		$('#register-cmpbox').show();
		$('#information').hide();
		$('#notuser').hide();
	});
	
	$('#doLog').click( function(){
		$('#register-cmpbox').hide();
		$('#login-cmpbox').show();
		$('#information').show();
		$('#notuser').show();
	});

jQuery.validator.addMethod("client_first_name", function(value, element) { 
  if(value == ""){
	  $("#first_name").css({"background-color": "#ffdedd"});
	  return false;
  }else{
	  $("#first_name").css({"background-color": "#FFF"});
	  return true;
  }
});
jQuery.validator.addMethod("client_last_name", function(value, element) { 
  if(value == ""){
	  $("#last_name").css({"background-color": "#ffdedd"});
	  return false;
  }else{
	  $("#last_name").css({"background-color": "#FFF"});
	  return true;
  }
});


jQuery.validator.addMethod("client_email", function(value, element) { 
  if(value == ""){
	  $("#email").css({"background-color": "#ffdedd"});
	  return false;
  }else{
	  $("#email").css({"background-color": "#FFF"});
	  return true;
  }
});


jQuery.validator.addMethod("client_username", function(value, element) { 
  if(value == ""){
	  $("#username").css({"background-color": "#ffdedd"});
	  return false;
  }else{
	  $("#username").css({"background-color": "#FFF"});
	  return true;
  }
});
jQuery.validator.addMethod("client_password", function(value, element) { 
  if(value == ""){
	  $("#password").css({"background-color": "#ffdedd"});
	  return false;
  }else{
	  $("#password").css({"background-color": "#FFF"});
	  return true;
  }
});
jQuery.validator.addMethod("client_confirmpassword", function(value, element) { 
  if(value == ""){
	  $("#confirm_password").css({"background-color": "#ffdedd"});
	  return false;
  }else{
	  $("#confirm_password").css({"background-color": "#FFF"});
	  return true;
  }
});
	$("#frm_client_info").validate({
		rules: {
			client_company: {makeValid:true},
			telephone: {makeValid:true},
			address: {makeValid:true},
			postcode: {makeValid:true},
			city: {makeValid:true},
			first_name: {client_first_name:true},
			last_name: {client_last_name:true},
			country_id: {selectNone:true},
			email:{
				client_email:true,
				email:true,
				emailCheckClient:true,
			},
			username: {
				client_username: true,
				usernameCheckClient:true,
			},
			password: {client_password:true},
			confirm_password:{
				client_confirmpassword:true,
				equalTo:"#password",
			},
			vat_no:{ 
			    /*required: true,*/
				number: true,
			    minlength: 10
			},
			captcha: {
				required: true,
				checkCaptcha: true,
			}

		},
		messages: {
			client_company: "&nbsp;",
			address: "&nbsp;",
			postcode: "&nbsp;",
			city: "&nbsp;",
			telephone: "&nbsp;",
			first_name: "&nbsp;",
			last_name: "&nbsp;",
			country_id: "&nbsp;",
			email:{
				client_email:"&nbsp;",
				email:"Invalid Email.",
				emailCheckClient:"Already in use",
			},
			username: {
				client_username: "&nbsp;",
				usernameCheckClient:"Username already in use",
			},
			password: "&nbsp;",
			confirm_password:{
				client_confirmpassword: "&nbsp;",
				equalTo: "Password doesn't match",
			},
			vat_no: "Enter a valid VAT Number.",
			captcha: {
				required: "&nbsp;",
				checkCaptcha: "Invalid Captcha Code.",
			}
		},
		submitHandler: function(form){
			
			var country_code = $('#country_code').val();
			var vat_no = $('#vat_no').val();			
			var option = $('#from_eu').val(); //getCheckedValue(document.forms['frm_client_info'].elements['from_eu']);
			
			if(option=='1' && (country_code!='' && vat_no!=''))
			{				
				$('#for_vat_no').html('<img src="ccp/images/ajax-loader.gif" />');
				
				$.post('ccp/verify-vat-number/index.php',
					   {
						  'country_code':country_code,
						  'vat_no':vat_no
					   },
					   function(data){	
						  $('#for_vat_no').html(data.response);
						  
						  if(data.response == 'Yes, valid VAT number')
						  {
							  $('#vat_no_valid').val(1);
							  form.submit();
						  }
						  else
						    $('#vat_no_valid').val(0);
						  
						  return false;
						  
					   },'json');
				
				return false;
			}
			else
			{
				$('#vat_no_valid').val(0);
				form.submit();
			}
			
			return false;
		},
		success: function(label) {
			//label.html("&nbsp;").addClass("checked");
		}
	});

$.validator.addMethod('makeValid', function(value) {
	if($("input[name='iscompany']:checked").val() == 'Yes'){
		if(value != ''){
			result = true;
		}else{
			result = false;	
		}
	}else{
		result = true;
	}
	return result;
	}, ''
);

    // extend the validation plugin to do remote username and email dupe checking
	$.validator.addMethod('emailCheckClient', function(value) {
		/*$("#status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');*/											 	
		var postURL = "scripts/ajax/index.php";
		$.ajax({
			cache:false,
			async:false,
			type: "POST",
			data: "method=check_duplicate&email="+value+"&type=client",
			url: postURL,
			success: function(msg) {
				/*$("#status").html('');*/	
				result = (msg=='OK') ? true : false;
			}
		});
		return result;
		}, ''
	);
	
	$.validator.addMethod('usernameCheckClient', function(value) {
		/*$("#status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');*/											 	
		var postURL = "scripts/ajax/index.php";
		$.ajax({
			cache:false,
			async:false,
			type: "POST",
			data: "method=check_duplicate&username="+value+"&type=client",
			url: postURL,
			success: function(msg) {
				/*$("#status").html('');*/	
				result = (msg=='OK') ? true : false;
			}
		});
		return result;
		}, ''
	);
	
	$("#frm_client_login").validate({
		rules: {
			username_login: "required",
			password_login: "required",
		},
		messages: {
			username_login: "&nbsp;",
			password_login: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
	
	$("#frm_testimonials").validate({
		rules: {
			testimonials: "required"
		},
		messages: {
			testimonials: "&nbsp;"
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
	
});
/* ====== END   : COMPANY VIEW JS ============ */ 

/* ====== START : PRICE SETTING PAGE JS ============ */ 
$(document).ready(function(){
	$('#whose_design').change( function(){
		var val = $('#whose_design').val();
		if(val == 'Personal'){
			$('#ots').hide();
			$('#prs').show();
			$("#project_price_prs").valid();
		}else if(val == 'Outsourcing'){
			$('#prs').hide();
			$('#ots').show();
			$("#project_price_ots").valid();
		}
	});
	
	$('#total_concepts').change( function(){
		var concepts = $('#total_concepts').val();
		var concept_price = $('#project_price_ots').val();
		var total_price = concepts*concept_price;
		var data = "&nbsp;= "+total_price+" $";
		$('#total').html(data);
	});
	
	$("#frm_setPprice").validate({	 			 
		rules: {
			project_price_prs: {
			  project_price_prs_validate:true,
			  minPriceCheck:true,
			},
			project_price_ots:{
			  project_price_ots_validate:true,
			  minConceptPriceCheck:true,
			}
		},
		messages: {
			project_price_prs:{
				project_price_prs_validate: "&nbsp;",
				minPriceCheck:"Award Price must be more than Min Price",
			},
			project_price_ots:{
				project_price_ots_validate: "&nbsp;",
				minConceptPriceCheck:"Must be more than Min Price",
			},
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});	
});

$.validator.addMethod('project_price_prs_validate', function(value) {
	if($("#whose_design").val() == 'Personal'){
		if($("#project_price_prs").val() != ''){
			result = true;	
		}else{
			result = false;	
		}
	}else{
		result = true;
	}
	return result;
	}, ''
);

$.validator.addMethod('project_price_ots_validate', function(value) {
	if($("#whose_design").val() == 'Outsourcing'){
		if($("#project_price_ots").val() != ''){
			result = true;	
		}else{
			result = false;	
		}	
	}else{
		result = true;
	}
	return result;
	}, ''
);


$.validator.addMethod('minPriceCheck', function(value) {
	if($("#whose_design").val() == 'Personal'){											
		MinPrice = $("#min_prs").val();
		result = (parseFloat(value) >= parseFloat(MinPrice)) ? true : false;
	}else{
		result = true;
	}
	return result;
	}, ''
);

$.validator.addMethod('minConceptPriceCheck', function(value) {
	if($("#whose_design").val() == 'Outsourcing'){												   	
		MinPrice = $("#min_concept_ots").val();
		result = (parseFloat(value) >= parseFloat(MinPrice)) ? true : false;
	}else{
		result = true;
	}
	return result;
	}, ''
);
/* ====== END   : PRICE SETTING PAGE JS ============ */ 

/* ====== START : CONTEST PAGE - FRONT JS ============ */ 
$(document).ready(function(){
	$(".concept_header-mode").click(function(){
		$("#hideshow").toggleClass("hide_proj_desc");
		$(this).text($(this).text() == 'HIDE CONTEST' ? 'SHOW CONTEST' : 'HIDE CONTEST');
	});
});
/* ====== END   : CONTEST PAGE - FRONT JS ============ */ 

/* ====== START : SUBMIT ENTRY PAGE - FRONT JS ============ */ 
$(document).ready(function(){
	$("#frm_submit_entry").validate({
		rules: {
			logo: "required",
		},
		messages: {
			logo: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
	
});
/* ====== END   : SUBMIT ENTRY PAGE JS ============ */ 

/* ====== START : CONTEST PAGE FOR DESIGN VIOLATION JS ============ */ 
$(document).ready(function(){
	$("#frm_report_design_violation").validate({
		rules: {
			project_id: "required",
			designer_id: "required",
			subject: "required",
			reason: "required",
		},
		messages: {
			project_id: "&nbsp;",
			designer_id: "&nbsp;",
			subject: "&nbsp;",
			reason: "&nbsp;",
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
	
});
/* ====== END   : CONTEST PAGE FOR DESIGN VIOLATION  JS ============ */ 

function getProjDesigners(ProjId,DesignerId){
	$.ajax(
		{
			type: "POST",
			url: "scripts/ajax/index.php",
			data: "method=getProjDesigners&ProjId="+ProjId+"&DesignerId="+DesignerId,
			success: function(msg){
				if(msg != "NO DESIGNERS"){
					var designerCombo = $("#designer_id");
					designerCombo.html("");
					designerCombo.html(msg);
				}
			}
		}
	);
}// JavaScript Document

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
