// this variable is used to set the height of the video page
var videoPageHeight = "412px";

//keydown func
//document.onkeydown = checkForEnter;


//function called on click of sign in button
function signIn_onClick( arg ) {

    if ( arg == 'fromForgotPwd' ) {

        oceanGetElementById("forgotPwdSent").style.display="none";
        oceanGetElementById("notsigninTd").style.display="block";
        return;

    }

    // check to ensure not already signed in on another window
    var userIdRef = oceanGetElementById("USERID");
    var userIdVal = userIdRef.value;
    var isSignedInCsv = "," + oceanGetCookie( "ISSIGNEDIN" ) + ",";
    if ( isSignedInCsv.indexOf( "," + userIdVal + "," ) != -1 ) {
        oceanGetElementById("errorMsgTd").style.display="block";
        oceanGetElementById("errorMsgTd").innerHTML="You are already signed-in as this user on another window.";
        userIdRef.focus();
        return;
    }

    oceanGetElementById("USERID").value=oceanGetElementById("USERID").value.toLowerCase();
    var userName = oceanTrim(oceanGetElementById("USERID").value); 
    var password = oceanGetElementById("pwdPassword").value;

    if(userName===""){
        //alert("Please enter Username.");
        oceanGetElementById("errorMsgTd").style.display="block";
        oceanGetElementById("errorMsgTd").innerHTML="Please enter Username.";
        oceanGetElementById("USERID").focus();
        return;
    }

    if(password === ""){
        //alert("Please enter Password.");
        oceanGetElementById("errorMsgTd").style.display="block";
        oceanGetElementById("errorMsgTd").innerHTML="Please enter Password.";
        oceanGetElementById("pwdPassword").focus();
        return;
    }

    //var curr_locn=window.location;
    //oceanSetCookie("currLocn",curr_locn);
    var locn=window.location.toString();
    if(locn.indexOf("portfolio_audio")!=-1 || locn.indexOf("portfolio_video")!=-1 || locn.indexOf("portfolio_graphic")!=-1 || locn.indexOf("portfolio_literature")!=-1){
        oceanGetElementById("init_page").value=oceanGetElementById("init_page").value+"?content_id="+content_id;
        oceanGetElementById("error_page").value=oceanGetElementById("error_page").value+"?content_id="+content_id;
    }else if(locn.indexOf("portfolio_landing")!=-1){
        oceanGetElementById("init_page").value=oceanGetElementById("init_page").value+"?type="+item_type;
        oceanGetElementById("error_page").value=oceanGetElementById("error_page").value+"?type="+item_type;
    }
    oceanGetElementById("loginForm").submit();
}

//function called when 'enter' key is pressed
function checkForEnter(e){

    if(e.keyCode==13){
        signIn_onClick();
        e.cancelBubble = true;
        return false;
    }
}

// method to remove current user from list of signed in users
function signOut_removeUserFromIsSignedInList() {

    var isSignedInCsv = oceanGetCookie( "ISSIGNEDIN" );
    var isSignedIn = [];
    if ( oceanTrim( isSignedInCsv ) !== '' ) {
        isSignedIn = isSignedInCsv.split( "," );
    }

    // look for current user
    var loopLen = isSignedIn.length;
    for ( var i = 0 ; i < loopLen ; i = i + 1 ) {

        var nextUser = isSignedIn[ i ];
        if ( nextUser == user ) {
            // remove from list
            isSignedIn.splice( i, 1 );
            break;
        }

    }

    // store new csv
    isSignedIn = isSignedIn.join( "," );
    oceanSetCookie( "ISSIGNEDIN", isSignedIn, true );

}

 //function to sign out.
 function signOut_onClick(){
   
    var currLocn=window.location;
    currLocn=currLocn.toString();
    if(currLocn.indexOf("membership")!=-1){
        currLocn="home.html.nocache";
    }

    signOut_removeUserFromIsSignedInList();

    oceanLogout(currLocn);     
 }

//function to show forgotPwd section
function showforgotpwdsection(){
    oceanGetElementById("notsigninTd").style.display="none";
    oceanGetElementById("forgotpwdTd").style.display="block";

}

//function called when clicked on forgot/name pwd link
function submit_onClick(id){
    if(id=="forgotpwd"){
        var email_add = oceanGetElementById("txtForgotEmail").value;
        if(email_add === ""){
            //alert("Please enter an email address");
            oceanGetElementById("errorMsgForgotPwdTd").style.display="block";
            oceanGetElementById("errorMsgForgotPwdTd").innerHTML="Please enter an email address.";
            return;
        }
        var sql="if ( EXISTS ( select top 1 * from ocean_user where email like '"+email_add+"')) select username,password from ocean_user where email='"+email_add+"' else select -1";
        sql=oceanEscape("url",sql);
        oceanExecLong("query.sql()","blueprint=com.webflow.dcosmic&sql="+sql,"forgotpwd1()");  
    }else if(id=="validateedit"){
          validateEditFields();
    } else {
        // generic - not part of logging in section - pass the method name as parameter
        eval( id );
    }
}

