﻿var errorMsgs="";
var errorMsgCount=0;
var errorMsg;
var errorMsgDiv;

	tinyMCE.init({
		mode : "exact",
	    elements : "cvHTML",
		theme : "simple"
	});
var responseArray;
userID = -1;
function validateForm() {
    _email = document.getElementById('ctl00_main_panel_sign1_newEmail').value;
    document.getElementById('ctl00_main_panel_sign1_newEmail').disabled=true;
    document.getElementById('ctl00_main_panel_sign1_firstname').disabled=true;
    document.getElementById('ctl00_main_panel_sign1_surname').disabled=true;
    document.getElementById('ctl00_main_panel_sign1_telephone').disabled=true;
    document.getElementById('ctl00_main_panel_sign1_postcode').disabled=true;
    document.getElementById('ctl00_main_panel_sign1_sectorID').disabled=true;
    document.getElementById('ctl00_main_panel_sign1_educationLevel').disabled=true;
    document.getElementById('ctl00_main_panel_sign1_passwordNew').disabled=true;
    document.getElementById('ctl00_main_panel_sign1_passwordConfirm').disabled=true;
    if(userID=='')
        userID=-1;


    new Ajax.Request('/jobs/ajax/validateEmail.aspx', {parameters:'uid='+userID+'&e='+_email , onSuccess:handlerValidateEmail, onFailure:errFuncx});
    //alert('/jobs/ajax/validateEmail.aspx?uid=' + userID + '&e=' + _email);
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}



function validateExtensions(extensionsList,filename)
{
    var allowedExtensions = extensionsList.toLowerCase().split('|');
    var passed = false;
    var cnt=0;
    while (cnt< allowedExtensions.length)
    {
        if(filename.toLowerCase().lastIndexOf('.'+allowedExtensions[cnt])>=0)
            passed=true
        
        cnt++;
    } 
    
    return passed;
}






function validatePostcode()
{

        result = postcodeCheck(document.getElementById("ctl00_main_panel_sign1_postcode").value);
        if (result == "") {
            document.getElementById('ctl00_main_panel_sign1_postcode').style.borderColor = "#eee";
            new Ajax.Request('/jobs/ajax/validatePostcode.aspx', { parameters: 'postcode=' + document.getElementById("ctl00_main_panel_sign1_postcode").value, onSuccess: postcodeFunc, onFailure: postcodeError });
        }
        else {
            errorMsgCount++;
            errorMsgs += '<li>' + result + '</li>';
            document.getElementById('ctl00_main_panel_sign1_postcode').style.borderColor = "#cc0000";
            validateComplete();
        }
    
}
	
var postcodeFunc = function(t) {
    
    if(t.responseText=="ok")
    {
        
    }
    else
    {
        errorMsgCount++;
        errorMsgs += '<li>This does not appear to be a valid postcode</li>';
        document.getElementById('ctl00_main_panel_sign1_postcode').style.borderColor = "#cc0000"; 
    }
    validateComplete();
}

var postcodeError = function(t)
{
    errorMsgCount++;
    errorMsgs += '<li>This does not appear to be a valid postcode</li>';
    document.getElementById('ctl00_main_panel_sign1_postcode').style.borderColor = "#cc0000";
    validateComplete();
}


function postcodeCheck(test){ //check postcode format is valid
 size = test.length
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
  
 if (size < 6 || size > 8){ //Code length rule
  return "'"+test + "' is not a valid postcode - wrong length";
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   return ("'"+test + "' is not a valid postcode - cannot start with a number");
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   return ("'"+test + "' is not a valid postcode - alpha character in wrong position");
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   return ("'"+test + "' is not a valid postcode - number in wrong position");
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   return ("'"+test + "' is not a valid postcode - number in wrong position");
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   return ("'"+test + "' is not a valid postcode - no space or space in wrong position");
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   return ("'"+test + "' is not a valid postcode - only one space allowed");
  }
  
return "";
}





handlerValidateEmail = function(t) {
    errorMsgs="";
    errorMsgCount=0;
    
    if(t.responseText=='failed')
    {
        errorMsgCount++;
        errorMsgs+='<li>The email address you have chosen is already registered.</li>';
        document.getElementById('ctl00_main_panel_sign1_newEmail').style.borderColor = "#cc0000";
    }
    else {
        if (!echeck(document.getElementById('ctl00_main_panel_sign1_newEmail').value)) {
            errorMsgCount++;
            errorMsgs += '<li>The email address you have chosen is not valid.</li>';
            document.getElementById('ctl00_main_panel_sign1_newEmail').style.borderColor = "#cc0000";
        }
        else {
            document.getElementById('ctl00_main_panel_sign1_newEmail').style.borderColor = "#eee";
        }
    } 
    validatePostcode();
}





