var otherview = false;
var forceview = false;

var rowcount = 1;

$(function() {
	$('#deliverypostcode').blur(function() {
		if ($(this).val() !="")
		{
			$.ajax({
				url: '/ajax/ajax.php',
				type: 'post',
				dataType: 'json',
				data: ({action : 'pcode_check',Pcode:this.value, Country: $('#deliverycountry').val()}),
				success: function(data) {
					if (data.result==false)
					{
						$.validationEngine.buildPrompt($("#deliverypostcode"),data.msg,"error");
						$("#sprytextfield11").html('<select class="catselectfeed"><option value="">--- Please enter a postcode ---</option></select>');
						
						if ($('#deliverycountry').val() == 'Australia')
						{
							$("#deliverystate").val('');
						}
						else
						{
							$("#deliverystate").attr('data-state', '');
						}
					}
					else
					{
						//		/$("#deliverysuburb").val(data.Locality);
						$("#sprytextfield11").html(data.Locality).show();
						
						if ($('#deliverycountry').val() == 'Australia')
						{
							$("#deliverystate").val(data.State);
						}
						else
						{
							$("#deliverystate").attr('data-state', data.State);
						}
						
						$.validationEngine.closePrompt($("#deliverypostcode"),"");
					}
				}
			});
		}
	});  	
	
	$('#deliverycountry').change(function() {
		var d_state = $('#deliverystate');
		if ($(this).val() == 'Australia')
		{
			if (d_state.attr('data-state'))
			{
				d_state.val(d_state.attr('data-state'));
			}
			else
			{
				d_state.attr('data-state', d_state.val()).val('');
			}
		}
		else
		{
			d_state.attr('data-state', d_state.val()).val('NA');
		}
	});

    
    var otherE2 = $('#academicdiv');
    var otherE4 = $('#otherdiv');

    $('#hearaboutus').change(function() {
		var s = $('#hearaboutus').val();
		if(s != -2)
		{   
		    otherE4.hide();
		    otherE4.animate({'height': 0});//.tween('height', 0);
		}
		else
		{
		    otherE4.show();
		    otherE4.animate({'height': 20});//.tween('height', 20);
		}
		return false;
	});
 });



function addrow(http_request,elm)
{
        var temp= http_request;
		 var tbl = document.getElementById('ac');  
		 var lastRow = tbl.rows.length -1;
        temp = temp.replace(/institutiona1/g, "institutiona"+lastRow);
		temp = temp.replace(/qualificationa1/g, "qualificationa"+lastRow);
		temp = temp.replace(/spry/g, "srpy");
			temp = temp.replace(/selectRequiredMsg/g, "hidden");
			temp = temp.replace(/selectRequiredState/g, "n");
			temp = temp.replace(/qualcheck/g, "changes");
			temp = temp.replace(/selected/g, "");
			temp = temp.replace(/\*/g, "");
			temp = temp.replace(/highest relevant/g, "");
			
		
		  window.rowcount++;
	
          try 
          {
            var row = elm;
            var cell = row.insertCell(-1);
			
            cell.colSpan = "6";
            cell.className="nopadding";
            cell.innerHTML = "<table style='margin:0; padding:0; width:100%' border='0'><tr>"+temp+"</tr></table>";
          } catch (e) 
          {
            var row = tablebody.insertRow(elm.rows.length-1);
            row.id ="sctablerow"+nextelm.value;
            row.innerHTML = temp;
          }
}
function addRowToTable()
{
  var tbl = document.getElementById('ac');
 var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow-1);
   // left cell 
  var s = $('#row1').html();
 
 //s = s.replace(/a1/gi,"a"+iteration);
// alert(s);
  
addrow(s,row);
  
/*  // var st = 'institutiona'+iteration;
  */
 // pageScroll();
	//scrolldelay2 = setTimeout('stopScroll()',500);
	return false;
}

function changes(s) { 
    var tbl = document.getElementById('ac');  
    var lastRow = tbl.rows.length -1;
    var ts = false;

    for (j=1;j<=window.rowcount;j++)
    { 
	if(($("#institutiona" + j).val()=='-1')){ts = true;}
	if(($("#qualificationa" + j).val()=='-1')){ts = true;}
    }

    otherview = ts;
    updateview();
}
function pageScroll() {
    	window.scrollBy(0,2); // horizontal and vertical scroll increments
    	scrolldelay = setTimeout('pageScroll()',19); // scrolls every 100 milliseconds
}
 