//callback
function forgotpwd1(res){

    res=eval(res);
    if(res[0][0] == -1){
        //alert("No such email id exists.");
        oceanGetElementById("errorMsgForgotPwdTd").style.display="block";
        oceanGetElementById("errorMsgForgotPwdTd").innerHTML="No such email address has been registered as a member of DCosmic.";
        return;
    }
    var email_add = oceanGetElementById("txtForgotEmail").value;
    var subject = "Password Details";
    var message = "Hi,\n\nYour login details are as follows:\n\nUsername: "+res[0][0]+"\nPassword: "+res[0][1]+"\n\nThanking You,\nThe DCosmic Team.";
    var from = "veenadhari@ocean-global.com";
    var jspArgs = "separateThread=T&to=" + email_add + "&from=" + from + "&fromName=DCosmic Team&subject=" + subject + "&textOnly=T&message=" +message;
    oceanExecLong("emailManager.sendMail()",jspArgs, "doneSendMail()");     
}

//function done send mail
function doneSendMail(res){

    res = webflowTrim(res); 
                  
    /*
    if (res != "ok"){
       alert("Error sending the request.... Send Failed.");
       return;
    }
    */
    //alert("A Password reminder email has been sent to this email id.");
    //oceanGetElementById("errorMsgForgotPwdTd").style.display="block";
    //oceanGetElementById("errorMsgForgotPwdTd").innerHTML="";
    oceanGetElementById("txtForgotEmail").value="";
    //oceanGetElementById("notsigninTd").style.display="block";
    oceanGetElementById("forgotPwdSent").style.display="block";
    oceanGetElementById("forgotpwdTd").style.display="none";

}

//edit profile
function editProfile(){
    location.href="membership.html.nocache?userid="+user_id;
}

//set feature pod
function setFeaturePod(){
    oceanGetElementById("podTitle").innerHTML=pod_info[0][1];
    oceanGetElementById("podImg").src="pod/"+pod_info[0][2];
    pod_info[0][4]=pod_info[0][4].replace(/\n/g,"<br>");
    oceanGetElementById("podDesc").innerHTML=pod_info[0][4];
    var display=(pod_info[0][5]==="")? "none" : "block";
    oceanGetElementById("podURL").style.display=display;
}

//function called onclick of register button
function register_onClick(){
    location.href='membership.html.nocache?userid=-1';
}

// sign out of the application
function doSignOut() {

    if ( oceanIsUserLoggedIn() === true ) {

        signOut_removeUserFromIsSignedInList();

    }

}


// ensure that given username is registered 
// on the ISSIGNEDIN cookie list
function ensureIsOnSigninList( user ) {

    // check to see if already on signin list
    var isSignedIn = oceanGetCookie( "ISSIGNEDIN" );
    var isSignedInCsv = "," + isSignedIn + ",";
    if ( isSignedInCsv.indexOf( "," + user + "," ) != -1 ) {

        return;

    }

    // if not, add to list
    if ( oceanTrim( isSignedIn ) === '' ) {

        isSignedIn = [];

    } else {

        isSignedIn = isSignedIn.split( "," );

    }

    isSignedIn.push( user );
    isSignedIn = isSignedIn.join( "," );

    // update sign in list cookie value
    oceanSetCookie( "ISSIGNEDIN", isSignedIn, true );

}

// change background image and page style depending on page type.
function setDisplayType( type ){
    if ( type === 0 ){
//        oceanGetElementById("imgBg").src = "images/Portfoliolanding_bckgrndwoc.jpg?version=" + dcosmic_version_number;
        document.body.style.backgroundImage = "url(images/Portfoliolanding_bckgrndwoc.jpg?version=" + dcosmic_version_number + ")";
        oceanGetElementById("promoTd").style.height = "270px";
        oceanGetElementById("promoTd").style.paddingLeft = "15px";
        videoPageHeight = "270px";
    }else{
    //    oceanGetElementById("imgBg").src = "images/portfolio_bg_new.jpg?version=" + dcosmic_version_number;
        document.body.style.backgroundImage = "url(images/portfolio_bg_new.jpg?version=" + dcosmic_version_number + ")";
        oceanGetElementById("promoTd").style.height = "412px";
        oceanGetElementById("promoTd").style.paddingLeft = "0px";
        videoPageHeight = "412px";
    }
}

document.body.onbeforeunload = doSignOut;