function validateComplete()
{
    errorMsg = '<div class="details_text" stle="line-height:150%">';
    errorMsgDiv = document.getElementById('errorDiv');
    var ed = tinyMCE.get('cvHTML');
	textCV = ed.getContent();
		      
    document.getElementById('ctl00_main_panel_sign1_newEmail').disabled=false;
    document.getElementById('ctl00_main_panel_sign1_firstname').disabled=false;
    document.getElementById('ctl00_main_panel_sign1_surname').disabled=false;
    document.getElementById('ctl00_main_panel_sign1_telephone').disabled=false;
    document.getElementById('ctl00_main_panel_sign1_postcode').disabled=false;
    document.getElementById('ctl00_main_panel_sign1_sectorID').disabled=false;
    document.getElementById('ctl00_main_panel_sign1_educationLevel').disabled=false;
    document.getElementById('ctl00_main_panel_sign1_passwordNew').disabled=false;
    document.getElementById('ctl00_main_panel_sign1_passwordConfirm').disabled=false;    


    
    var ed = tinyMCE.get('cvHTML');
	textCV = ed.getContent();
		        
   if(!document.getElementById('readTerms').checked)
    {
        errorMsgCount++;
        errorMsgs+='<li>Please confirm you agree to our terms and conditions.</li>';
        document.getElementById('readTerms').style.borderColor = "#cc0000";
    }
    else {
        document.getElementById('readTerms').style.borderColor = "#eee";
    }   
    /*
    if(document.getElementById('ctl00_main_panel_sign1_cvFile').value==''&&document.getElementById('ctl00_main_panel_sign1_cvFileName').innerHTML==''&&textCV=='')
    {
        errorMsgCount++;
        errorMsgs+='<li>Please attach a CV File or enter a text CV</li>';
    }
    
    if(document.getElementById('ctl00_main_panel_sign1_cvFile').value!='')
    {
        if(!validateExtensions('doc|pdf|rtf|txt|docx',document.getElementById('ctl00_main_panel_sign1_cvFile').value))
        {
            errorMsgCount++;
            errorMsgs+='<li>Please only upload CV files in doc, docx, pdf, rtf or txt file formats.</li>';
        }
    } 
    */
    

    if(document.getElementById('ctl00_main_panel_sign1_firstname').value=='')
    {
        errorMsgCount++;
        errorMsgs+='<li>Please enter your first name.</li>';
        document.getElementById('ctl00_main_panel_sign1_firstname').style.borderColor = "#cc0000";
    }
    else {
        document.getElementById('ctl00_main_panel_sign1_firstname').style.borderColor = "#eee";
    }

    if (document.getElementById('ctl00_main_panel_sign1_surname').value == '') {
        errorMsgCount++;
        errorMsgs += '<li>Please enter your surname.</li>';
        document.getElementById('ctl00_main_panel_sign1_surname').style.borderColor = "#cc0000";
    }
    else {
        document.getElementById('ctl00_main_panel_sign1_surname').style.borderColor = "#eee";
    }      

    if(document.getElementById('ctl00_main_panel_sign1_telephone').value=='')
    {
        errorMsgCount++;
        errorMsgs+='<li>Please enter your telephone number.</li>';
        document.getElementById('ctl00_main_panel_sign1_telephone').style.borderColor = "#cc0000";
    }
    else {
        document.getElementById('ctl00_main_panel_sign1_telephone').style.borderColor = "#eee";
    }         
    
  /* 
    if(document.getElementById('ctl00_main_panel_sign1_sectorID').selectedIndex==0)
    {
        errorMsgCount++;
        errorMsgs+='<li>Please specify the sector you are seeking to work in.</li>';
    }                  

    if(document.getElementById('ctl00_main_panel_sign1_educationLevel').selectedIndex==0)
    {
        errorMsgCount++;
        errorMsgs+='<li>Please education level.</li>';
    }
    */
    if(document.getElementById('ctl00_main_panel_sign1_passwordNew').value=='' || (document.getElementById('ctl00_main_panel_sign1_passwordNew').value!=document.getElementById('ctl00_main_panel_sign1_passwordConfirm').value))
    {
        errorMsgCount++;
        errorMsgs+='<li>Your passwords do not match or are blank</li>';
        document.getElementById('ctl00_main_panel_sign1_passwordConfirm').style.borderColor = "#cc0000";
        document.getElementById('ctl00_main_panel_sign1_passwordNew').style.borderColor = "#cc0000";
    }
    else {
        document.getElementById('ctl00_main_panel_sign1_passwordConfirm').style.borderColor = "#eee";
         document.getElementById('ctl00_main_panel_sign1_passwordNew').style.borderColor = "#eee";
    }          
    
    if (errorMsgCount>0)
    {
        if(errorMsgCount==1)
            errorMsg += '<h2 class="jobs" style="color:#5A9BF7; ">Sign-up problem</h2><ul style="color:#ff0000; line-height:150%;">'
        else
            errorMsg += '<h2 class="jobs" style="color:#5A9BF7; ">Sign-up problems</h2><ul style="color:#ff0000; line-height:150%;">'
            
        errorMsg+=''+errorMsgs+'</ul></div>';
        errorMsgDiv.innerHTML=errorMsg;

        jQuery('html,body').animate({ scrollTop: 315 }, 500);
        resizeDiv('errorDiv', 75 + (errorMsgCount * 17));

    }
    else
    {
        document.getElementById('applyGo').value = "go";
        document.getElementById('aspnetForm').submit();
    }
}