function stopScroll() {
    	clearTimeout(scrolldelay);
}

function qualcheck(url) {

  http.open("GET", url, true);
  changes();
  http.onreadystatechange=function() { 
    if(http.readyState == 4) {
	
		if(http.responseText=="MIR")
		{
			forceview = true;
			updateview();
 
		}else
		{
			forceview = false;
			updateview();
		}
			
    }
  }
  http.send(null);
  
} 

function checke(url) {

  http.open("GET", url, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
	
		if(http.responseText!="0")
		{
			alert("An existing member is using this email address, please try another. If you have forgotten your password please go to the login screen and request a reminder.");
			$('#email').val("");
			$('#email').focus();
		}
    }
  }
  http.send(null);
  
}

//var sprytextfield17u;
//var sprytextfield18u;
//var sprytextfield19u;
var isopen = false;
function updateview()
{
	if(otherview || forceview)
	{
		if(!isopen)
		{
				$('#academicdiv').animate({'height': 240});
			//tween('height', 240);
				/*sprytextfield17u = new Spry.Widget.ValidationTextField("sprytextfield17", "none", {hint:"Enter your academic institution *"});
			sprytextfield18u = new Spry.Widget.ValidationTextField("sprytextfield18", "none", {hint:"Enter your qualification *"});
			sprytextfield19u = new Spry.Widget.ValidationTextField("sprytextfield19", "none", {hint:"Enter year *"});*/
			/*spryselect4u.reset();
			spryselect4u.destroy();
			spryselect5u.reset();
			spryselect5u.destroy();
			spryselect6u.reset();
			spryselect6u.destroy();*/
			isopen = true;
		}
	}
	else
	{
		if(isopen)
		{
		$('#academicdiv').animate({'height': 0});//.tween('height', 0);
		/*sprytextfield17u.reset();
		sprytextfield17u.destroy();
		sprytextfield18u.reset();
		sprytextfield18u.destroy();
		sprytextfield19u.reset();
		sprytextfield19u.destroy();*/
		/*spryselect4u = new Spry.Widget.ValidationSelect("spryselect4", {validateOn:["change"]});
spryselect5u = new Spry.Widget.ValidationSelect("spryselect5", {validateOn:["change"]});
spryselect6u = new Spry.Widget.ValidationSelect("spryselect6", {validateOn:["change"]});
isopen = false;*/
		}
	}
	}
	
	function checkcontact(s)
	{
	var otherE3 = $('#interestdiv');
		if(s == 0)
		{  
			otherE.animate({'height': 0});//.tween('height', 0);
		}
		else
		{
			otherE3.animate({'height': 300});//.tween('height', 300);
		}
		return false;
		
	}
	
	var hnum = 170;
	

function updateviewpro() {
		otherEl = $('#professiondiv');
		//var s = $('#profession').value.search('Psychologist');
		var s = $('#profession').val().search('Psychologist');
		
		if(s == -1)
		{
			//otherEl.tween('height', 0);
			otherEl.animate({'height': 0});
			 
/*			spryselect9u.reset();
			spryselect9u.destroy();
			
			spryselect10u.reset();
			spryselect10u.destroy();
			
			sprytextfield14u.reset();
			sprytextfield14u.destroy();
			
			sprycheckbox1u.reset();
			sprycheckbox1u.destroy();
			
			
			sprytextfield15u.reset();
			sprytextfield15u.destroy();
			
			
			sprytextfield16u.reset();
			sprytextfield16u.destroy();
*/			
			window.hnum = 170;
				
			$('#ihide').css("display", "block");
			$('#ishow1').css("display", "none");
			$('#ishow2').css("display", "none");
			$('#ishow3').css("display", "none");

			$('#phychregnumber, #psychregstate ,#psychregcountry,#supervisornumber,#supervisorname ').removeClass("validate[required]");
			$("#ihide2").show();
			$("#ishowpsych").hide();
		}
		else
		{
			//otherEl.tween('height', window.hnum);
			otherEl.animate({
			    'height': window.hnum});
			//common fields
			
			//spryselect10u = new Spry.Widget.ValidationSelect("spryselect10", {invalidValue:"0"});
			
			//spryselect9u = new Spry.Widget.ValidationSelect("spryselect9", {invalidValue:"0"});

			//sprytextfield14u = new Spry.Widget.ValidationTextField("sprytextfield14", "none", {hint:"Psychologist registration Number *"});
			 
		}
		//reinitialize_validation();
		setTimeout(reinitialize_validation, 500);
		return false;
		}
		
		
		function showpsych()
		{
			window.hnum = 262;
				otherEl = $('#professiondiv');
				otherEl.animate({'height': window.hnum});//.tween('height', window.hnum);
			$('#ihide2').css("display", "none");
			$('#ishow1,#ishow2,#ishow3').hide();
			//$('#ishow3').hide();
			$('#ihide1,#ihide').show();
			$('#phychregnumber, #psychregstate ,#psychregcountry,#supervisornumber,#supervisorname ').removeClass("validate[required]");
			$('#ishowpsych').css("display", "block");
			$('#phychregnumber, #psychregstate ,#psychregcountry ').addClass("validate[required]");
			
	//spryselect10u = new Spry.Widget.ValidationSelect("spryselect10", {invalidValue:"0"});
			
			//spryselect9u = new Spry.Widget.ValidationSelect("spryselect9", {invalidValue:"0"});

			//sprytextfield14u = new Spry.Widget.ValidationTextField("sprytextfield14", "none", {hint:"Psychologist registration Number *"});
			
			//spryselect1u.reset();
			//spryselect1u.destroy();
			
			//spryselect15u.reset();
			//spryselect15u.destroy();
			
			//sprytextfield16u.reset();
			//sprytextfield16u.destroy();
			setTimeout(reinitialize_validation, 500);
			
		}
		
		function showintern()
		{
			window.hnum = 262;
			otherEl = $('#professiondiv');
			otherEl.animate({'height': window.hnum});//.tween('height', window.hnum);
			$('#ihide').css("display", "none");
			$('#ihide2').show();
			$('#ishowpsych').hide();
			$('#phychregnumber, #psychregstate ,#psychregcountry,#supervisornumber,#supervisorname ').removeClass("validate[required]");
			$('#ishow1').css("display", "block");
			$('#ishow2').css("display",  "block");
			$('#ishow3').css("display",  "block");
			$('#supervisornumber,#supervisorname ').addClass("validate[required]");
			
		//sprycheckbox1u = new Spry.Widget.ValidationCheckbox("sprycheckbox1", {validateOn:["change"]});
			
		//sprytextfield15u = new Spry.Widget.ValidationTextField("sprytextfield15", "none", {hint:"Supervisor name *", validateOn:["change"]});
//sprytextfield16u = new Spry.Widget.ValidationTextField("sprytextfield16", "none", {hint:"Supervisors registration number *", validateOn:["change"]});

			//spryselect9u.reset();
			//spryselect9u.destroy();
			
			//spryselect10u.reset();
			//spryselect10u.destroy();
			
			//sprytextfield14u.reset();
			//sprytextfield14u.destroy();
		    setTimeout(reinitialize_validation, 500);
		}
		
	function pd_profession_change()
  {
        if ($("#profession option:contains('Psychologist'):selected").length==1)
        {
          $("#profession_psych").show();
          //common fields
          $('#profession_psych_common input,#profession_psych_common select').addClass("validate[required]");
        }
        else
        {
          $("#profession_intern, #profession_psych").hide();
          $("#ihide").show();
          
          $("#psychregnumber,#supervisorregnumber,#supervisorname,#psychregstate, #psychregcountry ").val("");
          
          //common fields
          $('#profession_psych_common input,#profession_psych_common select').removeClass("validate[required]");
          //intern form
          $('#profession_intern input,#profession_intern select').removeClass("validate[required]");
        }
        //if hitted submit
        reinitialize_validation();
  }
  function reinitialize_validation()
  {
  //console.log("resized");
          if ($(".formError").length>0)
              {
                $(".formError").remove();
                $.validationEngine.submitValidation($("#register"));
              }  
  }
  $(window).resize(function() {
     reinitialize_validation();
  });
	