function ajaxSend() 
{
    _email = document.getElementById('loginEmailID').value;
    _password = document.getElementById('loginPasswordID').value;  
    document.getElementById('loginEmailID').disabled=true;     
    document.getElementById('loginPasswordID').disabled=true;
    new Ajax.Request('/jobs/ajax/login.aspx', {parameters:'e='+_email+'&p='+_password+'&guid='+_guid , onSuccess:handlerFuncx, onFailure:errFuncx});
}

var handlerFuncx = function(t) {
   // alert("response:"+t.responseText)
   document.getElementById('loginEmailID').disabled=false;     
   document.getElementById('loginPasswordID').disabled=false;
   if(t.responseText=='failed')
   {
        document.getElementById('loginMsg').innerHTML = '<span class="jobSearch" style="color:#dd1111;"> Login incorrect.</span>';
   }
   else
   {
        window.location='/mySearch.aspx?lg=1';
   }
}

function fillUserDetails()
{
        userID = responseArray[0];
        document.getElementById('ctl00_main_panel_sign1_newEmail').value = responseArray[1];
        document.getElementById('ctl00_main_panel_sign1_passwordNew').value = responseArray[2];
        document.getElementById('ctl00_main_panel_sign1_passwordConfirm').value = responseArray[2];
        document.getElementById('ctl00_main_panel_sign1_firstname').value = responseArray[3];
        document.getElementById('ctl00_main_panel_sign1_surname').value = responseArray[4];
        document.getElementById('ctl00_main_panel_sign1_postcode').value = responseArray[5];
        document.getElementById('ctl00_main_panel_sign1_telephone').value = responseArray[6];  
        



        educationLevelID = responseArray[7];
        userSectorID = responseArray[8];
        publicCVID = responseArray[10];
        
        var sectorObj = document.getElementById('ctl00_main_panel_sign1_sectorID')
        cnt=0
        while(cnt<sectorObj.options.length)
        {
            if(sectorObj.options[cnt].value==userSectorID)
            sectorObj.selectedIndex=cnt;
            cnt++;
        }
        
        var educationLevelObj = document.getElementById('ctl00_main_panel_sign1_educationLevel')
        cnt=0
        while(cnt<educationLevelObj.options.length)
        {
            if(educationLevelObj.options[cnt].value==educationLevelID)
            educationLevelObj.selectedIndex=cnt;
            cnt++;
        }
        
        var publicCVObj = document.getElementById('ctl00_main_panel_sign1_publicCV')
        cnt=0
        while(cnt<publicCVObj.options.length)
        {
            if(publicCVObj.options[cnt].value==publicCVID)
            publicCVObj.selectedIndex=cnt;
            cnt++;
        }        
        

      
        document.getElementById('ctl00_main_panel_sign1_cvFileName').innerHTML = "Your <b>stored CV</b> file is <b>"+responseArray[9]+"</b><br />Uploaded "+responseArray[11]+".<br />";
    
       // document.getElementById('loginLabel').innerHTML = "<span class=\"jobSearch\">Your Stored <b>Details</b></span><br /><br />";

        dummy_mc2 = document.getElementById('newUserDiv');
        zzz2 = new Tween(dummy_mc2.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc2.style.height),0,1,'px');
        zzz2.fixTime;      
        zzz2.start();   

}

var errFuncx = function(t) {
    alert('Error>> ' + t.status + ' -- ' + t.statusText);
}

function resizeDiv(id,finishHeight)
{
    document.getElementById("body_main_panel").style.height = "100%";
     dummy_mc = document.getElementById(id);
    //	zzz = new Tween(dummy_mc.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc.style.height),parseInt(dummy_mc.style.height)+1,.01,'px');
    //	zzz.fixTime;
    xxx = new Tween(dummy_mc.style, 'height', Tween.strongEaseInOut, parseInt(dummy_mc.style.height), finishHeight, 1.5, 'px');
    xxx.start();
    xxx.fixTime

    var a = new Object();
    a.onMotionFinished = function() {
        document.getElementById("body_right_panel").style.height = "100%";
        vertHeights();
    };
    xxx.addListener(a);
    xxx.start(); 
}

function showCV(forceOpen) {
    document.getElementById("body_main_panel").style.height = "100%";
    var a = new Object();
    a.onMotionFinished = function() {
        document.getElementById("body_right_panel").style.height = "100%";
        vertHeights();
    };
    
	dummy_mc = document.getElementById('CVWYSIWYG');
	if (dummy_mc.style.height == "0px" || forceOpen) {
	    xxx2 = new Tween(dummy_mc.style, 'height', Tween.strongEaseInOut, parseInt(dummy_mc.style.height), 360, 1, 'px');
	    xxx2.addListener(a);
	}
	else {
	    xxx2 = new Tween(dummy_mc.style, 'height', Tween.strongEaseInOut, parseInt(dummy_mc.style.height), 0, 1, 'px');
	    xxx2.addListener(a);
	}
    xxx2.start(); 
}

function selectAll(txtObj)
{
    if (txtObj.value=="email address")
        txtObj.select();
    if (txtObj.value=="password")
        txtObj.select();              
}



dd_location=false;
dd_sector=true;
function showLogin(clickObjID)
{
    srink='loginLink';
    expand='loginDiv';

    dummy_mc = document.getElementById(srink);
    zzz = new Tween(dummy_mc.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc.style.height),0,1,'px');
    zzz.fixTime;

    var a = new Object();
    a.onMotionFinished = function(){
        dummy_mc = document.getElementById(expand);
        xxx = new Tween(dummy_mc.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc.style.height),_loginDivHeight,1,'px');
        xxx.start(); 
        xxx.fixTime  
    };
    zzz.addListener(a);
    zzz.start(); 
   
}


function sendPassword(show)
{
    if(show)
    {
        srink='loginDiv';
        expand='passwordSendDiv';
        height=160;
    }
    else
    {
        srink='passwordSendDiv';
        expand='loginDiv';   
        height=210;
    }

    dummy_mc = document.getElementById(srink);
    zzz = new Tween(dummy_mc.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc.style.height),0,1,'px');
    zzz.fixTime();

    var a = new Object();
    a.onMotionFinished = function(){
        dummy_mc = document.getElementById(expand);
        xxx = new Tween(dummy_mc.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc.style.height),height,1,'px');
        xxx.start(); 
        xxx.fixTime();
    };
    zzz.addListener(a);
    zzz.start(); 
   
}

function sendPasswordAjax()
{
    var _email = document.getElementById('sendEmail2').value;
 
    new Ajax.Request('/jobs/ajax/spass.aspx', {parameters:'e='+_email+'&rand='+Math.floor(Math.random()*100000), onSuccess:function() {
    document.getElementById('loginMsg').innerHTML = '<span class="jobSearch" style="color:#dd1111;"> Password sent.</span>';
            var shrink='passwordSendDiv';
            var expand='loginDiv';
            var dummy_mc = document.getElementById(shrink);
            var zzz = new Tween(dummy_mc.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc.style.height),0,1,'px');
            zzz.fixTime();

            var a = {
                onMotionFinished : function() {
        dummy_mc = document.getElementById(expand);
        xxx = new Tween(dummy_mc.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc.style.height),_loginDivHeight,1,'px');
        xxx.start(); 
                    xxx.fixTime();
            }};
    zzz.addListener(a);
    zzz.start(); 
        }, 
        onFailure: function() {
        }
    });
    

}

function dropDownx(clickObjID)
{
    document.getElementById("body_main_panel").style.height = "100%";
    var a = new Object();
    a.onMotionFinished = function() {
        vertHeights();
    };

    document.getElementById('fillerMid').style.height=0;  
    height=0;
    if (clickObjID =="loginDiv")
    {
        if (dd_loginDiv)
        {
            height=0;
            expand='loginDiv';
        }
        else
        {
            height=loginDivHeight;
            expand='loginDiv';        
        }
        
        dd_loginDiv = !dd_loginDiv
    }
    
    dummy_mc = document.getElementById(expand);
    zzz = new Tween(dummy_mc.style,'height',Tween.strongEaseInOut,parseInt(dummy_mc.style.height),height,1,'px');
    zzz.fixTime;
    zzz.start(); 
    zzz.addListener(a);
}

